Sha256: 1bde2450fd8d7fc1871bcd042b428b430017465118f6076ea7b823636f681092
Contents?: true
Size: 1.88 KB
Versions: 1
Compression:
Stored size: 1.88 KB
Contents
module Dry module Data class Type class Hash < Type def self.safe_constructor(hash_constructor, value_constructors, input) attributes = hash_constructor[input] value_constructors.each_with_object({}) do |(key, value_constructor), result| if attributes.key?(key) result[key] = value_constructor[attributes[key]] end end end def self.symbolized_constructor(hash_constructor, value_constructors, input) attributes = hash_constructor[input] value_constructors.each_with_object({}) do |(key, value_constructor), result| key_name = key.to_s if attributes.key?(key_name) result[key.to_sym] = value_constructor[attributes[key_name]] end end end def self.strict_constructor(hash_constructor, value_constructors, input) attributes = hash_constructor[input] value_constructors.each_with_object({}) do |(key, value_constructor), result| begin value = attributes.fetch(key) result[key] = value_constructor[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) value_constructors = type_map.each_with_object({}) { |(name, type_id), result| result[name] = Data[type_id] } self.class.new( self.class.method(meth).to_proc.curry.(constructor, value_constructors), primitive ) end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
dry-data-0.1.0 | lib/dry/data/type/hash.rb |