Class: Decay::ActiveEnumAttribute
- Inherits:
-
ActiveModel::Type::Value
- Object
- ActiveModel::Type::Value
- Decay::ActiveEnumAttribute
- Defined in:
- lib/decay/active_enum_attribute.rb
Instance Method Summary collapse
-
#cast(value) ⇒ Object
User input.
-
#deserialize(value) ⇒ Object
Database => Ruby.
-
#initialize(enum:) ⇒ ActiveEnumAttribute
constructor
A new instance of ActiveEnumAttribute.
-
#serialize(value) ⇒ Object
Ruby => Database.
Constructor Details
#initialize(enum:) ⇒ ActiveEnumAttribute
Returns a new instance of ActiveEnumAttribute.
3 4 5 |
# File 'lib/decay/active_enum_attribute.rb', line 3 def initialize(enum:) @enum = enum end |
Instance Method Details
#cast(value) ⇒ Object
User input
8 9 10 11 12 13 14 15 16 |
# File 'lib/decay/active_enum_attribute.rb', line 8 def cast(value) if value.is_a?(Decay::EnumeratedType) value elsif value.respond_to?(:to_sym) @enum[value.to_sym] else @enum[value] end end |
#deserialize(value) ⇒ Object
Database => Ruby
28 29 30 |
# File 'lib/decay/active_enum_attribute.rb', line 28 def deserialize(value) @enum[@enum.key_for(value)] end |
#serialize(value) ⇒ Object
Ruby => Database
19 20 21 22 23 24 25 |
# File 'lib/decay/active_enum_attribute.rb', line 19 def serialize(value) if value.respond_to?(:value) value.value else value end end |