Class: Thy::Types::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/thy/types/map.rb

Instance Method Summary collapse

Constructor Details

#initialize(key_type, value_type) ⇒ Map

Returns a new instance of Map.



6
7
8
9
# File 'lib/thy/types/map.rb', line 6

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

Instance Method Details

#check(value) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/thy/types/map.rb', line 11

def check(value)
  unless value.is_a?(::Hash)
    return Result::Failure.new("Expected a Hash, but got: #{value.inspect}")
  end

  value.keys.each do |k|
    if @key_type.check(k).failure?
      return Result::Failure.new(
        "Expected key #{k.inspect} to be of type #{@key_type.inspect}",
      )
    end
  end

  value.values.each do |v|
    if @value_type.check(v).failure?
      return Result::Failure.new(
        "Expected value #{v.inspect} to be of type #{@value_type.inspect}",
      )
    end
  end

  Result::Success
end