Class: ActiveRecord::Enum::EnumType

Inherits:
Type::Value
  • Object
show all
Defined in:
activerecord/lib/active_record/enum.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(name, mapping, subtype) ⇒ EnumType

Returns a new instance of EnumType.


119
120
121
122
123
# File 'activerecord/lib/active_record/enum.rb', line 119

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

Instance Method Details

#assert_valid_value(value) ⇒ Object


146
147
148
149
150
# File 'activerecord/lib/active_record/enum.rb', line 146

def assert_valid_value(value)
  unless value.blank? || mapping.has_key?(value) || mapping.has_value?(value)
    raise ArgumentError, "'#{value}' is not a valid #{name}"
  end
end

#cast(value) ⇒ Object


125
126
127
128
129
130
131
132
133
134
135
# File 'activerecord/lib/active_record/enum.rb', line 125

def cast(value)
  if mapping.has_key?(value)
    value.to_s
  elsif mapping.has_value?(value)
    mapping.key(value)
  elsif value.blank?
    nil
  else
    assert_valid_value(value)
  end
end

#deserialize(value) ⇒ Object


137
138
139
140
# File 'activerecord/lib/active_record/enum.rb', line 137

def deserialize(value)
  return if value.nil?
  mapping.key(subtype.deserialize(value))
end

#serialize(value) ⇒ Object


142
143
144
# File 'activerecord/lib/active_record/enum.rb', line 142

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