Sha256: ec94c65a00c663bec51054a65d6894f63892de87d428c129550c761c6fdca608
Contents?: true
Size: 1.83 KB
Versions: 1
Compression:
Stored size: 1.83 KB
Contents
module Dry module Types class Definition class Hash < Definition def self.safe_constructor(types, hash) types.each_with_object({}) do |(key, type), result| if hash.key?(key) result[key] = type[hash[key]] elsif type.is_a?(Default) result[key] = type.evaluate end end end def self.symbolized_constructor(types, hash) types.each_with_object({}) do |(key, type), result| if hash.key?(key) result[key] = type[hash[key]] else key_name = key.to_s if hash.key?(key_name) result[key] = type[hash[key_name]] elsif type.is_a?(Default) result[key] = type.evaluate end end end end def self.strict_constructor(types, hash) types.each_with_object({}) do |(key, type), result| begin value = hash.fetch(key) result[key] = type[value] rescue TypeError raise SchemaError.new(key, value) rescue KeyError raise SchemaKeyError.new(key) end end end def strict(type_map) schema(type_map, :strict_constructor) end def symbolized(type_map) schema(type_map, :symbolized_constructor) end def schema(type_map, meth = :safe_constructor) types = type_map.each_with_object({}) { |(name, type), result| result[name] = case type when String, Class then Types[type] else type end } fn = self.class.method(meth).to_proc.curry.(types) constructor(fn, schema: types) end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
dry-types-0.6.0 | lib/dry/types/definition/hash.rb |