Class: EnumType

Inherits:
ActiveModel::Type::Value
  • Object
show all
Defined in:
app/types/enum_type.rb

Instance Method Summary collapse

Constructor Details

#initialize(allowed_values) ⇒ EnumType

Returns a new instance of EnumType.



2
3
4
# File 'app/types/enum_type.rb', line 2

def initialize(allowed_values)
  @allowed_values = allowed_values
end

Instance Method Details

#cast(value) ⇒ Object

Raises:

  • (ArgumentError)


6
7
8
9
10
# File 'app/types/enum_type.rb', line 6

def cast(value)
  return nil if value.blank?
  raise ArgumentError, "Invalid value: #{value}" unless @allowed_values.include?(value)
  value
end

#deserialize(value) ⇒ Object



16
17
18
# File 'app/types/enum_type.rb', line 16

def deserialize(value)
  cast(value)
end

#serialize(value) ⇒ Object



12
13
14
# File 'app/types/enum_type.rb', line 12

def serialize(value)
  cast(value)
end