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.
72 73 74 75 |
# File 'lib/enumerize/activerecord.rb', line 72 def initialize(attr, subtype) @attr = attr @subtype = subtype end |
Instance Method Details
#as_json(options = nil) ⇒ Object
90 91 92 |
# File 'lib/enumerize/activerecord.rb', line 90 def as_json( = nil) {attr: @attr.name, subtype: @subtype}.as_json() end |
#deserialize(value) ⇒ Object Also known as: type_cast_from_database
84 85 86 |
# File 'lib/enumerize/activerecord.rb', line 84 def deserialize(value) @attr.find_value(value) end |
#encode_with(coder) ⇒ Object
94 95 96 97 98 |
# File 'lib/enumerize/activerecord.rb', line 94 def encode_with(coder) coder[:class_name] = @attr.klass.name coder[:attr_name] = @attr.name coder[:subtype] = @subtype end |
#init_with(coder) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/enumerize/activerecord.rb', line 100 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
77 78 79 80 |
# File 'lib/enumerize/activerecord.rb', line 77 def serialize(value) v = @attr.find_value(value) v && v.value end |