Class: Avromatic::Model::Types::FixedType

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

Constant Summary collapse

VALUE_CLASSES =
[::String].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractType

#input_classes

Constructor Details

#initialize(size) ⇒ FixedType

Returns a new instance of FixedType.



13
14
15
# File 'lib/avromatic/model/types/fixed_type.rb', line 13

def initialize(size)
  @size = size
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



11
12
13
# File 'lib/avromatic/model/types/fixed_type.rb', line 11

def size
  @size
end

Instance Method Details

#coerce(input) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/avromatic/model/types/fixed_type.rb', line 25

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

#coercible?(input) ⇒ Boolean Also known as: coerced?

Returns:

  • (Boolean)


33
34
35
# File 'lib/avromatic/model/types/fixed_type.rb', line 33

def coercible?(input)
  input.nil? || (input.is_a?(::String) && input.length == size)
end

#nameObject



17
18
19
# File 'lib/avromatic/model/types/fixed_type.rb', line 17

def name
  "fixed(#{size})"
end

#referenced_model_classesObject



43
44
45
# File 'lib/avromatic/model/types/fixed_type.rb', line 43

def referenced_model_classes
  EMPTY_ARRAY
end

#serialize(value) ⇒ Object



39
40
41
# File 'lib/avromatic/model/types/fixed_type.rb', line 39

def serialize(value, **)
  value
end

#value_classesObject



21
22
23
# File 'lib/avromatic/model/types/fixed_type.rb', line 21

def value_classes
  VALUE_CLASSES
end