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.


108
109
110
111
112
# File 'activerecord/lib/active_record/enum.rb', line 108

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

Instance Method Details

#assert_valid_value(value) ⇒ Object


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

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


114
115
116
117
118
119
120
121
122
123
124
# File 'activerecord/lib/active_record/enum.rb', line 114

def cast(value)
  return if value.blank?

  if mapping.has_key?(value)
    value.to_s
  elsif mapping.has_value?(value)
    mapping.key(value)
  else
    assert_valid_value(value)
  end
end

#deserialize(value) ⇒ Object


126
127
128
129
# File 'activerecord/lib/active_record/enum.rb', line 126

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

#serialize(value) ⇒ Object


131
132
133
# File 'activerecord/lib/active_record/enum.rb', line 131

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