-
Notifications
You must be signed in to change notification settings - Fork 313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow codependent Dash attributes to initialize #437
Conversation
lib/hashie/dash.rb
Outdated
def initialize_attributes(attributes, options = {}) | ||
return unless attributes | ||
|
||
set_nils = options.delete(:set_nils) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This modifies the calling hash, so probably not a great idea?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's an internal function so it's unused data, but I see your point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're purists here, never make assumptions about the caller, things change fast ;)
lib/hashie/dash.rb
Outdated
self[att] = value | ||
end if attributes | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should break up this function instead in two and kill the option, since it's used very differently in different places.
Definition: Codependent properties A set of two or more properties who have "required" validations that are based on each other. Example: ```ruby class OneOrMore < Hashie::Dash property :a, required: -> { b.nil? } property :b, required: -> { a.nil? } end ``` When constructing a Dash via the merge initializer, codependent properties have their "required" validation run too early when their values are set to `nil`, which causes an `ArgumentError` to be raised in the case that the first property is set to `nil`. This is an order-dependence bug that is fixed by this commit. By pruning off `nil` values only during initialization via the merge initializer, we can prevent this problem from occurring for the case of `nil` values. However, this is an indication of a larger problem with the architecture of Dash. We should be setting all the properties before running the validations. Rearchitecting this will be quite an undertaking.
76e81e0
to
0bd18ee
Compare
Refreshed! What do you think? |
👏 very very nice solution, naming things really clearly, love it |
## [3.6.0] - 2018-08-13 [3.6.0]: hashie/hashie@v3.5.7...v3.6.0 ### Added * [#455](hashie/hashie#455): Allow overriding methods when passing in a hash - [@lnestor](https://github.com/lnestor). ### Fixed * [#435](hashie/hashie#435): Mash `default_proc`s are now propagated down to nested sub-Hashes - [@michaelherold](https://github.com/michaelherold). * [#436](hashie/hashie#436): Ensure that `Hashie::Extensions::IndifferentAccess` injects itself after a non-destructive merge - [@michaelherold](https://github.com/michaelherold). * [#437](hashie/hashie#437): Allow codependent properties to be set on Dash - [@michaelherold](https://github.com/michaelherold). * [#438](hashie/hashie#438): Fix: `NameError (uninitialized constant Hashie::Extensions::Parsers::YamlErbParser::Pathname)` in `Hashie::Mash.load` - [@onk](https://github.com/onk). * [#457](hashie/hashie#457): Fix `Trash` to allow it to copy properties from other properties - [@michaelherold](https://github.com/michaelherold). ### Miscellaneous * [#433](hashie/hashie#433): Update Rubocop to the most recent version - [@michaelherold](https://github.com/michaelherold). * [#434](hashie/hashie#434): Add documentation around Mash sub-Hashes - [@michaelherold](https://github.com/michaelherold). * [#439](hashie/hashie#439): Add an integration spec for Elasticsearch - [@michaelherold](https://github.com/michaelherold).
Definition: Codependent properties
A set of two or more properties who have "required" validations that are based
on each other.
Example:
When constructing a Dash via the merge initializer, codependent properties have
their "required" validation run too early when their values are set to
nil
,which causes an
ArgumentError
to be raised in the case that the first propertyis set to
nil
.This is an order-dependence bug that is fixed by this commit. By pruning off
nil
values only during initialization via the merge initializer, we canprevent this problem from occurring for the case of
nil
values.However, this is an indication of a larger problem with the architecture of
Dash. We should be setting all the properties before running the validations.
Rearchitecting this will be quite an undertaking.
Fixes #431