diff options
author | gregory <greg2502@gmail.com> | 2014-06-19 07:33:56 -0400 |
---|---|---|
committer | dblock <dblock@dblock.org> | 2014-06-19 07:33:56 -0400 |
commit | 11445218ecaa3bd0a1cb05f8bc50f697f46e3f0b (patch) | |
tree | 5a63d836e4654add76dd2b9419bc2d9e5b29b6b2 | |
parent | 4eaedf54bba117b39d43f00b536c0d496809815d (diff) | |
download | hashie-11445218ecaa3bd0a1cb05f8bc50f697f46e3f0b.tar.gz |
Auto include Dash::IndifferentAccess when Extensions::IndiferentAccess is included in Dash.
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | lib/hashie/extensions/indifferent_access.rb | 4 | ||||
-rw-r--r-- | spec/hashie/extensions/dash/indifferent_access_spec.rb | 10 | ||||
-rw-r--r-- | spec/hashie/extensions/indifferent_access_spec.rb | 14 |
5 files changed, 29 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index f1b1892..b8b0e8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ **Note:** This version introduces several backward incompatible API changes. See [UPGRADING](UPGRADING.md) for details. * [#172](https://github.com/intridea/hashie/pull/172): Added Dash and Trash#update_attributes! - [@gregory](https://github.com/gregory). +* [#173](https://github.com/intridea/hashie/pull/173): Auto include Dash::IndifferentAccess when Extensions::IndiferentAccess is included in Dash - [@gregory](https://github.com/gregory). * [#169](https://github.com/intridea/hashie/pull/169): Hash#to_hash will also convert nested objects that implement to_hash - [@gregory](https://github.com/gregory). * [#150](https://github.com/intridea/hashie/pull/159): Handle nil intermediate object on deep fetch - [@stephenaument](https://github.com/stephenaument). * [#146](https://github.com/intridea/hashie/issues/146): Mash#respond_to? inconsistent with #method_missing and does not respond to #permitted? - [@dblock](https://github.com/dblock). @@ -81,8 +81,6 @@ This extension can be mixed in to instantly give you indifferent access to your A unique feature of Hashie's IndifferentAccess mixin is that it will inject itself recursively into subhashes *without* reinitializing the hash in question. This means you can safely merge together indifferent and non-indifferent hashes arbitrarily deeply without worrying about whether you'll be able to `hash[:other][:another]` properly. -Use `Hashie::Extensions::Dash::IndifferentAccess` for instances of `Hashie::Dash`. - ### IgnoreUndeclared This extension can be mixed in to silently ignore undeclared properties on initialization instead of raising an error. This is useful when using a Trash to capture a subset of a larger hash. diff --git a/lib/hashie/extensions/indifferent_access.rb b/lib/hashie/extensions/indifferent_access.rb index 8af278e..4fc5bb7 100644 --- a/lib/hashie/extensions/indifferent_access.rb +++ b/lib/hashie/extensions/indifferent_access.rb @@ -24,6 +24,10 @@ module Hashie # module IndifferentAccess def self.included(base) + Hashie::Extensions::Dash::IndifferentAccess::ClassMethods.tap do |extension| + base.extend(extension) if base <= Hashie::Dash && !base.singleton_class.included_modules.include?(extension) + end + base.class_eval do alias_method :regular_writer, :[]= alias_method :[]=, :indifferent_writer diff --git a/spec/hashie/extensions/dash/indifferent_access_spec.rb b/spec/hashie/extensions/dash/indifferent_access_spec.rb index 80d300c..f88a280 100644 --- a/spec/hashie/extensions/dash/indifferent_access_spec.rb +++ b/spec/hashie/extensions/dash/indifferent_access_spec.rb @@ -6,6 +6,16 @@ describe Hashie::Extensions::Dash::IndifferentAccess do property :name end + context 'when included in Dash' do + let(:patch) { Hashie::Extensions::Dash::IndifferentAccess::ClassMethods } + let(:dash_class) { Class.new(Hashie::Dash) } + + it 'extends with the patch once' do + expect(patch).to receive(:extended).with(dash_class).once + dash_class.send(:include, Hashie::Extensions::Dash::IndifferentAccess) + end + end + context 'initialized with' do it 'string' do instance = DashWithIndifferentAccess.new('name' => 'Name') diff --git a/spec/hashie/extensions/indifferent_access_spec.rb b/spec/hashie/extensions/indifferent_access_spec.rb index 8ae982d..225a236 100644 --- a/spec/hashie/extensions/indifferent_access_spec.rb +++ b/spec/hashie/extensions/indifferent_access_spec.rb @@ -26,6 +26,20 @@ describe Hashie::Extensions::IndifferentAccess do end end + class IndifferentHashWithDash < Hashie::Dash + include Hashie::Extensions::IndifferentAccess + property :foo + end + + describe 'when included in dash' do + let(:params) { { foo: 'bar' } } + subject { IndifferentHashWithDash.new(params) } + + it 'initialize with a symbol' do + expect(subject.foo).to eq params[:foo] + 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) |