123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::Enum::EnumType

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Type::Value
Instance Chain:
self, Type::Value
Inherits: Type::Value
  • ::Object
Defined in: activerecord/lib/active_record/enum.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(name, mapping, subtype, raise_on_invalid_values: true) ⇒ EnumType

[ GitHub ]

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

def initialize(name, mapping, subtype, raise_on_invalid_values: true)
  @name = name
  @mapping = mapping
  @subtype = subtype
  @_raise_on_invalid_values = raise_on_invalid_values
end

Instance Attribute Details

#mapping (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/enum.rb', line 213

attr_reader :name, :mapping

#name (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/enum.rb', line 213

attr_reader :name, :mapping

#subtype (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/enum.rb', line 210

attr_reader :subtype

#type (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/enum.rb', line 171

delegate :type, to: :subtype

Instance Method Details

#assert_valid_value(value)

[ GitHub ]

  
# File 'activerecord/lib/active_record/enum.rb', line 202

def assert_valid_value(value)
  return unless @_raise_on_invalid_values

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

#cast(value)

[ GitHub ]

  
# File 'activerecord/lib/active_record/enum.rb', line 180

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)

[ GitHub ]

  
# File 'activerecord/lib/active_record/enum.rb', line 190

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

#serializable?(value, &block) ⇒ Boolean

[ GitHub ]

  
# File 'activerecord/lib/active_record/enum.rb', line 198

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

#serialize(value)

[ GitHub ]

  
# File 'activerecord/lib/active_record/enum.rb', line 194

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