diff options
author | Michael Bleigh <michael@intridea.com> | 2011-01-27 08:23:17 -0600 |
---|---|---|
committer | Michael Bleigh <michael@intridea.com> | 2011-01-27 08:23:17 -0600 |
commit | 8956bee342a705f566a23666b003be145801d0fc (patch) | |
tree | 0c1c1066ffb65cbab2599991fe99a6f0a3c3158f | |
parent | 7f16c83468bf3601fe2a81f6bbba08e810bdd1e6 (diff) | |
download | hashie-8956bee342a705f566a23666b003be145801d0fc.tar.gz |
Closes #11 - Applied dismaldenizen's patch to allow block setting.
-rw-r--r-- | lib/hashie/dash.rb | 10 | ||||
-rw-r--r-- | lib/hashie/mash.rb | 6 | ||||
-rw-r--r-- | spec/hashie/dash_spec.rb | 20 | ||||
-rw-r--r-- | spec/hashie/mash_spec.rb | 14 |
4 files changed, 44 insertions, 6 deletions
diff --git a/lib/hashie/dash.rb b/lib/hashie/dash.rb index 74612fb..f734abc 100644 --- a/lib/hashie/dash.rb +++ b/lib/hashie/dash.rb @@ -36,12 +36,12 @@ module Hashie unless instance_methods.map { |m| m.to_s }.include?("#{property_name}=") class_eval <<-ACCESSORS - def #{property_name} - _regular_reader(#{property_name.to_s.inspect}) + def #{property_name}(&block) + self.[](#{property_name.to_s.inspect}, &block) end def #{property_name}=(value) - _regular_writer(#{property_name.to_s.inspect}, value) + self.[]=(#{property_name.to_s.inspect}, value) end ACCESSORS end @@ -92,7 +92,9 @@ module Hashie # property's default value if it hasn't been set). def [](property) assert_property_exists! property - super(property.to_s) + value = super(property.to_s) + yield value if block_given? + value end # Set a value on the Dash in a Hash-like way. Only works diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb index f3d90f1..9c34b49 100644 --- a/lib/hashie/mash.rb +++ b/lib/hashie/mash.rb @@ -67,7 +67,9 @@ module Hashie # Retrieves an attribute set in the Mash. Will convert # any key passed in to a string before retrieving. def [](key) - regular_reader(convert_key(key)) + value = regular_reader(convert_key(key)) + yield value if block_given? + value end # Sets an attribute in the Mash. Key will be converted to @@ -145,7 +147,7 @@ module Hashie end def method_missing(method_name, *args, &blk) - return self[method_name] if key?(method_name) + return self.[](method_name, &blk) if key?(method_name) match = method_name.to_s.match(/(.*?)([?=!]?)$/) case match[2] when "=" diff --git a/spec/hashie/dash_spec.rb b/spec/hashie/dash_spec.rb index f7412e6..2de5490 100644 --- a/spec/hashie/dash_spec.rb +++ b/spec/hashie/dash_spec.rb @@ -54,6 +54,26 @@ describe DashTest do subject.first_name.should == 'Franklin' end end + + context 'reading from properties' do + it 'fails reading from a non-existent property using []' do + lambda { subject['nonexistent'] }.should raise_error(NoMethodError) + end + + it "should be able to retrieve properties through blocks" do + subject["first_name"] = "Aiden" + value = nil + subject.[]("first_name") { |v| value = v } + value.should == "Aiden" + end + + it "should be able to retrieve properties through blocks with method calls" do + subject["first_name"] = "Frodo" + value = nil + subject.first_name { |v| value = v } + value.should == "Frodo" + end + end describe '.new' do it 'fails with non-existent properties' do diff --git a/spec/hashie/mash_spec.rb b/spec/hashie/mash_spec.rb index eed1189..c61592c 100644 --- a/spec/hashie/mash_spec.rb +++ b/spec/hashie/mash_spec.rb @@ -18,6 +18,20 @@ describe Hashie::Mash do @mash["test"] = "abc" @mash.test.should == "abc" end + + it "should be able to retrieve set values through blocks" do + @mash["test"] = "abc" + value = nil + @mash.[]("test") { |v| value = v } + value.should == "abc" + end + + it "should be able to retrieve set values through blocks with method calls" do + @mash["test"] = "abc" + value = nil + @mash.test { |v| value = v } + value.should == "abc" + end it "should test for already set values when passed a ? method" do @mash.test?.should be_false |