Class: EnumIsh::ActiveRecordEnumType

Inherits:
ActiveRecord::Type::Value
  • Object
show all
Defined in:
lib/enum_ish/active_record_enum_type.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, mapping, subtype) ⇒ ActiveRecordEnumType

Returns a new instance of ActiveRecordEnumType.



5
6
7
8
9
# File 'lib/enum_ish/active_record_enum_type.rb', line 5

def initialize(name, mapping, subtype)
  @name = name
  @mapping = mapping
  @subtype = subtype
end

Instance Method Details

#cast(value) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/enum_ish/active_record_enum_type.rb', line 11

def cast(value)
  return if value.nil?
  if @mapping.has_key?(value.to_s.to_sym)
    value.to_s.to_sym
  elsif @mapping.has_value?(value)
    @mapping.key(value)
  else
    value
  end
end

#deserialize(value) ⇒ Object



22
23
24
# File 'lib/enum_ish/active_record_enum_type.rb', line 22

def deserialize(value)
  @mapping.key(@subtype.deserialize(value))
end

#serialize(value) ⇒ Object



26
27
28
# File 'lib/enum_ish/active_record_enum_type.rb', line 26

def serialize(value)
  @mapping.fetch(value, value)
end