Class: Dry::Types::Hash::Schema

Inherits:
Dry::Types::Hash show all
Defined in:
lib/dry/types/hash/schema.rb

Direct Known Subclasses

Safe, Strict, Symbolized

Instance Attribute Summary collapse

Attributes inherited from Definition

#options, #primitive

Attributes included from Options

#meta, #options

Instance Method Summary collapse

Methods inherited from Dry::Types::Hash

#schema, #strict, #symbolized

Methods inherited from Definition

[], #call, #failure, #name, #primitive?, #result, #success

Methods included from Builder

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

Methods included from Options

#with

Constructor Details

#initialize(primitive, options = {}) ⇒ Schema

Returns a new instance of Schema.



7
8
9
10
# File 'lib/dry/types/hash/schema.rb', line 7

def initialize(primitive, options = {})
  @member_types = options.fetch(:member_types)
  super
end

Instance Attribute Details

#member_typesObject (readonly)

Returns the value of attribute member_types.



5
6
7
# File 'lib/dry/types/hash/schema.rb', line 5

def member_types
  @member_types
end

Instance Method Details

#try(hash, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/dry/types/hash/schema.rb', line 12

def try(hash, &block)
  result = call(hash, :try)

  if result.values.all?(&:success?)
    success(result.each_with_object({}) { |(key, res), h| h[key] = res.input })
  else
    failure = failure(hash, result)
    block ? yield(failure) : failure
  end
end