lib/kind/__lib__/strict.rb in kind-5.7.0 vs lib/kind/__lib__/strict.rb in kind-5.8.0

- old
+ new

@@ -4,20 +4,20 @@ module Kind module STRICT extend self - require 'kind/__lib__/assert_hash_schema' + require 'kind/__lib__/assert_hash' def error(kind_name, value, label = nil) # :nodoc: raise Error.new(kind_name, value, label: label) end - def object_is_a(kind, value, label = nil) # :nodoc: + def object_is_a(kind, value, label = nil, expected = nil) # :nodoc: return value if kind === value - error(kind.name, value, label) + error(expected || kind.name, value, label) end def kind_of(kind, value, kind_name = nil) # :nodoc: return value if kind === value @@ -51,35 +51,40 @@ raise Error.new("#{value} expected to be included in #{list.inspect}") end def assert_hash!(hash, options) + check_keys = options.key?(:keys) + check_schema = options.key?(:schema) + + raise ArgumentError, ':keys or :schema is missing' if !check_keys && !check_schema + raise ArgumentError, "hash can't be empty" if hash.empty? + require_all = options[:require_all] - return assert_hash_keys!(hash, options[:keys], require_all) if options.key?(:keys) - return assert_hash_schema!(hash, options[:schema], require_all) if options.key?(:schema) + return assert_hash_keys!(hash, options[:keys], require_all) if check_keys - raise ArgumentError, ':keys or :schema is missing' + assert_hash_schema!(hash, options[:schema], require_all) end private def assert_hash_keys!(hash, arg, require_all) keys = Array(arg) - ASSERT_HASH_KEYS.require_all(keys, hash) if require_all + AssertHash::Keys.require_all(keys, hash) if require_all hash.each_key do |k| unless keys.include?(k) raise ArgumentError.new("Unknown key: #{k.inspect}. Valid keys are: #{keys.map(&:inspect).join(', ')}") end end end def assert_hash_schema!(hash, schema, require_all) - return ASSERT_HASH_SCHEMA.all(hash, schema) if require_all + return AssertHash::Schema.all(hash, schema) if require_all - ASSERT_HASH_SCHEMA.any(hash, schema) + AssertHash::Schema.any(hash, schema) end end private_constant :STRICT end