Skip to content

Commit

Permalink
Merge pull request #150 from ggiamarchi/issu146
Browse files Browse the repository at this point in the history
Override default merge method for our configuration
  • Loading branch information
ggiamarchi committed Nov 7, 2014
2 parents 8d98831 + f5d959b commit 97a9a2c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
30 changes: 30 additions & 0 deletions source/lib/vagrant-openstack-provider/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,36 @@ def initialize
@ssh_disabled = UNSET_VALUE
end

def merge(other)
result = self.class.new

# Set all of our instance variables on the new class
[self, other].each do |obj|
obj.instance_variables.each do |key|
# Ignore keys that start with a double underscore. This allows
# configuration classes to still hold around internal state
# that isn't propagated.
next if key.to_s.start_with?('@__')

# Don't set the value if it is the unset value, either.
value = obj.instance_variable_get(key)
print key
if [:@networks, :@volumes, :@rsync_includes].include? key
result.instance_variable_set(key, value) unless value.empty?
else
result.instance_variable_set(key, value) if value != UNSET_VALUE
end
end
end

# Persist through the set of invalid methods
this_invalid = @__invalid_methods || Set.new
other_invalid = other.instance_variable_get(:"@__invalid_methods") || Set.new
result.instance_variable_set(:"@__invalid_methods", this_invalid + other_invalid)

result
end

# rubocop:disable Style/CyclomaticComplexity
def finalize!
@password = nil if @password == UNSET_VALUE
Expand Down
49 changes: 49 additions & 0 deletions source/spec/vagrant-openstack-provider/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,55 @@
end
end

describe 'merge' do
let(:foo_class) do
Class.new(described_class) do
attr_accessor :networks
end
end

subject { foo_class.new }

context 'with original network not empty' do
it 'should overidde the config' do
one = foo_class.new
one.networks = ['foo']

two = foo_class.new
two.networks = ['bar']

result = one.merge(two)
result.networks.should =~ ['bar']
end
end

context 'with original network empty' do
it 'should add the network to the existing list' do
one = foo_class.new
one.networks = []

two = foo_class.new
two.networks = ['bar']

result = one.merge(two)
result.networks.should =~ ['bar']
end
end

context 'with original network not empty and new empty' do
it 'should keep the original network' do
one = foo_class.new
one.networks = ['foo']

two = foo_class.new
two.networks = []

result = one.merge(two)
result.networks.should =~ ['foo']
end
end
end

describe 'validation' do
let(:machine) { double('machine') }
let(:validation_errors) { subject.validate(machine)['Openstack Provider'] }
Expand Down

0 comments on commit 97a9a2c

Please sign in to comment.