Class: Dry::Types::Hash::Weak

Inherits:
Schema show all
Defined in:
lib/dry/types/hash/schema.rb

Direct Known Subclasses

Symbolized

Instance Attribute Summary

Attributes inherited from Schema

#member_types

Attributes inherited from Definition

#options, #primitive

Attributes included from Options

#meta, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Schema

#call, #initialize

Methods inherited from Dry::Types::Hash

#schema, #strict, #symbolized, #weak

Methods inherited from Definition

[], #call, #default?, #failure, #initialize, #maybe?, #name, #primitive?, #result, #success

Methods included from Builder

#constrained, #constrained_type, #constructor, #default, #enum, #maybe, #optional, #safe, #|

Methods included from Options

#initialize, #with

Constructor Details

This class inherits a constructor from Dry::Types::Hash::Schema

Class Method Details

.new(primitive, options = {}) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/dry/types/hash/schema.rb', line 63

def self.new(primitive, options = {})
  member_types = options.
    fetch(:member_types, {}).
    each_with_object({}) { |(k, t), res| res[k] = t.safe }

  super(primitive, options.merge(member_types: member_types))
end

Instance Method Details

#try(hash, &block) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/dry/types/hash/schema.rb', line 71

def try(hash, &block)
  if hash.is_a?(::Hash)
    super
  else
    result = failure(hash, "#{hash} must be a hash")
    block ? yield(result) : result
  end
end