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.


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

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

Instance Attribute Details

#subtypeObject (readonly)

Returns the value of attribute subtype.


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

def subtype
  @subtype
end

Instance Method Details

#assert_valid_value(value) ⇒ Object


155
156
157
158
159
# File 'activerecord/lib/active_record/enum.rb', line 155

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


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

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


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

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

#serializable?(value, &block) ⇒ Boolean

Returns:

  • (Boolean)

151
152
153
# File 'activerecord/lib/active_record/enum.rb', line 151

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

#serialize(value) ⇒ Object


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

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