diff options
author | FoboCasteR <fobocaster@gmail.com> | 2014-08-11 16:09:29 -0400 |
---|---|---|
committer | dB <dblock@dblock.org> | 2014-08-11 16:09:29 -0400 |
commit | 7df81b6fdf87e9dd03da11a2213d943b091583f8 (patch) | |
tree | 79b7608f39dba5d70dba2c98ebf55a74a39641a7 | |
parent | 0c3dd5a8e7dc5ce3f9ea899c11f96b3193aa925d (diff) | |
download | hashie-7df81b6fdf87e9dd03da11a2213d943b091583f8.tar.gz |
Hashie::Trash transforms can be inherited.
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | lib/hashie/trash.rb | 18 | ||||
-rw-r--r-- | spec/hashie/trash_spec.rb | 22 |
3 files changed, 33 insertions, 8 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 4525ac1..1643152 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ * [#183](https://github.com/intridea/hashie/pull/183): Added Mash#load with YAML file support - [@gregory](https://github.com/gregory). * [#197](https://github.com/intridea/hashie/pull/197): Dont convert keys to string on initalization of mash - [@gregory](https://github.com/gregory). +* [#201](https://github.com/intridea/hashie/pull/201): Hashie::Trash transforms can be inherited - [@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 6179dc2..e0adb10 100644 --- a/lib/hashie/trash.rb +++ b/lib/hashie/trash.rb @@ -39,6 +39,16 @@ module Hashie end end + class << self + attr_reader :transforms + end + instance_variable_set('@transforms', {}) + + def self.inherited(klass) + super + klass.instance_variable_set('@transforms', transforms.dup) + end + # Set a value on the Dash in a Hash-like way. Only works # on pre-existing properties. def []=(property, value) @@ -69,10 +79,6 @@ module Hashie private - def self.properties - @properties ||= [] - end - def self.translations @translations ||= {} end @@ -81,10 +87,6 @@ module Hashie @inverse_translations ||= Hash[translations.map(&:reverse)] end - def self.transforms - @transforms ||= {} - end - # Raises an NoMethodError if the property doesn't exist # def property_exists?(property) diff --git a/spec/hashie/trash_spec.rb b/spec/hashie/trash_spec.rb index 63fd0a4..b570ef2 100644 --- a/spec/hashie/trash_spec.rb +++ b/spec/hashie/trash_spec.rb @@ -186,6 +186,28 @@ describe Hashie::Trash do end end + describe 'inheritable transforms' do + class TrashA < Hashie::Trash + property :some_value, transform_with: lambda { |v| v.to_i } + end + + class TrashB < TrashA + property :some_other_value, transform_with: lambda { |v| v.to_i } + end + + class TrashC < TrashB + 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) + end + + it 'replaces property transform' do + expect(TrashC.new(some_value: '123', some_other_value: '456').some_value).to eq(-123) + end + end + it 'raises an error when :from have the same value as property' do expect do class WrongTrash < Hashie::Trash |