-
Notifications
You must be signed in to change notification settings - Fork 311
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
Add MethodOverridingInitializer extension #455
Conversation
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.
Some thoughts for discussion if need be.
# h = MyHash.new(zip: 'a-dee-doo-dah') | ||
# h.zip # => 'a-dee-doo-dah' | ||
# h.__zip # => [[['zip', 'a-dee-doo-dah']]] | ||
module MethodOverridingInitializer |
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 made this its own extension in case people want to restrict which methods can be overwritten. Using this extension only, they can pass in a hash of only the methods they want overwritten and prevent overwriting other methods.
end | ||
|
||
context 'when the original hash has double keys' do | ||
let(:original_hash) { { zip: 'a-dee-doo-dah', zip: 'a-dee-day' } } |
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 causes a warning to be shown when running the specs. If that's an issue it can easily be removed. I added this spec because the already_overridden?
isn't needed in this extension, so I was testing the case of duplicate keys.
Great idea! A couple things I would like to see:
One additional thing that I'm thinking about here is that this somewhat replicates a subset of the behavior of What are your thoughts about an approach like that? |
I like that idea. We could maybe make it an optional named argument. So, for a normal hash we would do something like Although |
Neat |
After implementing it both ways, I think it makes more sense to keep this under a |
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 is looking good. I'll do the version bumping in the release commit, so I'm going to pop that off this branch, take the necessary changes from it, and merge outside of Github. You'll still get credit!
Thanks for the contribution.
Add MethodOverridingInitializer extension
Merged via cabb3e7 |
## [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).
Describe the change
This PR adds a new extension - MethodOverridingInitializer. This extension allows existing hash methods to be overridden if a hash passed in to the constructor contains such keys. For example:
Issue
References #338
Design Decisions
I made this its own extension in case people want to restrict which methods can be overwritten. Using this extension only, they can pass in a hash of only the methods they want overwritten and prevent overwriting other methods.
Possible Alternatives
This could be included in the extension MethodAccessWithOverride instead of being it's own extension.