diff options
author | Daniel Doubrovkine (dB.) @dblockdotorg <dblock@dblock.org> | 2016-10-31 10:31:02 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-31 10:31:02 +0100 |
commit | aa4f809c03c0be694bb31b7e85e29cc74e1ee83a (patch) | |
tree | 07af4c7ea814adc13f42cc12ea4029ad45e1340f | |
parent | 9c38812a3e54e1313e826d945e03a311367ddb1e (diff) | |
parent | 453ae843d479432898db71864a6bb951c54f7715 (diff) | |
download | hashie-aa4f809c03c0be694bb31b7e85e29cc74e1ee83a.tar.gz |
Merge pull request #380 from sazor/dash-default-and-as_json
Dash default and as json
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | UPGRADING.md | 12 | ||||
-rw-r--r-- | lib/hashie/dash.rb | 6 | ||||
-rw-r--r-- | spec/hashie/dash_spec.rb | 11 | ||||
-rw-r--r-- | spec/spec_helper.rb | 3 |
5 files changed, 32 insertions, 1 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 453d62f..c6e6753 100644 --- a/spec/hashie/dash_spec.rb +++ b/spec/hashie/dash_spec.rb @@ -12,6 +12,10 @@ class DashTest < Hashie::Dash property :count, default: 0 end +class DashTestDefaultProc < Hashie::Dash + property :fields, default: -> { [] } +end + class DashNoRequiredTest < Hashie::Dash property :first_name property :email @@ -51,6 +55,13 @@ class DeferredWithSelfTest < Hashie::Dash property :updated_at, default: ->(test) { test.created_at } end +describe DashTestDefaultProc do + it 'as_json behaves correctly with default proc' do + object = described_class.new + expect(object.as_json).to be == { 'fields' => [] } + end +end + describe DashTest do def property_required_error(property) [ArgumentError, "The property '#{property}' is required for #{subject.class.name}."] diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2cd68e3..d7b297e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,6 +10,9 @@ require 'hashie' require 'rspec/pending_for' require './spec/support/ruby_version_check' +require 'active_support' +require 'active_support/core_ext' + RSpec.configure do |config| config.extend RubyVersionCheck config.expect_with :rspec do |expect| |