diff --git a/lib/xcodeproj/scheme/environment_variables.rb b/lib/xcodeproj/scheme/environment_variables.rb index 09a340b7b..b7b3911a2 100644 --- a/lib/xcodeproj/scheme/environment_variables.rb +++ b/lib/xcodeproj/scheme/environment_variables.rb @@ -11,11 +11,10 @@ class XCScheme # class EnvironmentVariables < XMLElementWrapper # @param [nil,REXML::Element,Array,Array String,Bool}>] node_or_variables - # The 'EnvironmentVariables' XML node that this object represents. If nil, it will create a default XML - # node to use. - # @note If initializing with an array of hashes, the hash should be of the form - # {:key => , :value => (, :enabled => )}. - # @see EnvironmentVariable#initialize + # The 'EnvironmentVariables' XML node that this object represents. + # - If nil, an empty 'EnvironmentVariables' XML node will be created + # - If an REXML::Element, it must be named 'EnvironmentVariables' + # - If an Array of objects or Hashes, they'll each be passed to {#assign_variable} # def initialize(node_or_variables = nil) create_xml_element_with_fallback(node_or_variables, VARIABLES_NODE) do @@ -34,9 +33,9 @@ def all_variables # Adds a given variable to the set of environment variables, or replaces it if that key already exists # # @param [EnvironmentVariable,Hash{Symbol => String,Bool}] variable - # The variable to set - # @note If initializing with an array of hashes, the hash should be of the form - # {:key => , :value => (, :enabled => )}. + # The variable to add or update, backed by an EnvironmentVariable node. + # - If an EnvironmentVariable, the previous reference should still be valid + # - If a Hash, must contain keys :key and :value (Strings) and optionally :enabled (Boolean) # @see EnvironmentVariable#initialize # @return [Array] # The new set of environment variables after addition @@ -62,6 +61,29 @@ def remove_variable(variable) @all_variables -= [env_var] end + # @param [String] key + # The key to lookup + # @return [EnvironmentVariable] variable + # Returns the matching environment variable for a specified key + # + def [](key) + all_variables.find { |var| var.key == key } + end + + # Assigns a value for a specified key + # + # @param [String] key + # The key to update in the environment variables + # @param [String] value + # The value to lookup + # @return [EnvironmentVariable] variable + # The newly updated environment variable + # + def []=(key, value) + assign_variable(:key => key, :value => value) + self[key] + end + # @return Array String,Bool}> # The current environment variables represented as an array # diff --git a/spec/scheme/environment_variables_spec.rb b/spec/scheme/environment_variables_spec.rb index cf23dc0a5..0237d79fd 100644 --- a/spec/scheme/environment_variables_spec.rb +++ b/spec/scheme/environment_variables_spec.rb @@ -159,6 +159,32 @@ def self.xml_for_object(object) subject.to_a.should == [:key => 'key1', :value => 'value1', :enabled => true] end end + + describe '#[] and #[]=' do + element = REXML::Document.new('' \ + "" \ + '').elements['EnvironmentVariables'] + subject = XCScheme::EnvironmentVariables.new(element) + + it 'updates an existing key if one exists, and automatically sets it to be enabled' do + subject['key1'].class.should == XCScheme::EnvironmentVariable + subject['key1'].to_h.should == { :key => 'key1', :value => 'value1', :enabled => false } + + subject['key1'] = 'updated' + subject.all_variables.count.should == 1 + subject['key1'].to_h.should == { :key => 'key1', :value => 'updated', :enabled => true } + Xcodeproj.xml_for_object(subject['key1']).should == "" + end + + it 'inserts a new key if a match does not already exists' do + subject['key2'].should.be.nil + + subject['key2'] = 'inserted' + subject.all_variables.count.should == 2 + subject['key2'].to_h.should == { :key => 'key2', :value => 'inserted', :enabled => true } + Xcodeproj.xml_for_object(subject['key2']).should == "" + end + end end describe XCScheme::EnvironmentVariable do