summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJerry Cheung <jollyjerry@gmail.com>2013-03-18 14:02:32 -0700
committerJerry Cheung <jollyjerry@gmail.com>2013-03-18 14:02:32 -0700
commitfba4680c8c6ccc3c01597dc9518ae631ef7a399e (patch)
tree454f8e2b7510174b5fcd477ea98082fad3bc656d
parent12cac601c86cb0ce523612b6b9ecf1d574845eef (diff)
parentab75e0c2ad128b8d38f570430e9ac6806507b59f (diff)
downloadhashie-fba4680c8c6ccc3c01597dc9518ae631ef7a399e.tar.gz
Merge pull request #88 from 7even/fix_suffixed_respond_to
Fix Hashie::Mash#respond_to? with a suffixed key
-rw-r--r--lib/hashie/mash.rb46
-rw-r--r--spec/hashie/mash_spec.rb16
2 files changed, 39 insertions, 23 deletions
diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb
index ee26ca5..29b6271 100644
--- a/lib/hashie/mash.rb
+++ b/lib/hashie/mash.rb
@@ -181,29 +181,29 @@ module Hashie
self
end
- # Will return true if the Mash has had a key
- # set in addition to normal respond_to? functionality.
- def respond_to?(method_name, include_private=false)
- return true if key?(method_name)
- super
- end
-
- def method_missing(method_name, *args, &blk)
- return self.[](method_name, &blk) if key?(method_name)
- match = method_name.to_s.match(/(.*?)([?=!_]?)$/)
- case match[2]
- when "="
- self[match[1]] = args.first
- when "?"
- !!self[match[1]]
- when "!"
- initializing_reader(match[1])
- when "_"
- underbang_reader(match[1])
- else
- default(method_name, *args, &blk)
- end
- end
+ # Will return true if the Mash has had a key
+ # set in addition to normal respond_to? functionality.
+ def respond_to?(method_name, include_private=false)
+ return true if key?(method_name) || method_name.to_s.slice(/[=?!_]\Z/)
+ super
+ end
+
+ def method_missing(method_name, *args, &blk)
+ return self.[](method_name, &blk) if key?(method_name)
+ match = method_name.to_s.match(/(.*?)([?=!_]?)$/)
+ case match[2]
+ when "="
+ self[match[1]] = args.first
+ when "?"
+ !!self[match[1]]
+ when "!"
+ initializing_reader(match[1])
+ when "_"
+ underbang_reader(match[1])
+ else
+ default(method_name, *args, &blk)
+ end
+ end
protected
diff --git a/spec/hashie/mash_spec.rb b/spec/hashie/mash_spec.rb
index 4dbb349..ef688ab 100644
--- a/spec/hashie/mash_spec.rb
+++ b/spec/hashie/mash_spec.rb
@@ -297,6 +297,22 @@ describe Hashie::Mash do
it 'should respond to a set key' do
Hashie::Mash.new(:abc => 'def').should be_respond_to(:abc)
end
+
+ it 'should respond to a set key with a suffix' do
+ %w(= ? ! _).each do |suffix|
+ Hashie::Mash.new(:abc => 'def').should be_respond_to(:"abc#{suffix}")
+ end
+ end
+
+ it 'should respond to an unknown key with a suffix' do
+ %w(= ? ! _).each do |suffix|
+ Hashie::Mash.new(:abc => 'def').should be_respond_to(:"xyz#{suffix}")
+ end
+ end
+
+ it "should not respond to an unknown key without a suffix" do
+ Hashie::Mash.new(:abc => 'def').should_not be_respond_to(:xyz)
+ end
end
context "#initialize" do