Class: ActiveRecord::Enum::EnumType

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, mapping, subtype) ⇒ EnumType

Returns a new instance of EnumType.



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

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

Instance Attribute Details

#subtypeObject (readonly)

Returns the value of attribute subtype



172
173
174
# File 'activerecord/lib/active_record/enum.rb', line 172

def subtype
  @subtype
end

Instance Method Details

#assert_valid_value(value) ⇒ Object



166
167
168
169
170
# File 'activerecord/lib/active_record/enum.rb', line 166

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



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

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

#deserialize(value) ⇒ Object



154
155
156
# File 'activerecord/lib/active_record/enum.rb', line 154

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

#serializable?(value, &block) ⇒ Boolean

Returns:

  • (Boolean)


162
163
164
# File 'activerecord/lib/active_record/enum.rb', line 162

def serializable?(value, &block)
  subtype.serializable?(mapping.fetch(value, value), &block)
end

#serialize(value) ⇒ Object



158
159
160
# File 'activerecord/lib/active_record/enum.rb', line 158

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