diff options
author | Jerry Cheung <jollyjerry@gmail.com> | 2013-02-11 16:42:25 -0800 |
---|---|---|
committer | Jerry Cheung <jollyjerry@gmail.com> | 2013-02-11 16:42:25 -0800 |
commit | 743b44c50a56e73040eec485b73f2368e64974a1 (patch) | |
tree | 6ee7bdc3c5398db66d30dcff6aa442f0afe2b3a8 | |
parent | 75ef232210e699d58299975422328374db2341f4 (diff) | |
parent | 86e7e9e07b5067ac8f37fc05b30d5f9d40f28d5b (diff) | |
download | hashie-743b44c50a56e73040eec485b73f2368e64974a1.tar.gz |
Merge pull request #39 from sleverbor/initialize_translations_first
initialize translations before other properties in trash
-rw-r--r-- | lib/hashie/dash.rb | 10 | ||||
-rw-r--r-- | lib/hashie/trash.rb | 13 | ||||
-rw-r--r-- | spec/hashie/trash_spec.rb | 6 |
3 files changed, 26 insertions, 3 deletions
diff --git a/lib/hashie/dash.rb b/lib/hashie/dash.rb index cbfc1f5..603ec5e 100644 --- a/lib/hashie/dash.rb +++ b/lib/hashie/dash.rb @@ -93,9 +93,7 @@ module Hashie self[prop] = value end - attributes.each_pair do |att, value| - self[att] = value - end if attributes + initialize_attributes(attributes) assert_required_properties_set! end @@ -122,6 +120,12 @@ module Hashie private + def initialize_attributes(attributes) + attributes.each_pair do |att, value| + self[att] = value + end if attributes + end + def assert_property_exists!(property) unless self.class.property?(property) raise NoMethodError, "The property '#{property}' is not defined for this Dash." diff --git a/lib/hashie/trash.rb b/lib/hashie/trash.rb index 39d6e8e..ca7be06 100644 --- a/lib/hashie/trash.rb +++ b/lib/hashie/trash.rb @@ -73,5 +73,18 @@ module Hashie end true end + + private + + def initialize_attributes(attributes) + return unless attributes + attributes_copy=attributes.dup.delete_if do |k,v| + if self.class.translations.include?(k.to_sym) + self[k]=v + true + end + end + super attributes_copy + end end end diff --git a/spec/hashie/trash_spec.rb b/spec/hashie/trash_spec.rb index 296b8ad..40d27c2 100644 --- a/spec/hashie/trash_spec.rb +++ b/spec/hashie/trash_spec.rb @@ -63,6 +63,12 @@ describe Hashie::Trash do TrashTest.new(:first_name => 'Michael').first_name.should == 'Michael' end + context "with both the translated property and the property" do + it 'sets the desired properties' do + TrashTest.new(:first_name => 'Michael', :firstName=>'Maeve').first_name.should == 'Michael' + end + end + it 'sets the translated properties' do TrashTest.new(:firstName => 'Michael').first_name.should == 'Michael' end |