diff options
author | Daniel Doubrovkine (dB.) <dblock@dblock.org> | 2021-11-08 02:14:40 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-08 02:14:40 +0000 |
commit | a979d7234ef1588a0a70ed48fe26c990725be8f6 (patch) | |
tree | 4770ed35cdc0e7ab50fa9b7c695839e451a3b63f | |
parent | cf61c298c5a146bacdb255d823d42445033c7cbd (diff) | |
parent | e295a0df579525ab1574a8ff6539a6c5a109fb2c (diff) | |
download | hashie-a979d7234ef1588a0a70ed48fe26c990725be8f6.tar.gz |
Merge pull request #547 from danwa5/issue-546-trash-properties
Fixed issue where a source hash key can be used in translating multip…
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | lib/hashie/extensions/dash/property_translation.rb | 7 | ||||
-rw-r--r-- | spec/hashie/trash_spec.rb | 17 |
3 files changed, 24 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 0db8b66..eb7203f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ Any violations of this scheme are considered to be bugs. * [#531](https://github.com/hashie/hashie/pull/531): Fixed [slice doesn't work using symbols](https://github.com/hashie/hashie/issues/529) using hash with `IndifferentAccess` extension - [@gnomex](https://github.com/gnomex). * [#533](https://github.com/hashie/hashie/pull/533): Fixed `NoMethodError: undefined method `to_json'` at `hashie/dash_spec` - [@gnomex](https://github.com/gnomex). * [#537](https://github.com/hashie/hashie/pull/537): Fixed inconsistencies with handling defaults in `Dash` with and without `IgnoreUnclared` mixed in - [@michaelherold](https://github.com/michaelherold). +* [#547](https://github.com/hashie/hashie/pull/547): Fixed issue where a source hash key can be used in translating multiple properties - [@danwa5](https://github.com/danwa5). * Your contribution here. ### Security diff --git a/lib/hashie/extensions/dash/property_translation.rb b/lib/hashie/extensions/dash/property_translation.rb index 8149faa..69a2d71 100644 --- a/lib/hashie/extensions/dash/property_translation.rb +++ b/lib/hashie/extensions/dash/property_translation.rb @@ -153,7 +153,12 @@ module Hashie def []=(property, value) if self.class.translation_exists? property send("#{property}=", value) - super(property, value) if self.class.properties.include?(property) + + if self.class.transformation_exists? property + super property, self.class.transformed_property(property, value) + elsif self.class.properties.include?(property) + super(property, value) + end elsif self.class.transformation_exists? property super property, self.class.transformed_property(property, value) elsif property_exists? property diff --git a/spec/hashie/trash_spec.rb b/spec/hashie/trash_spec.rb index 9998c8d..aaabe70 100644 --- a/spec/hashie/trash_spec.rb +++ b/spec/hashie/trash_spec.rb @@ -157,6 +157,23 @@ describe Hashie::Trash do end end + describe 'translating multiple properties from the same source hash key' do + class AnotherDataModel < Hashie::Trash + property :first_name, transform_with: ->(n) { n.upcase } + property :first_name_short, from: :first_name, transform_with: ->(n) { n[0, 3] } + end + + subject { AnotherDataModel.new(first_name: 'Cathy') } + + it 'translates the first key with the given lambda' do + expect(subject.first_name).to eq('CATHY') + end + + it 'translates the second key with the given lambda and the initial value of the first key' do + expect(subject.first_name_short).to eq('Cat') + end + end + describe 'uses with or transform_with interchangeably' do class TrashLambdaTestTransformWith < Hashie::Trash property :first_name, from: :firstName, transform_with: ->(value) { value.reverse } |