Class: ActiveRecord::Enum::EnumType

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(name, mapping, subtype) ⇒ EnumType

Returns a new instance of EnumType.


111
112
113
114
115
# File 'lib/active_record/enum.rb', line 111

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

Instance Method Details

#assert_valid_value(value) ⇒ Object


138
139
140
141
142
# File 'lib/active_record/enum.rb', line 138

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


117
118
119
120
121
122
123
124
125
126
127
# File 'lib/active_record/enum.rb', line 117

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


129
130
131
132
# File 'lib/active_record/enum.rb', line 129

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

#serialize(value) ⇒ Object


134
135
136
# File 'lib/active_record/enum.rb', line 134

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