crystal
crystal copied to clipboard
Add alternative letter casings for `Enum` names
I need these in enums all the time. A lot of times, enum values are serialized in other systems (databases, third-party services, etc) differently, so I usually implement these methods myself in my apps. I wasn't sure how many other people were experiencing this, but then I saw this thread on the forum, so I'm clearly not alone in this.
In hindsight, snakecase_name should probably be underscore_name to match String#underscore. We don't seem to have any precedent for kebabcase in the stdlib, but it's pretty common to serialize low-cardinality multi-word string values this way.
This isn't complete yet, there are still a couple things that should be done, but I wanted to get something started since I'd already written the code for the forum post.
- [ ] Update
Enum#to_jsonto usesnakecase_name(orunderscore_name, whatever we call it) rather than its current bespoke implementation- This implementation is also more efficient than the current one since it avoids a heap allocation
- [ ] Use
member_namebecause I'd forgotten about this - [ ] Add
Flagssupport
TIL about "kebabcase" :) I have heard "dash case", so perhaps that's clearer?
Also, FWIW, ActiveSupport calls the "dash case" method dasherize
We should discuss this problem and figure out a generic solution. Could you please open an issue to explain the use cases. It may also contain conrete ideas, but it's better to start the discussion uncoupled from a specific implementation. We can leave this PR as a potential start for an implementation. A related discussion is #11660 Thank you. ❤️