Class: Enumerize::ActiveRecordSupport::Type

Inherits:
ActiveRecord::Type::Value
  • Object
show all
Defined in:
lib/enumerize/activerecord.rb

Instance Method Summary collapse

Constructor Details

#initialize(attr, subtype) ⇒ Type

Returns a new instance of Type.


102
103
104
105
# File 'lib/enumerize/activerecord.rb', line 102

def initialize(attr, subtype)
  @attr = attr
  @subtype = subtype
end

Instance Method Details

#as_json(options = nil) ⇒ Object


120
121
122
# File 'lib/enumerize/activerecord.rb', line 120

def as_json(options = nil)
  {attr: @attr.name, subtype: @subtype}.as_json(options)
end

#deserialize(value) ⇒ Object Also known as: type_cast_from_database


114
115
116
# File 'lib/enumerize/activerecord.rb', line 114

def deserialize(value)
  @attr.find_value(value)
end

#encode_with(coder) ⇒ Object


124
125
126
127
128
# File 'lib/enumerize/activerecord.rb', line 124

def encode_with(coder)
  coder[:class_name] = @attr.klass.name
  coder[:attr_name] = @attr.name
  coder[:subtype] = @subtype
end

#init_with(coder) ⇒ Object


130
131
132
133
134
135
# File 'lib/enumerize/activerecord.rb', line 130

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


107
108
109
110
# File 'lib/enumerize/activerecord.rb', line 107

def serialize(value)
  v = @attr.find_value(value)
  (v && v.value) || value
end