Class: Candid::Internal::Types::Hash

Inherits:
Object
  • Object
show all
Includes:
Type
Defined in:
lib/candid/internal/types/hash.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Type

#strict!, #strict?

Methods included from JSON::Serializable

#dump, #load

Constructor Details

#initialize(key_type, value_type) ⇒ Hash

Returns a new instance of Hash.



17
18
19
20
# File 'lib/candid/internal/types/hash.rb', line 17

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.



9
10
11
# File 'lib/candid/internal/types/hash.rb', line 9

def key_type
  @key_type
end

#value_typeObject (readonly)

Returns the value of attribute value_type.



9
10
11
# File 'lib/candid/internal/types/hash.rb', line 9

def value_type
  @value_type
end

Class Method Details

.[](key_type, value_type) ⇒ Object



12
13
14
# File 'lib/candid/internal/types/hash.rb', line 12

def [](key_type, value_type)
  new(key_type, value_type)
end

Instance Method Details

#coerce(value, strict: strict?) ) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/candid/internal/types/hash.rb', line 22

def coerce(value, strict: strict?)
  unless value.is_a?(::Hash)
    raise Errors::TypeError, "not hash" if strict

    return value
  end

  value.to_h do |k, v|
    [Utils.coerce(key_type, k, strict: strict), Utils.coerce(value_type, v, strict: strict)]
  end
end