Class: Typeguard::Validation::GenericHash

Inherits:
Base
  • Object
show all
Defined in:
lib/typeguard/types.rb

Instance Method Summary collapse

Methods inherited from Base

from

Constructor Details

#initialize(node) ⇒ GenericHash



82
83
84
85
86
# File 'lib/typeguard/types.rb', line 82

def initialize(node)
  @klass = node.[:const]
  @keys   = node.children.first.map { |k| Base.from(k) }
  @values = node.children.last.map { |v| Base.from(v) }
end

Instance Method Details

#valid?(value) ⇒ Boolean



88
89
90
91
92
93
94
95
96
# File 'lib/typeguard/types.rb', line 88

def valid?(value)
  return false unless value.is_a?(@klass)

  value.all? do |k, v|
    key_valid = @keys.any? { |child| child.valid?(k) }
    value_valid = @values.any? { |child| child.valid?(v) }
    key_valid && value_valid
  end
end