diff options
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | lib/hashie/extensions/method_access.rb | 35 | ||||
-rw-r--r-- | spec/hashie/dash_spec.rb | 23 | ||||
-rw-r--r-- | spec/hashie/extensions/method_access_spec.rb | 10 |
4 files changed, 61 insertions, 8 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index b758559..fbfc785 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * [#310](https://github.com/intridea/hashie/pull/310): Fixed `Hashie::Extensions::SafeAssignment` bug with private methods - [@marshall-lee](https://github.com/marshall-lee). * [#313](https://github.com/intridea/hashie/pull/313): Restrict pending spec to only Ruby versions 2.2.0-2.2.2 - [@pboling](https://github.com/pboling). * [#315](https://github.com/intridea/hashie/pull/315): Default `bin/` scripts: `console` and `setup` - [@pboling](https://github.com/pboling). +* [#317](https://github.com/intridea/hashie/pull/317): Ensure `Hashie::Extensions::MethodQuery` methods return boolean values - [@michaelherold](https://github.com/michaelherold). ## 3.4.2 (6/2/2015) diff --git a/lib/hashie/extensions/method_access.rb b/lib/hashie/extensions/method_access.rb index 9c3d537..0ed2b22 100644 --- a/lib/hashie/extensions/method_access.rb +++ b/lib/hashie/extensions/method_access.rb @@ -107,16 +107,41 @@ module Hashie # h.hji? # => NoMethodError module MethodQuery def respond_to?(name, include_private = false) - return true if name.to_s =~ /(.*)\?$/ && (key?(Regexp.last_match[1]) || key?(Regexp.last_match[1].to_sym)) - super + if query_method?(name) && indifferent_key?(key_from_query_method(name)) + true + else + super + end end def method_missing(name, *args) - if args.empty? && name.to_s =~ /(.*)\?$/ && (key?(Regexp.last_match[1]) || key?(Regexp.last_match[1].to_sym)) - return self[Regexp.last_match[1]] || self[Regexp.last_match[1].to_sym] + return super unless args.empty? + + if query_method?(name) + key = key_from_query_method(name) + if indifferent_key?(key) + !!(self[key] || self[key.to_sym]) + else + super + end + else + super end + end - super + private + + def indifferent_key?(name) + name = name.to_s + key?(name) || key?(name.to_sym) + end + + def key_from_query_method(query_method) + query_method.to_s[0..-2] + end + + def query_method?(name) + name.to_s.end_with?('?') end end diff --git a/spec/hashie/dash_spec.rb b/spec/hashie/dash_spec.rb index 1de533f..453d62f 100644 --- a/spec/hashie/dash_spec.rb +++ b/spec/hashie/dash_spec.rb @@ -511,3 +511,26 @@ context 'Dynamic Dash Class' do expect(my_property).to eq(my_orig) end end + +context 'with method access' do + class DashWithMethodAccess < Hashie::Dash + include Hashie::Extensions::IndifferentAccess + include Hashie::Extensions::MethodQuery + + property :test + end + + subject(:dash) { DashWithMethodAccess.new(test: 'value') } + + describe '#test' do + subject { dash.test } + + it { is_expected.to eq('value') } + end + + describe '#test?' do + subject { dash.test? } + + it { is_expected.to eq true } + end +end diff --git a/spec/hashie/extensions/method_access_spec.rb b/spec/hashie/extensions/method_access_spec.rb index b48b4e1..03528e4 100644 --- a/spec/hashie/extensions/method_access_spec.rb +++ b/spec/hashie/extensions/method_access_spec.rb @@ -92,15 +92,19 @@ describe Hashie::Extensions::MethodQuery do subject { QueryHash } it 'is true for non-nil string key values' do - expect(subject.new('abc' => 123)).to be_abc + expect(subject.new('abc' => 123).abc?).to eq true end it 'is true for non-nil symbol key values' do - expect(subject.new(abc: 123)).to be_abc + expect(subject.new(abc: 123).abc?).to eq true + end + + it 'is false for false key values' do + expect(subject.new(abc: false).abc?).to eq false end it 'is false for nil key values' do - expect(subject.new(abc: false)).not_to be_abc + expect(subject.new(abc: nil).abc?).to eq false end it 'raises a NoMethodError for non-set keys' do |