Class: Avromatic::Model::Types::MapType

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

Constant Summary collapse

VALUE_CLASSES =
[::Hash].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractType

#input_classes

Constructor Details

#initialize(key_type:, value_type:) ⇒ MapType

Returns a new instance of MapType.



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

def initialize(key_type:, value_type:)
  @key_type = key_type
  @value_type = value_type
end

Instance Attribute Details

#key_typeObject (readonly)

Returns the value of attribute key_type.



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

def key_type
  @key_type
end

#value_typeObject (readonly)

Returns the value of attribute value_type.



11
12
13
# File 'lib/avromatic/model/types/map_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
35
36
# File 'lib/avromatic/model/types/map_type.rb', line 26

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) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/avromatic/model/types/map_type.rb', line 50

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) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/avromatic/model/types/map_type.rb', line 38

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

#nameObject



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

def name
  "map[#{key_type.name} => #{value_type.name}]"
end

#referenced_model_classesObject



72
73
74
75
# File 'lib/avromatic/model/types/map_type.rb', line 72

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:) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/avromatic/model/types/map_type.rb', line 62

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: strict)] = value_type.serialize(element_value, strict: strict)
    end
  end
end

#value_classesObject



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

def value_classes
  VALUE_CLASSES
end