Skip to content

Commit

Permalink
Fixed inheritance of transformations in Trash.
Browse files Browse the repository at this point in the history
  • Loading branch information
FoboCasteR authored and dblock committed Aug 22, 2014
1 parent 0598fcc commit 05aee4d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions lib/hashie/trash.rb
Expand Up @@ -40,13 +40,15 @@ def self.property(property_name, options = {})
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
Expand Down Expand Up @@ -79,10 +81,6 @@ def self.permitted_input_keys

private

def self.translations
@translations ||= {}
end

def self.inverse_translations
@inverse_translations ||= Hash[translations.map(&:reverse)]
end
Expand Down
24 changes: 19 additions & 5 deletions spec/hashie/trash_spec.rb
Expand Up @@ -187,24 +187,38 @@ class TrashLambdaTest3 < Hashie::Trash
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

Expand Down

0 comments on commit 05aee4d

Please sign in to comment.