diff options
author | Michael Herold <opensource@michaeljherold.com> | 2018-08-11 11:10:14 -0500 |
---|---|---|
committer | Michael Herold <opensource@michaeljherold.com> | 2018-08-11 11:53:22 -0500 |
commit | b7209f6f580816bc2d030e1c52f2f2c688454bdc (patch) | |
tree | 06b5db063241fb1ee4d2590db02b39cd6be9b911 | |
parent | 50898af55a4fd9fac96e5748e59abee3614e786b (diff) | |
download | hashie-b7209f6f580816bc2d030e1c52f2f2c688454bdc.tar.gz |
Allow Trash to copy properties from others
Trash should be able to copy properties from other properties without
causing a problem. Trash should also, when doing this, not set any
properties that it doesn't define.
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | lib/hashie/extensions/dash/property_translation.rb | 1 | ||||
-rw-r--r-- | spec/hashie/trash_spec.rb | 48 |
3 files changed, 50 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index f04a249..365ecfc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ scheme are considered to be bugs. * [#436](https://github.com/intridea/hashie/pull/436): Ensure that `Hashie::Extensions::IndifferentAccess` injects itself after a non-destructive merge - [@michaelherold](https://github.com/michaelherold). * [#437](https://github.com/intridea/hashie/pull/437): Allow codependent properties to be set on Dash - [@michaelherold](https://github.com/michaelherold). * [#438](https://github.com/intridea/hashie/pull/438): Fix: `NameError (uninitialized constant Hashie::Extensions::Parsers::YamlErbParser::Pathname)` in `Hashie::Mash.load` - [@onk](https://github.com/onk). +* [#457](https://github.com/intridea/hashie/pull/457): Fix `Trash` to allow it to copy properties from other properties - [@michaelherold](https://github.com/michaelherold). * Your contribution here. ### Security diff --git a/lib/hashie/extensions/dash/property_translation.rb b/lib/hashie/extensions/dash/property_translation.rb index 25b6499..85bfb89 100644 --- a/lib/hashie/extensions/dash/property_translation.rb +++ b/lib/hashie/extensions/dash/property_translation.rb @@ -150,6 +150,7 @@ module Hashie def []=(property, value) if self.class.translation_exists? property send("#{property}=", value) + super(property, value) if self.class.properties.include?(property) 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 20b717c..a5e5d02 100644 --- a/spec/hashie/trash_spec.rb +++ b/spec/hashie/trash_spec.rb @@ -265,4 +265,52 @@ describe Hashie::Trash do expect(subject.first_name).to eq('Frodo') end end + + context 'when copying properties from other properties' do + it 'retains the original and also sets the copy' do + simple = Class.new(Hashie::Trash) do + property :id + property :copy_of_id, from: :id + end + + subject = simple.new(id: 1) + + expect(subject.id).to eq(1) + expect(subject.copy_of_id).to eq(1) + end + + it 'grabs the default for the original if it is not set' do + with_default = Class.new(Hashie::Trash) do + property :id, default: 0 + property :copy_of_id, from: :id + end + + subject = with_default.new + + expect(subject.id).to eq(0) + expect(subject.copy_of_id).to eq(0) + end + + it 'can be a required value' do + with_required = Class.new(Hashie::Trash) do + property :id + property :copy_of_id, from: :id, required: true, message: 'must be set' + end + + expect { with_required.new }.to raise_error(ArgumentError, "The property 'copy_of_id' must be set") + end + + it 'does not set properties that do not exist' do + from_non_property = Class.new(Hashie::Trash) do + property :copy_of_value, from: :value + end + + subject = from_non_property.new(value: 0) + + expect(subject).not_to respond_to(:value) + expect { subject[:value] }.to raise_error(NoMethodError, "The property 'value' is not defined for .") + expect(subject.to_h[:value]).to eq(nil) + expect(subject.copy_of_value).to eq(0) + end + end end |