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
17
# 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

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



27
28
29
30
31
32
33
34
35
36
37
# 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) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
# 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

#nameObject



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

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

#referenced_model_classesObject



73
74
75
76
# 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) ⇒ Object



63
64
65
66
67
68
69
70
71
# 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_classesObject



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

def value_classes
  VALUE_CLASSES
end