Class: StoreModel::Types::EnumType
- Inherits:
-
ActiveModel::Type::Value
- Object
- ActiveModel::Type::Value
- StoreModel::Types::EnumType
- Defined in:
- lib/store_model/types/enum_type.rb
Overview
Implements ActiveModel::Type::Value type for handling Rails-like enums
Instance Method Summary collapse
-
#cast_value(value) ⇒ Object
Casts
value
from DB or user to StoreModel::Model instance. -
#initialize(mapping, raise_on_invalid_values) ⇒ StoreModel::Types::EnumType
constructor
Initializes type for mapping.
-
#type ⇒ Symbol
Returns type.
Constructor Details
#initialize(mapping, raise_on_invalid_values) ⇒ StoreModel::Types::EnumType
Initializes type for mapping
14 15 16 17 |
# File 'lib/store_model/types/enum_type.rb', line 14 def initialize(mapping, raise_on_invalid_values) @mapping = mapping @raise_on_invalid_values = raise_on_invalid_values end |
Instance Method Details
#cast_value(value) ⇒ Object
Casts value
from DB or user to StoreModel::Model instance
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/store_model/types/enum_type.rb', line 31 def cast_value(value) return if value.blank? case value when String, Symbol then cast_symbol_value(value) when Integer then cast_integer_value(value) else raise StoreModel::Types::CastError, "failed casting #{value.inspect}, only String, Symbol or " \ "Integer instances are allowed" end end |
#type ⇒ Symbol
Returns type
22 23 24 |
# File 'lib/store_model/types/enum_type.rb', line 22 def type :integer end |