Class: Arf::Types::MapType

Inherits:
BaseType show all
Defined in:
lib/arf/types/map_type.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseType

#bind, #coerce_value, #resolve_type

Constructor Details

#initialize(k, v) ⇒ MapType

Returns a new instance of MapType.



8
9
10
11
12
# File 'lib/arf/types/map_type.rb', line 8

def initialize(k, v)
  super()
  @key = k
  @value = v
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/arf/types/map_type.rb', line 6

def key
  @key
end

#valueObject (readonly)

Returns the value of attribute value.



6
7
8
# File 'lib/arf/types/map_type.rb', line 6

def value
  @value
end

Class Method Details

.[](k, v) ⇒ Object



14
# File 'lib/arf/types/map_type.rb', line 14

def self.[](k, v) = new(k, v)

Instance Method Details

#coerce(val) ⇒ Object



24
25
26
27
28
29
# File 'lib/arf/types/map_type.rb', line 24

def coerce(val)
  key_type, value_type = resolved_types
  val
    .transform_keys { coerce_value(_1, key_type) }
    .transform_values { coerce_value(_1, value_type) }
end

#resolved_typesObject



16
17
18
19
20
21
22
# File 'lib/arf/types/map_type.rb', line 16

def resolved_types
  return @resolved_types if @resolved_types

  key = resolve_type(@key)
  value = resolve_type(@value)
  @resolved_types = [key, value]
end