Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix merge and merge! methods for Dash. #122

Merged
merged 2 commits into from Mar 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/hashie/dash.rb
Expand Up @@ -127,6 +127,21 @@ def []=(property, value)
super(property.to_s, value)
end

def merge(other_hash)
new_dash = self.dup
other_hash.each do |k,v|
new_dash[k] = block_given? ? yield(k, self[k], v) : v
end
new_dash
end

def merge!(other_hash)
other_hash.each do |k,v|
self[k] = block_given? ? yield(k, self[k], v) : v
end
self
end

def replace(other_hash)
other_hash = self.class.defaults.merge(other_hash)
(keys - other_hash.keys).each { |key| delete(key) }
Expand Down
58 changes: 58 additions & 0 deletions spec/hashie/dash_spec.rb
Expand Up @@ -147,6 +147,64 @@ class DeferredTest < Hashie::Dash
end
end

describe '#merge' do
it 'creates a new instance of the Dash' do
new_dash = subject.merge(:first_name => 'Robert')
subject.object_id.should_not == new_dash.object_id
end

it 'merges the given hash' do
new_dash = subject.merge(:first_name => 'Robert', :email => 'robert@example.com')
new_dash.first_name.should == 'Robert'
new_dash.email.should == 'robert@example.com'
end

it 'fails with non-existent properties' do
expect { subject.merge(:middle_name => 'James') }.to raise_error(NoMethodError)
end

it 'errors out when attempting to set a required property to nil' do
expect { subject.merge(:first_name => nil) }.to raise_error(ArgumentError)
end

context "given a block" do
it "sets merged key's values to the block's return value" do
subject.merge(:first_name => 'Jim') do |key, oldval, newval|
"#{key}: #{newval} #{oldval}"
end.first_name.should == 'first_name: Jim Bob'
end
end
end

describe '#merge!' do
it 'modifies the existing instance of the Dash' do
original_dash = subject.merge!(:first_name => 'Robert')
subject.object_id.should == original_dash.object_id
end

it 'merges the given hash' do
subject.merge!(:first_name => 'Robert', :email => 'robert@example.com')
subject.first_name.should == 'Robert'
subject.email.should == 'robert@example.com'
end

it 'fails with non-existent properties' do
expect { subject.merge!(:middle_name => 'James') }.to raise_error(NoMethodError)
end

it 'errors out when attempting to set a required property to nil' do
expect { subject.merge!(:first_name => nil) }.to raise_error(ArgumentError)
end

context "given a block" do
it "sets merged key's values to the block's return value" do
subject.merge!(:first_name => 'Jim') do |key, oldval, newval|
"#{key}: #{newval} #{oldval}"
end.first_name.should == 'first_name: Jim Bob'
end
end
end

describe 'properties' do
it 'lists defined properties' do
described_class.properties.should == Set.new([:first_name, :email, :count])
Expand Down