gjaldon / ecto_enum

Ecto extension to support enums in models
MIT License
562 stars 131 forks source link

Auto define enum variants as functions #90

Closed zph closed 4 months ago

zph commented 5 years ago

Implements https://github.com/gjaldon/ecto_enum/issues/87

This patch allows using enum variants as you would use a constant in some languages or a full featured Enum in others.

When variants are defined on an enum, a matching function will be defined for reducing code re-definitions.

It allows for usage as:

defenum StatusEnum, ["registered", "active", "inactive", "archived"]

> StatusEnum.registered()
=> "registered"

Complex atom types are also coerced into idiomatic names:

defenum StatusEnum, [:"on-hold"]

> StatusEnum.on_hold()
=> :"on-hold"

This PR works for both arity 2 and 3 versions of defenum. I had tested it for different cases in PG but have not deeply assessed any changes needed for MySQL.

You're welcome to tweak the PR for small changes and I'm happy to adjust it myself if something more thorough is needed.

:D

zph commented 5 years ago

Note: this works for some cases but not inside matches and in function heads. To make these work cleanly it needs to be a defmacro and be inlined during compilation.

We can hold off on merging this until I can implement that component.

The conversion to Macros broke tests, revising :) and will comment when ready for review.

zph commented 5 years ago

This is ready for review and I'm using it currently as it is in my own projects :).

axelson commented 5 years ago

This would be really helpful to ensure that we're using the right values as constants in the rest of the code-base (i.e. when creating records). Can this be merged? Although it could use some docs.

sample-usr commented 4 years ago

Are there any plans to merge this PR?