active_enum
active_enum copied to clipboard
Adding the ability to write enums as strings.
Firstly, thank you for writing this module. It has improved the performance of my employer's application greatly.
We generally expect that a setter can accept a value from its own getter. However, when using ActiveEnum.use_name_as_value = true, enums must be written as either symbols or integers. Consider the following situation:
person = Person.new(sex: :male)
puts person.sex # "Male"
person.sex = person.sex
puts person.sex # nil
This behavior is counter-intuitive: a leaky abstraction. Particularly, it causes issues saving form data. This commit makes an enum accept strings, symbols and integers, fixing the leaky abstraction.
Take care and thank you for reviewing this request.
In principal I can see this would make things more consistent. The problem is it interferes with normal typecasting of strings to integers which can be passed to a write method in various scenarios. The result could be a nil when assigning a '1' for example. Not what we want.