diff options
author | FoboCasteR <fobocaster@gmail.com> | 2014-08-22 14:55:54 -0400 |
---|---|---|
committer | dblock <dblock@dblock.org> | 2014-08-22 14:55:54 -0400 |
commit | 05aee4d5b8b842729fcd3a91fa26253e7ddb3f91 (patch) | |
tree | d01baf8bb193f38dede76d42acc6ddf94bfba409 | |
parent | 0598fcc24150001671c376f0ef618dc83a4adbe8 (diff) | |
download | hashie-05aee4d5b8b842729fcd3a91fa26253e7ddb3f91.tar.gz |
Fixed inheritance of transformations in Trash.
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | lib/hashie/trash.rb | 8 | ||||
-rw-r--r-- | spec/hashie/trash_spec.rb | 24 |
3 files changed, 23 insertions, 10 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index c4bf894..5ed3704 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * [#204](https://github.com/intridea/hashie/pull/204): Added Hashie::Extensions::MethodOverridingWriter and Hashie::Extensions::MethodAccessWithOverride - [@michaelherold](https://github.com/michaelherold). * [#205](http://github.com/intridea/hashie/pull/205): Added Hashie::Extensions::Mash::SafeAssignment - [@michaelherold](https://github.com/michaelherold). * [#206](http://github.com/intridea/hashie/pull/206): Fixed stack overflow from repetitively including coercion in subclasses - [@michaelherold](https://github.com/michaelherold). +* [#207](http://github.com/intridea/hashie/pull/207): Fixed inheritance of transformations in Trash - [@fobocaster](https://github.com/fobocaster). * Your contribution here. ## 3.2.0 (7/10/2014) diff --git a/lib/hashie/trash.rb b/lib/hashie/trash.rb index e0adb10..204b0d7 100644 --- a/lib/hashie/trash.rb +++ b/lib/hashie/trash.rb @@ -40,13 +40,15 @@ module Hashie end class << self - attr_reader :transforms + attr_reader :transforms, :translations end instance_variable_set('@transforms', {}) + instance_variable_set('@translations', {}) def self.inherited(klass) super klass.instance_variable_set('@transforms', transforms.dup) + klass.instance_variable_set('@translations', translations.dup) end # Set a value on the Dash in a Hash-like way. Only works @@ -79,10 +81,6 @@ module Hashie private - def self.translations - @translations ||= {} - end - def self.inverse_translations @inverse_translations ||= Hash[translations.map(&:reverse)] end diff --git a/spec/hashie/trash_spec.rb b/spec/hashie/trash_spec.rb index b570ef2..e1d35fb 100644 --- a/spec/hashie/trash_spec.rb +++ b/spec/hashie/trash_spec.rb @@ -187,24 +187,38 @@ describe Hashie::Trash do end describe 'inheritable transforms' do - class TrashA < Hashie::Trash + class TransformA < Hashie::Trash property :some_value, transform_with: lambda { |v| v.to_i } end - class TrashB < TrashA + class TransformB < TransformA property :some_other_value, transform_with: lambda { |v| v.to_i } end - class TrashC < TrashB + class TransformC < TransformB property :some_value, transform_with: lambda { |v| -v.to_i } end it 'inherit properties transforms' do - expect(TrashB.new(some_value: '123', some_other_value: '456').some_value).to eq(123) + expect(TransformB.new(some_value: '123', some_other_value: '456').some_value).to eq(123) end it 'replaces property transform' do - expect(TrashC.new(some_value: '123', some_other_value: '456').some_value).to eq(-123) + expect(TransformC.new(some_value: '123', some_other_value: '456').some_value).to eq(-123) + end + end + + describe 'inheritable translations' do + class TranslationA < Hashie::Trash + property :some_value, from: 'someValue', with: lambda { |v| v.to_i } + end + + class TranslationB < TranslationA + property :some_other_value, from: 'someOtherValue' + end + + it 'inherit properties translations' do + expect(TranslationB.new('someValue' => '123').some_value).to eq(123) end end |