Evaluate procs default values in object initialization

This commit is contained in:
sazor 2016-10-19 16:56:49 +03:00
parent 018551cfd0
commit 453ae843d4
5 changed files with 20 additions and 4 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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'