Class: ConfigurableEnum::EnumType
- Inherits:
-
ActiveRecord::Type::Value
- Object
- ActiveRecord::Type::Value
- ConfigurableEnum::EnumType
- Defined in:
- lib/configurable_enum/enum_type.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#col_name ⇒ Object
readonly
Returns the value of attribute col_name.
-
#mapping ⇒ Object
readonly
Returns the value of attribute mapping.
-
#subtype ⇒ Object
readonly
Returns the value of attribute subtype.
Instance Method Summary collapse
- #assert_valid_value(value) ⇒ Object
- #cast(value) ⇒ Object
- #deserialize(value) ⇒ Object
-
#initialize(col_name, kls, subtype) ⇒ EnumType
constructor
A new instance of EnumType.
- #serialize(value) ⇒ Object
Constructor Details
#initialize(col_name, kls, subtype) ⇒ EnumType
Returns a new instance of EnumType.
6 7 8 9 10 |
# File 'lib/configurable_enum/enum_type.rb', line 6 def initialize(col_name, kls, subtype) @col_name = col_name @kls = kls @subtype = subtype end |
Instance Attribute Details
#col_name ⇒ Object (readonly)
Returns the value of attribute col_name.
4 5 6 |
# File 'lib/configurable_enum/enum_type.rb', line 4 def col_name @col_name end |
#mapping ⇒ Object (readonly)
Returns the value of attribute mapping.
4 5 6 |
# File 'lib/configurable_enum/enum_type.rb', line 4 def mapping @mapping end |
#subtype ⇒ Object (readonly)
Returns the value of attribute subtype.
4 5 6 |
# File 'lib/configurable_enum/enum_type.rb', line 4 def subtype @subtype end |
Instance Method Details
#assert_valid_value(value) ⇒ Object
37 38 39 40 41 |
# File 'lib/configurable_enum/enum_type.rb', line 37 def assert_valid_value(value) unless value.blank? || mapping.has_key?(value) || mapping.has_value?(value) raise ArgumentError, "'#{value}' is not a valid #{col_name}" end end |
#cast(value) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/configurable_enum/enum_type.rb', line 16 def cast(value) return if value.blank? if mapping.has_key?(value) value.to_s elsif mapping.has_value?(value) mapping.key(value) else assert_valid_value(value) end end |
#deserialize(value) ⇒ Object
28 29 30 31 |
# File 'lib/configurable_enum/enum_type.rb', line 28 def deserialize(value) return if value.nil? mapping.key(subtype.deserialize(value)) end |
#serialize(value) ⇒ Object
33 34 35 |
# File 'lib/configurable_enum/enum_type.rb', line 33 def serialize(value) mapping.fetch(value, value) end |