crystal icon indicating copy to clipboard operation
crystal copied to clipboard

Add alternative letter casings for `Enum` names

Open jgaskins opened this issue 1 year ago • 3 comments

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_json to use snakecase_name (or underscore_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_name because I'd forgotten about this
  • [ ] Add Flags support

jgaskins avatar Jan 12 '24 19:01 jgaskins

TIL about "kebabcase" :) I have heard "dash case", so perhaps that's clearer?

Fryguy avatar Jan 12 '24 20:01 Fryguy

Also, FWIW, ActiveSupport calls the "dash case" method dasherize

Fryguy avatar Jan 12 '24 20:01 Fryguy

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. ❤️

straight-shoota avatar Jan 12 '24 22:01 straight-shoota