class Avromatic::Model::Types::MapType

Constants

VALUE_CLASSES

Attributes

key_type[R]
value_type[R]

Public Class Methods

new(key_type:, value_type:) click to toggle source
Calls superclass method
# File lib/avromatic/model/types/map_type.rb, line 13
def initialize(key_type:, value_type:)
  super()
  @key_type = key_type
  @value_type = value_type
end

Public Instance Methods

coerce(input) click to toggle source
# File lib/avromatic/model/types/map_type.rb, line 27
def coerce(input)
  if input.nil?
    input
  elsif input.is_a?(::Hash)
    input.each_with_object({}) do |(key_input, value_input), result|
      result[key_type.coerce(key_input)] = value_type.coerce(value_input)
    end
  else
    raise ArgumentError.new("Could not coerce '#{input.inspect}' to #{name}")
  end
end
coerced?(value) click to toggle source
# File lib/avromatic/model/types/map_type.rb, line 51
def coerced?(value)
  if value.nil?
    true
  elsif value.is_a?(Hash)
    value.all? do |element_key, element_value|
      key_type.coerced?(element_key) && value_type.coerced?(element_value)
    end
  else
    false
  end
end
coercible?(input) click to toggle source
# File lib/avromatic/model/types/map_type.rb, line 39
def coercible?(input)
  if input.nil?
    true
  elsif input.is_a?(Hash)
    input.all? do |key_input, value_input|
      key_type.coercible?(key_input) && value_type.coercible?(value_input)
    end
  else
    false
  end
end
name() click to toggle source
# File lib/avromatic/model/types/map_type.rb, line 19
def name
  "map[#{key_type.name} => #{value_type.name}]"
end
referenced_model_classes() click to toggle source
# File lib/avromatic/model/types/map_type.rb, line 73
def referenced_model_classes
  # According to Avro's spec, keys can only be strings, so we can safely disregard #key_type here.
  value_type.referenced_model_classes
end
serialize(value, strict) click to toggle source
# File lib/avromatic/model/types/map_type.rb, line 63
def serialize(value, strict)
  if value.nil?
    value
  else
    value.each_with_object({}) do |(element_key, element_value), result|
      result[key_type.serialize(element_key, strict)] = value_type.serialize(element_value, strict)
    end
  end
end
value_classes() click to toggle source
# File lib/avromatic/model/types/map_type.rb, line 23
def value_classes
  VALUE_CLASSES
end