Skip to content

Instantly share code, notes, and snippets.

@anibali
Created October 10, 2010 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anibali/618761 to your computer and use it in GitHub Desktop.
Save anibali/618761 to your computer and use it in GitHub Desktop.
From c75fd5684970834d8e440deb899adba8a886e97b Mon Sep 17 00:00:00 2001
From: Aiden Nibali <dismaldenizen@gmail.com>
Date: Sun, 10 Oct 2010 11:09:56 +1100
Subject: [PATCH 1/2] added block support for Mash
---
lib/hashie/mash.rb | 6 ++++--
spec/hashie/mash_spec.rb | 14 ++++++++++++++
2 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb
index 97af028..6fa98ef 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
@@ -141,7 +143,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/mash_spec.rb b/spec/hashie/mash_spec.rb
index 5aa8818..f347557 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
--
1.7.0.4
From 6f51fce29421f8da32b8091f05a2ef46fb4acc5a Mon Sep 17 00:00:00 2001
From: Aiden Nibali <dismaldenizen@gmail.com>
Date: Sun, 10 Oct 2010 11:23:40 +1100
Subject: [PATCH 2/2] added block support for Dash
---
lib/hashie/dash.rb | 10 ++++++----
spec/hashie/dash_spec.rb | 20 ++++++++++++++++++++
2 files changed, 26 insertions(+), 4 deletions(-)
diff --git a/lib/hashie/dash.rb b/lib/hashie/dash.rb
index 46f877e..96a1b03 100644
--- a/lib/hashie/dash.rb
+++ b/lib/hashie/dash.rb
@@ -34,12 +34,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
@@ -90,7 +90,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/spec/hashie/dash_spec.rb b/spec/hashie/dash_spec.rb
index 577bd65..f9d2c6d 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
--
1.7.0.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment