Skip to content

Commit

Permalink
Fix hashie#69: regression, multiple property assignments in Trash.
Browse files Browse the repository at this point in the history
Heavily inspired from hashie@9a38985.
  • Loading branch information
dblock committed Aug 24, 2014
1 parent 32d5b04 commit 06fb570
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@
* [#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).
* [#209](http://github.com/intridea/hashie/pull/209): Added Hashie::Extensions::DeepFind - [@michaelherold](https://github.com/michaelherold).
* [#69](https://github.com/intridea/hashie/pull/69): Fixed assigning multiple properties in Hashie::Trash - [@michaelherold](https://github.com/michaelherold), [@dblock](https://github.com/dblock).
* Your contribution here.

## 3.2.0 (7/10/2014)
Expand Down
18 changes: 14 additions & 4 deletions lib/hashie/trash.rb
Expand Up @@ -26,11 +26,13 @@ def self.property(property_name, options = {})
fail ArgumentError, "Property name (#{property_name}) and :from option must not be the same"
end

translations[options[:from]] = property_name
translations[options[:from]] ||= {}
translations[options[:from]][property_name] = options[:with] || options[:transform_with]

define_method "#{options[:from]}=" do |val|
with = options[:with] || options[:transform_with]
self[property_name] = with.respond_to?(:call) ? with.call(val) : val
self.class.translations[options[:from]].each do |name, with|
self[name] = with.respond_to?(:call) ? with.call(val) : val
end
end
else
if options[:transform_with].respond_to? :call
Expand Down Expand Up @@ -82,7 +84,15 @@ def self.permitted_input_keys
private

def self.inverse_translations
@inverse_translations ||= Hash[translations.map(&:reverse)]
@inverse_translations ||= begin
h = {}
translations.each do |(property_name, property_translations)|
property_translations.keys.each do |k|
h[k] = property_name
end
end
h
end
end

# Raises an NoMethodError if the property doesn't exist
Expand Down
2 changes: 1 addition & 1 deletion spec/hashie/trash_spec.rb
Expand Up @@ -29,7 +29,7 @@ class TrashTest < Hashie::Trash
end

it 'maintains translations hash mapping from the original to the translated name' do
expect(TrashTest.translations[:firstName]).to eq :first_name
expect(TrashTest.translations[:firstName]).to eq(first_name: nil)
end

it 'maintains inverse translations hash mapping from the translated to the original name' do
Expand Down

0 comments on commit 06fb570

Please sign in to comment.