Class: Avromatic::Model::Types::EnumType

Inherits:
AbstractType show all
Defined in:
lib/avromatic/model/types/enum_type.rb

Constant Summary collapse

VALUE_CLASSES =
[::String].freeze
INPUT_CLASSES =
[::String, ::Symbol].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(allowed_values) ⇒ EnumType

Returns a new instance of EnumType.



14
15
16
# File 'lib/avromatic/model/types/enum_type.rb', line 14

def initialize(allowed_values)
  @allowed_values = allowed_values.to_set
end

Instance Attribute Details

#allowed_valuesObject (readonly)

Returns the value of attribute allowed_values.



12
13
14
# File 'lib/avromatic/model/types/enum_type.rb', line 12

def allowed_values
  @allowed_values
end

Instance Method Details

#coerce(input) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/avromatic/model/types/enum_type.rb', line 30

def coerce(input)
  if input.nil?
    input
  elsif coercible?(input)
    input.to_s
  else
    raise ArgumentError.new("Could not coerce '#{input.inspect}' to #{name}")
  end
end

#coerced?(input) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/avromatic/model/types/enum_type.rb', line 40

def coerced?(input)
  input.nil? || (input.is_a?(::String) && allowed_values.include?(input))
end

#coercible?(input) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/avromatic/model/types/enum_type.rb', line 44

def coercible?(input)
  input.nil? ||
    (input.is_a?(::String) && allowed_values.include?(input)) ||
    (input.is_a?(::Symbol) && allowed_values.include?(input.to_s))
end

#input_classesObject



26
27
28
# File 'lib/avromatic/model/types/enum_type.rb', line 26

def input_classes
  INPUT_CLASSES
end

#nameObject



18
19
20
# File 'lib/avromatic/model/types/enum_type.rb', line 18

def name
  "enum#{allowed_values.to_a}"
end

#referenced_model_classesObject



54
55
56
# File 'lib/avromatic/model/types/enum_type.rb', line 54

def referenced_model_classes
  EMPTY_ARRAY
end

#serialize(value) ⇒ Object



50
51
52
# File 'lib/avromatic/model/types/enum_type.rb', line 50

def serialize(value, **)
  value
end

#value_classesObject



22
23
24
# File 'lib/avromatic/model/types/enum_type.rb', line 22

def value_classes
  VALUE_CLASSES
end