summaryrefslogtreecommitdiff
path: root/lib/hashie/dash.rb
diff options
context:
space:
mode:
authorMichael Herold <opensource@michaeljherold.com>2020-10-23 21:57:10 -0500
committerMichael Herold <>2020-10-23 21:57:10 -0500
commitf152e25912d0b93ff08b8964eb757200ad4b6f32 (patch)
treed357a27b5b2b8408d87f6d400cb5fe8d52259d8c /lib/hashie/dash.rb
parent14e923d899183595abb5ee096c29e1de707fcc04 (diff)
downloadhashie-f152e25912d0b93ff08b8964eb757200ad4b6f32.tar.gz
Fix inconsistencies with Dash defaults
The normal behavior of Dash with respect to property defaults differed from the behavior of a Dash/Trash with IgnoreUndeclared mixed in. This is because some situations called the defaults and some did not. This change normalizes the behavior so that all situations where the defaults should be used to override unset values behave consistently, as well as all situations where the default should not override a `nil` value.
Diffstat (limited to 'lib/hashie/dash.rb')
-rw-r--r--lib/hashie/dash.rb25
1 files changed, 9 insertions, 16 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)