summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsazor <sazor.mail@gmail.com>2016-10-19 16:56:49 +0300
committersazor <sazor.mail@gmail.com>2016-10-31 09:56:09 +0300
commit453ae843d479432898db71864a6bb951c54f7715 (patch)
tree07af4c7ea814adc13f42cc12ea4029ad45e1340f
parent018551cfd090f370e0887db431fe8131ba96f6d4 (diff)
downloadhashie-453ae843d479432898db71864a6bb951c54f7715.tar.gz
Evaluate procs default values in object initialization
-rw-r--r--CHANGELOG.md1
-rw-r--r--UPGRADING.md12
-rw-r--r--lib/hashie/dash.rb6
-rw-r--r--spec/hashie/dash_spec.rb4
-rw-r--r--spec/spec_helper.rb1
5 files changed, 20 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 60cd69a..0983711 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -32,6 +32,7 @@ scheme are considered to be bugs.
* [#376](https://github.com/intridea/hashie/pull/376): Leave string index unchanged if it can't be converted to integer for Array#dig - [@sazor](https://github.com/sazor).
* [#377](https://github.com/intridea/hashie/pull/377): Dont use Rubygems to check ruby version - [@sazor](https://github.com/sazor).
* [#378](https://github.com/intridea/hashie/pull/378): Deep find all searches inside all nested hashes - [@sazor](https://github.com/sazor).
+* [#380](https://github.com/intridea/hashie/pull/380): Evaluate procs default values of Dash in object initialization - [@sazor](https://github.com/sazor).
### Security
diff --git a/UPGRADING.md b/UPGRADING.md
index 5645686..a6334a1 100644
--- a/UPGRADING.md
+++ b/UPGRADING.md
@@ -1,6 +1,18 @@
Upgrading Hashie
================
+### Upgrading to 3.4.7
+
+#### Procs as default values for Dash
+
+```ruby
+class MyHash < Hashie::Dash
+ property :time, default: -> { Time.now }
+end
+```
+
+In versions < 3.4.7 `Time.now` will be evaluated when `time` property is accessed directly first time.
+In version >= 3.4.7 `Time.now` is evaluated in time of object initialization.
### Upgrading to 3.4.4
#### Mash subclasses and reverse_merge
diff --git a/lib/hashie/dash.rb b/lib/hashie/dash.rb
index cc20a93..4ac7676 100644
--- a/lib/hashie/dash.rb
+++ b/lib/hashie/dash.rb
@@ -97,7 +97,11 @@ module Hashie
self.class.defaults.each_pair do |prop, value|
self[prop] = begin
val = value.dup
- val.is_a?(Proc) && val.arity > 0 ? val.call(self) : val
+ if val.is_a?(Proc)
+ val.arity == 1 ? val.call(self) : val.call
+ else
+ val
+ end
rescue TypeError
value
end
diff --git a/spec/hashie/dash_spec.rb b/spec/hashie/dash_spec.rb
index cc03128..c6e6753 100644
--- a/spec/hashie/dash_spec.rb
+++ b/spec/hashie/dash_spec.rb
@@ -56,9 +56,9 @@ class DeferredWithSelfTest < Hashie::Dash
end
describe DashTestDefaultProc do
- it "as_json behaves correctly with default proc" do
+ it 'as_json behaves correctly with default proc' do
object = described_class.new
- expect(object.as_json).to be == { "fields" => [] }
+ expect(object.as_json).to be == { 'fields' => [] }
end
end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 726568c..d7b297e 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -10,7 +10,6 @@ require 'hashie'
require 'rspec/pending_for'
require './spec/support/ruby_version_check'
-# NOTE: should this be here?
require 'active_support'
require 'active_support/core_ext'