diff options
author | dblock <dblock@dblock.org> | 2014-03-30 19:38:21 -0400 |
---|---|---|
committer | dblock <dblock@dblock.org> | 2014-03-30 19:40:48 -0400 |
commit | 9f38d4508cb951185715c2e3afd3b4970a3d1116 (patch) | |
tree | 953e9a0c477ed336c760b1e118c911f00993568b | |
parent | cf9d24a2ae280aec042526f2500f3af32cd53d5a (diff) | |
parent | 5b132c6190c21b1328491d92a7cb7943de36c1b9 (diff) | |
download | hashie-9f38d4508cb951185715c2e3afd3b4970a3d1116.tar.gz |
Merge branch 'fix-113-dash-merge' of github.com:spencer1248/hashie
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | lib/hashie/dash.rb | 15 | ||||
-rw-r--r-- | spec/hashie/dash_spec.rb | 58 |
3 files changed, 74 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 91413fb..762658e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * [89](https://github.com/intridea/hashie/issues/89): Do not respond to every method with suffix in Hashie::Mash, fixes Rails strong_parameters - [@Maxim-Filimonov](https://github.com/Maxim-Filimonov). * [#110](https://github.com/intridea/hashie/pull/110): Correctly use Hash#default from Mash#method_missing - [@ryansouza](https://github.com/ryansouza). * [#120](https://github.com/intridea/hashie/pull/120): Pass options to recursive to_hash calls - [@pwillett](https://github.com/pwillett). +* [#113](https://github.com/intridea/hashie/issues/113): Fixed Hash#merge with Hashie::Dash - [@spencer1248](https://github.com/spencer1248). ## 2.0.5 diff --git a/lib/hashie/dash.rb b/lib/hashie/dash.rb index ff46d58..7a74895 100644 --- a/lib/hashie/dash.rb +++ b/lib/hashie/dash.rb @@ -121,6 +121,21 @@ module Hashie 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) } diff --git a/spec/hashie/dash_spec.rb b/spec/hashie/dash_spec.rb index 4299d97..6ed9c9a 100644 --- a/spec/hashie/dash_spec.rb +++ b/spec/hashie/dash_spec.rb @@ -151,6 +151,64 @@ describe DashTest do 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]) |