summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsleverbor <rob.revels@g5searchmarketing.com>2012-03-21 20:03:28 -0700
committersleverbor <rob.revels@g5searchmarketing.com>2012-03-21 20:03:28 -0700
commit86e7e9e07b5067ac8f37fc05b30d5f9d40f28d5b (patch)
treec18df1fbd0fb15450f09d963e69352ea4df2df3a
parentf7cb5e2b3c3ea3edd66ed2f9865eac7d7db80359 (diff)
downloadhashie-86e7e9e07b5067ac8f37fc05b30d5f9d40f28d5b.tar.gz
initialize translations before other properties in trash
-rw-r--r--lib/hashie/dash.rb10
-rw-r--r--lib/hashie/trash.rb13
-rw-r--r--spec/hashie/trash_spec.rb6
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 7babcca..1519287 100644
--- a/lib/hashie/trash.rb
+++ b/lib/hashie/trash.rb
@@ -51,5 +51,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 4c1e2a7..26802d0 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