diff options
author | Matt White <mattw922@gmail.com> | 2016-09-16 11:37:49 -0700 |
---|---|---|
committer | Matt White <mattw922@gmail.com> | 2016-09-16 13:29:10 -0700 |
commit | d603f62549657e108bff278c566e99621c071e02 (patch) | |
tree | 3aa3ce19a74063cfe7017d1f1c7830be3e32d7df | |
parent | 6186fe4b7d9392ce9db48ee9c9efbd311fac931a (diff) | |
download | hashie-d603f62549657e108bff278c566e99621c071e02.tar.gz |
Use existing translations when using IndifferentAccess and
IgnoreUndeclared
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | lib/hashie/extensions/dash/indifferent_access.rb | 3 | ||||
-rw-r--r-- | spec/hashie/extensions/indifferent_access_spec.rb | 37 |
3 files changed, 40 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 7940701..0fc3719 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,7 +28,7 @@ scheme are considered to be bugs. ### Fixed -* Your contribution here. +* [#369](https://github.com/intridea/hashie/pull/369): If a translation for a property exists when using IndifferentAccess and IgnoreUndeclared, use the translation to find the property - [@whitethunder](https://github.com/whitethunder). ### Security diff --git a/lib/hashie/extensions/dash/indifferent_access.rb b/lib/hashie/extensions/dash/indifferent_access.rb index d0d9b99..9d1b87b 100644 --- a/lib/hashie/extensions/dash/indifferent_access.rb +++ b/lib/hashie/extensions/dash/indifferent_access.rb @@ -11,6 +11,7 @@ module Hashie # Check to see if the specified property has already been # defined. def property?(name) + name = translations[name.to_sym] if included_modules.include?(Hashie::Extensions::Dash::PropertyTranslation) && translation_exists?(name) name = name.to_s !!properties.find { |property| property.to_s == name } end @@ -21,7 +22,7 @@ module Hashie end def transformed_property(property_name, value) - transform = transforms[property_name] || transforms[:"#{property_name}"] + transform = transforms[property_name] || transforms[property_name.to_sym] transform.call(value) end diff --git a/spec/hashie/extensions/indifferent_access_spec.rb b/spec/hashie/extensions/indifferent_access_spec.rb index 294c0b4..a4c2f43 100644 --- a/spec/hashie/extensions/indifferent_access_spec.rb +++ b/spec/hashie/extensions/indifferent_access_spec.rb @@ -31,6 +31,13 @@ describe Hashie::Extensions::IndifferentAccess do property :foo end + class IndifferentHashWithIgnoreUndeclaredAndPropertyTranslation < Hashie::Dash + include Hashie::Extensions::IgnoreUndeclared + include Hashie::Extensions::Dash::PropertyTranslation + include Hashie::Extensions::IndifferentAccess + property :foo, from: :bar + end + describe '#merge' do it 'indifferently merges in a hash' do indifferent_hash = Class.new(::Hash) do @@ -66,6 +73,36 @@ describe Hashie::Extensions::IndifferentAccess do end end + describe 'when translating properties and ignoring undeclared' do + let(:value) { 'baz' } + + subject { IndifferentHashWithIgnoreUndeclaredAndPropertyTranslation.new(params) } + + context 'and the hash keys are strings' do + let(:params) { { 'bar' => value } } + + it 'sets the property' do + expect(subject[:foo]).to eq value + end + end + + context 'and the hash keys are symbols' do + let(:params) { { bar: 'baz' } } + + it 'sets the property' do + expect(subject[:foo]).to eq value + end + end + + context 'and there are undeclared keys' do + let(:params) { { 'bar' => 'baz', 'fail' => false } } + + it 'sets the property' do + expect(subject[:foo]).to eq value + end + end + end + shared_examples_for 'hash with indifferent access' do it 'is able to access via string or symbol' do h = subject.build(abc: 123) |