Class: EnumType
- Inherits:
-
ActiveModel::Type::Value
- Object
- ActiveModel::Type::Value
- EnumType
- Defined in:
- app/types/enum_type.rb
Instance Method Summary collapse
- #cast(value) ⇒ Object
- #deserialize(value) ⇒ Object
-
#initialize(allowed_values) ⇒ EnumType
constructor
A new instance of EnumType.
- #serialize(value) ⇒ Object
Constructor Details
#initialize(allowed_values) ⇒ EnumType
Returns a new instance of EnumType.
2 3 4 |
# File 'app/types/enum_type.rb', line 2 def initialize(allowed_values) @allowed_values = allowed_values end |
Instance Method Details
#cast(value) ⇒ Object
6 7 8 9 10 |
# File 'app/types/enum_type.rb', line 6 def cast(value) return nil if value.blank? raise ArgumentError, "Invalid value: #{value}" unless @allowed_values.include?(value) value end |
#deserialize(value) ⇒ Object
16 17 18 |
# File 'app/types/enum_type.rb', line 16 def deserialize(value) cast(value) end |
#serialize(value) ⇒ Object
12 13 14 |
# File 'app/types/enum_type.rb', line 12 def serialize(value) cast(value) end |