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
Feature: Mash::UnderscoreKeys Extensions #566
base: master
Are you sure you want to change the base?
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.
Couple of questions ... Should we also extend KeyConversion
? Find a more generic implementation that can camelize, underscore, etc., keys?
|
||
private | ||
|
||
def _underscore(string) |
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 seems quite a bit different from, say, https://github.com/rails/rails/blob/04972d9b9ef60796dc8f0917817b5392d61fcf09/activesupport/lib/active_support/inflector/methods.rb#L96, what are the differences?
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 guess I went pretty old school and grabbed the version from 3.1 haha
https://github.com/rails/rails/blob/4dacedf983257aef38a8ebedb2d9a9c8fead8238/activesupport/lib/active_support/inflector/methods.rb#L48
I can update it to reflect the new version, I think there are likely performance and edge case improvements.
The one difference to this method that I added was converting spaces to underscores as well.
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.
Are you sure we want a different "underscore" behavior than ActiveSupport? Why?
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.
Hmm, I was thinking this could be slightly different than Rails. The intent here is making method names compatible with the Ruby's method naming conversions. createdAt
-> created_at
But I think it could also go a step further, "created at"
also does not follow the method naming convention, as a space is an illegal character. So my thought was we could convert them to underscore as well.
Maybe this should be called Hashie::Extensions::Mash::MethodSafeKeys
to not cause confusion. What do you think?
But I'm also okay just keeping it exactly the same as rails. Either way works for me, just feel the former is more convenient
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.
What do you think about implementing a generic extension that lets me transform keys whichever way I want (TransformKeys
) as a custom behavior, and then deriving it into a bunch of specific behaviors such as a rails-compatible UnderscoreKeys
?
## [Unreleased][unreleased] ### Miscellaneous Generated by 🚫 Danger |
@dblock were you thinking of |
Yes. |
I think this change is good to merge as is. Other improvements like suggested above can be made later. Can you please fix CHANGELOG to make Danger happy? |
@@ -13,6 +13,7 @@ Any violations of this scheme are considered to be bugs. | |||
### Added | |||
|
|||
* Your contribution here. | |||
* [#566](https://github.com/hashie/hashie/pull/566): Added `Mash::UnderscoreKeys` extensions for conversion of all keys to underscore - [@arianf](https://github.com/arianf) |
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 just missing a period.
end | ||
|
||
mash = UnderscoreMash.new | ||
mash.updatedAt = 'Today' #=> 'Today' |
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.
Nitpick: Can we lowercase today
so that it matches the below tomorrow
pls.
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.
Looking closer I think ACRONYMS
is very controversial. It has nothing to do with underscoring keys, it's a whole other function. Seems like it doesn't belong here. And generally modifying a CONSTANT inside a module is probably not the right way to override anything.
Care to remove for now and let's discuss in a separate PR?
Any plans to finish this? :) |
me 2 |
This extension can be mixed into a Mash to change the default behavior of converting keys to be underscore. After mixing this extension into a Mash, the Mash will convert all string keys to underscore. It can be useful to use with external source hashes, which maybe contain hyphens or CamelCase.
The other benefit is hashes that have hyphens can be accessed with methods