Module: Kind::AssertHash::Schema

Extended by:
Schema
Included in:
Schema
Defined in:
lib/kind/__lib__/assert_hash.rb

Constant Summary collapse

KindAny =
->(value) { defined?(Kind::Any) ? Kind::Any === value : false }
KindObject =
->(value) { defined?(Kind::Object) ? Kind::Object === value : false }
KindUnionType =
->(value) { defined?(Kind::UnionType) ? Kind::UnionType === value : false }

Instance Method Summary collapse

Instance Method Details

#all(hash, spec) ⇒ Object



48
49
50
51
52
# File 'lib/kind/__lib__/assert_hash.rb', line 48

def all(hash, spec)
  Keys.require_all(spec.keys, hash)

  any(hash, spec)
end

#any(hash, spec) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kind/__lib__/assert_hash.rb', line 30

def any(hash, spec)
  spec.each do |key, expected|
    value = hash[key]
    error_message = "The key #{key.inspect} has an invalid value"

    case expected
    when KindAny, KindObject, KindUnionType then assert_kind_object(expected, value, error_message)
    when ::Module then assert_kind_of(expected, value, error_message)
    when ::Proc then assert(expected.call(value), error_message)
    when ::Regexp then assert_match(expected, value, error_message)
    when ::NilClass then assert_nil(value, error_message)
    else assert_equal(expected, value, error_message)
    end
  end

  hash
end