diff options
author | Michael Herold <opensource@michaeljherold.com> | 2020-10-23 21:57:10 -0500 |
---|---|---|
committer | Michael Herold <> | 2020-10-23 21:57:10 -0500 |
commit | f152e25912d0b93ff08b8964eb757200ad4b6f32 (patch) | |
tree | d357a27b5b2b8408d87f6d400cb5fe8d52259d8c /README.md | |
parent | 14e923d899183595abb5ee096c29e1de707fcc04 (diff) | |
download | hashie-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 'README.md')
-rw-r--r-- | README.md | 21 |
1 files changed, 21 insertions, 0 deletions
@@ -875,6 +875,27 @@ p = Tricky.new('trick' => 'two') p.trick # => NoMethodError ``` +If you would like to update a Dash and use any default values set in the case of a `nil` value, use `#update_attributes!`. + +```ruby +class WithDefaults < Hashie::Dash + property :description, default: 'none' +end + +dash = WithDefaults.new +dash.description #=> 'none' + +dash.description = 'You committed one of the classic blunders!' +dash.description #=> 'You committed one of the classic blunders!' + +dash.description = nil +dash.description #=> nil + +dash.description = 'Only slightly less known is ...' +dash.update_attributes!(description: nil) +dash.description #=> 'none' +``` + ### Potential Gotchas Because Dashes are subclasses of the built-in Ruby Hash class, the double-splat operator takes the Dash as-is without any conversion. This can lead to strange behavior when you use the double-splat operator on a Dash as the first part of a keyword list or built Hash. For example: |