diff options
author | Erol Fornoles <erol.fornoles@gmail.com> | 2014-12-28 14:22:17 -0500 |
---|---|---|
committer | dB <dblock@dblock.org> | 2014-12-28 14:22:17 -0500 |
commit | a4938721f95f732aa335e0fd0963269f388b9b07 (patch) | |
tree | 4afcdb8a2d171b06df675fb16b462646505ccef1 | |
parent | 51299aa79498636a8781026baf48506786814637 (diff) | |
download | hashie-a4938721f95f732aa335e0fd0963269f388b9b07.tar.gz |
Inherit key coercions.
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | lib/hashie/extensions/coercion.rb | 14 | ||||
-rw-r--r-- | spec/hashie/extensions/coercion_spec.rb | 15 |
3 files changed, 27 insertions, 3 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b01f7a..ce62a40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * [#249](https://github.com/intridea/hashie/pull/249): SafeAssignment will now also protect hash-style assignments - [@jrochkind](https://github.com/jrochkind). * [#251](https://github.com/intridea/hashie/pull/251): Add block to indifferent access #fetch - [@jgraichen](https://github.com/jgraichen). * [#252](https://github.com/intridia/hashie/pull/252): Add support for conditionally required Hashie::Dash attributes - [@ccashwell](https://github.com/ccashwell). +* [#256](https://github.com/intridia/hashie/pull/256): Inherit key coercions - [@Erol](https://github.com/Erol). * Your contribution here. ## 3.3.2 (11/26/2014) diff --git a/lib/hashie/extensions/coercion.rb b/lib/hashie/extensions/coercion.rb index 8c8762b..601f21b 100644 --- a/lib/hashie/extensions/coercion.rb +++ b/lib/hashie/extensions/coercion.rb @@ -86,6 +86,9 @@ module Hashie end module ClassMethods + attr_writer :key_coercions + protected :key_coercions= + # Set up a coercion rule such that any time the specified # key is set it will be coerced into the specified class. # Coercion will occur by first attempting to call Class.coerce @@ -101,16 +104,15 @@ module Hashie # coerce_key :user, User # end def coerce_key(*attrs) - @key_coercions ||= {} into = attrs.pop - attrs.each { |key| @key_coercions[key] = into } + attrs.each { |key| key_coercions[key] = into } end alias_method :coerce_keys, :coerce_key # Returns a hash of any existing key coercions. def key_coercions - @key_coercions || {} + @key_coercions ||= {} end # Returns the specific key coercion for the specified key, @@ -172,6 +174,12 @@ module Hashie from = value.class strict_value_coercions[from] || lenient_value_coercions[from] end + + def inherited(klass) + super + + klass.key_coercions = key_coercions + end end end end diff --git a/spec/hashie/extensions/coercion_spec.rb b/spec/hashie/extensions/coercion_spec.rb index 682bfcd..1de2d5d 100644 --- a/spec/hashie/extensions/coercion_spec.rb +++ b/spec/hashie/extensions/coercion_spec.rb @@ -404,6 +404,21 @@ describe Hashie::Extensions::Coercion do expect(tweet[:user]).to be_a(UserHash) end end + + context 'when subclassing' do + class MyHash < Hash + include Hashie::Extensions::Coercion + + coerce_key :value, Integer + end + + class MySubclass < MyHash + end + + it 'inherits key coercions' do + expect(MyHash.key_coercions).to eql(MySubclass.key_coercions) + end + end end describe '#coerce_value' do |