Class: Enumerize::ActiveRecordSupport::Type
- Inherits:
-
ActiveRecord::Type::Value
- Object
- ActiveRecord::Type::Value
- Enumerize::ActiveRecordSupport::Type
- Defined in:
- lib/enumerize/activerecord.rb
Instance Method Summary collapse
- #as_json(options = nil) ⇒ Object
- #deserialize(value) ⇒ Object (also: #type_cast_from_database)
- #encode_with(coder) ⇒ Object
- #init_with(coder) ⇒ Object
-
#initialize(attr, subtype) ⇒ Type
constructor
A new instance of Type.
- #serialize(value) ⇒ Object (also: #type_cast_for_database)
Constructor Details
#initialize(attr, subtype) ⇒ Type
Returns a new instance of Type.
106 107 108 109 |
# File 'lib/enumerize/activerecord.rb', line 106 def initialize(attr, subtype) @attr = attr @subtype = subtype end |
Instance Method Details
#as_json(options = nil) ⇒ Object
124 125 126 |
# File 'lib/enumerize/activerecord.rb', line 124 def as_json( = nil) {attr: @attr.name}.as_json() end |
#deserialize(value) ⇒ Object Also known as: type_cast_from_database
118 119 120 |
# File 'lib/enumerize/activerecord.rb', line 118 def deserialize(value) @attr.find_value(value) end |
#encode_with(coder) ⇒ Object
128 129 130 131 132 |
# File 'lib/enumerize/activerecord.rb', line 128 def encode_with(coder) coder[:class_name] = @attr.klass.name coder[:attr_name] = @attr.name coder[:subtype] = @subtype end |
#init_with(coder) ⇒ Object
134 135 136 137 138 139 |
# File 'lib/enumerize/activerecord.rb', line 134 def init_with(coder) initialize( coder[:class_name].constantize.enumerized_attributes[coder[:attr_name]], coder[:subtype] ) end |
#serialize(value) ⇒ Object Also known as: type_cast_for_database
111 112 113 114 |
# File 'lib/enumerize/activerecord.rb', line 111 def serialize(value) v = @attr.find_value(value) (v && v.value) || value end |