Class: Avromatic::Model::Types::ArrayType

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

Constant Summary collapse

VALUE_CLASSES =
[::Array].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractType

#input_classes

Constructor Details

#initialize(value_type:) ⇒ ArrayType

Returns a new instance of ArrayType.



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

def initialize(value_type:)
  super()
  @value_type = value_type
end

Instance Attribute Details

#value_typeObject (readonly)

Returns the value of attribute value_type.



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

def value_type
  @value_type
end

Instance Method Details

#coerce(input) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/avromatic/model/types/array_type.rb', line 26

def coerce(input)
  if input.nil?
    input
  elsif input.is_a?(::Array)
    input.map { |element_input| value_type.coerce(element_input) }
  else
    raise ArgumentError.new("Could not coerce '#{input.inspect}' to #{name}")
  end
end

#coerced?(value) ⇒ Boolean

Returns:

  • (Boolean)


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

def coerced?(value)
  value.nil? || (value.is_a?(::Array) && value.all? { |element| value_type.coerced?(element) })
end

#coercible?(input) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/avromatic/model/types/array_type.rb', line 36

def coercible?(input)
  input.nil? || (input.is_a?(::Array) && input.all? { |element_input| value_type.coercible?(element_input) })
end

#nameObject



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

def name
  "array[#{value_type.name}]"
end

#referenced_model_classesObject



52
53
54
# File 'lib/avromatic/model/types/array_type.rb', line 52

def referenced_model_classes
  value_type.referenced_model_classes
end

#serialize(value, strict) ⇒ Object



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

def serialize(value, strict)
  if value.nil?
    value
  else
    value.map { |element| value_type.serialize(element, strict) }
  end
end

#value_classesObject



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

def value_classes
  VALUE_CLASSES
end