Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transformations in Trash are inherited correctly #207

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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): Transformations in Trash are inherited correctly - [@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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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