Permalink
Browse files

Closes #11 - Applied dismaldenizen's patch to allow block setting.

  • Loading branch information...
1 parent 7f16c83 commit 8956bee342a705f566a23666b003be145801d0fc Michael Bleigh committed Jan 27, 2011
Showing with 44 additions and 6 deletions.
  1. +6 −4 lib/hashie/dash.rb
  2. +4 −2 lib/hashie/mash.rb
  3. +20 −0 spec/hashie/dash_spec.rb
  4. +14 −0 spec/hashie/mash_spec.rb
View
@@ -36,12 +36,12 @@ def self.property(property_name, options = {})
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 @@ def initialize(attributes = {}, &block)
# 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
View
@@ -67,7 +67,9 @@ def id #:nodoc:
# 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 @@ def respond_to?(method_name)
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 "="
View
@@ -54,6 +54,26 @@ class Subclassed < DashTest
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
View
@@ -18,6 +18,20 @@
@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

0 comments on commit 8956bee

Please sign in to comment.