AaronLasseigne/active_interaction

Enum filter

casiodk opened this issue · 2 comments

I think something like dry types enums would be a great addition to the library!!

class Book < ActiveInteraction::Base
  enum :state, values: ["open", "closed"]
end

I'm not sure how I feel about adding enum. The other filters cast/convert incoming values to a particular type. With enum I guess we'd have to force the type to String. Alternately, it could avoid casting and only check exact values but I feel like that'd be a problem. People expect the casting of inputs.

It's longer but you can do this now with a validation:

class Book < ActiveInteraction::Base
  string :state

  validates :string, inclusion: { in: %w[open closed] }
end

what about

class Book < ActiveInteraction::Base
  enum :state, values: ["open", "closed"] do
    string
  end
end

Similar to arrays ?