ecto_enum icon indicating copy to clipboard operation
ecto_enum copied to clipboard

Auto define enum variants as functions

Open zph opened this issue 5 years ago • 4 comments

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 avatar Aug 29 '19 14:08 zph

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 avatar Aug 29 '19 15:08 zph

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

zph avatar Sep 12 '19 10:09 zph

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.

axelson avatar Nov 02 '19 02:11 axelson

Are there any plans to merge this PR?

sample-usr avatar May 17 '20 15:05 sample-usr