Skip to content

Commit

Permalink
Add EnvironmentVariables sugar for #[] and #[]=
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Martin committed Oct 27, 2015
1 parent b102e28 commit 1a11ee9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
38 changes: 30 additions & 8 deletions lib/xcodeproj/scheme/environment_variables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ class XCScheme
#
class EnvironmentVariables < XMLElementWrapper
# @param [nil,REXML::Element,Array<EnvironmentVariable>,Array<Hash{Symbol => 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 => <String>, :value => <String>(, :enabled => <Bool>)}.
# @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
Expand All @@ -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 => <String>, :value => <String>(, :enabled => <Bool>)}.
# 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<EnvironmentVariable>]
# The new set of environment variables after addition
Expand All @@ -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<Hash{Symbol => String,Bool}>
# The current environment variables represented as an array
#
Expand Down
26 changes: 26 additions & 0 deletions spec/scheme/environment_variables_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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('<EnvironmentVariables>' \
"<EnvironmentVariable key='key1' value='value1' isEnabled='NO'/>" \
'</EnvironmentVariables>').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 == "<EnvironmentVariable key='key1' value='updated' isEnabled='YES'/>"
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 == "<EnvironmentVariable key='key2' value='inserted' isEnabled='YES'/>"
end
end
end

describe XCScheme::EnvironmentVariable do
Expand Down

0 comments on commit 1a11ee9

Please sign in to comment.