summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/hashie/dash.rb25
-rw-r--r--lib/hashie/extensions/ignore_undeclared.rb9
2 files changed, 13 insertions, 21 deletions
diff --git a/lib/hashie/dash.rb b/lib/hashie/dash.rb
index 460790b..785d94d 100644
--- a/lib/hashie/dash.rb
+++ b/lib/hashie/dash.rb
@@ -104,19 +104,6 @@ module Hashie
def initialize(attributes = {}, &block)
super(&block)
- self.class.defaults.each_pair do |prop, value|
- self[prop] = begin
- val = value.dup
- if val.is_a?(Proc)
- val.arity == 1 ? val.call(self) : val.call
- else
- val
- end
- rescue TypeError
- value
- end
- end
-
initialize_attributes(attributes)
assert_required_attributes_set!
end
@@ -173,13 +160,19 @@ module Hashie
update_attributes(attributes)
self.class.defaults.each_pair do |prop, value|
- next unless self[prop].nil?
+ next unless fetch(prop, nil).nil?
self[prop] = begin
- value.dup
+ val = value.dup
+ if val.is_a?(Proc)
+ val.arity == 1 ? val.call(self) : val.call
+ else
+ val
+ end
rescue TypeError
value
end
end
+
assert_required_attributes_set!
end
@@ -189,7 +182,7 @@ module Hashie
return unless attributes
cleaned_attributes = attributes.reject { |_attr, value| value.nil? }
- update_attributes(cleaned_attributes)
+ update_attributes!(cleaned_attributes)
end
def update_attributes(attributes)
diff --git a/lib/hashie/extensions/ignore_undeclared.rb b/lib/hashie/extensions/ignore_undeclared.rb
index 9b506dd..64cf0b6 100644
--- a/lib/hashie/extensions/ignore_undeclared.rb
+++ b/lib/hashie/extensions/ignore_undeclared.rb
@@ -31,12 +31,11 @@ module Hashie
module IgnoreUndeclared
def initialize_attributes(attributes)
return unless attributes
+
klass = self.class
- translations = klass.respond_to?(:translations) && klass.translations
- attributes.each_pair do |att, value|
- next unless klass.property?(att) || (translations && translations.include?(att))
- self[att] = value
- end
+ translations = klass.respond_to?(:translations) && klass.translations || []
+
+ super(attributes.select { |attr, _| klass.property?(attr) || translations.include?(attr) })
end
def property_exists?(property)