Skip to content
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 alternative letter casings for Enum names #14226

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jgaskins
Copy link
Contributor

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

@Fryguy
Copy link
Contributor

Fryguy commented Jan 12, 2024

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

@Fryguy
Copy link
Contributor

Fryguy commented Jan 12, 2024

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

@straight-shoota straight-shoota marked this pull request as draft January 12, 2024 22:08
@straight-shoota
Copy link
Member

straight-shoota commented Jan 12, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants