class Dry::Types::Hash::Schema
def call(hash)
-
(Hash{Symbol => Object})
-
Parameters:
-
hash
(Hash
) --
def call(hash) coerce(hash) end
def coerce(hash)
-
(Hash{Symbol => Object})
-
Parameters:
-
hash
(Hash
) --
def coerce(hash) resolve(hash) do |type, key, value| begin type.call(value) rescue ConstraintError raise SchemaError.new(key, value) end end end
def initialize(_primitive, options)
(**options)
-
:member_types
(Hash{Symbol => Definition}
) --
Parameters:
-
options
(Hash
) -- -
_primitive
(Class
) --
def initialize(_primitive, options) @member_types = options.fetch(:member_types) super end
def resolve(hash)
-
(Hash{Symbol => Object})
-
Parameters:
-
hash
(Hash
) --
def resolve(hash) result = {} member_types.each do |key, type| if hash.key?(key) result[key] = yield(type, key, hash[key]) else resolve_missing_value(result, key, type) end end result end
def resolve_missing_value(result, key, type)
- See: Dry::Types::Default::Callable#evaluate -
See: Dry::Types::Default#evaluate -
Returns:
-
(Object)
-
Parameters:
-
type
(Type
) -- -
key
(Symbol
) -- -
result
(Hash
) --
def resolve_missing_value(result, key, type) if type.default? result[key] = type.evaluate else super end end
def try(hash, &block)
-
(Object)
- if coercion fails and a block is given -
(Logic::Result)
-
Other tags:
- Yieldreturn: -
Other tags:
- Yieldparam: failure -
Parameters:
-
block
(#call, nil
) -- -
hash
(Hash
) --
def try(hash, &block) success = true output = {} begin result = try_coerce(hash) do |key, member_result| success &&= member_result.success? output[key] = member_result.input member_result end rescue ConstraintError, UnknownKeysError, SchemaError => e success = false result = e end if success success(output) else failure = failure(output, result) block ? yield(failure) : failure end end
def try_coerce(hash)
-
(Hash{Symbol => Object})
-
Parameters:
-
hash
(Hash
) --
def try_coerce(hash) resolve(hash) do |type, key, value| yield(key, type.try(value)) end end