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

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

Direct Known Subclasses

Permissive, Weak

Instance Attribute Summary collapse

Attributes inherited from Definition

#options, #primitive

Attributes included from Options

#options

Instance Method Summary collapse

Methods inherited from Dry::Types::Hash

#permissive, #schema, #strict, #strict_with_defaults, #symbolized, #weak

Methods inherited from Definition

[], #constrained?, #default?, #failure, #name, #primitive?, #result, #success

Methods included from Builder

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

Methods included from Options

#meta, #with

Constructor Details

#initialize(_primitive, options) ⇒ 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

#call(hash) ⇒ Object Also known as: []



12
13
14
# File 'lib/dry/types/hash/schema.rb', line 12

def call(hash)
  coerce(hash)
end

#try(hash, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dry/types/hash/schema.rb', line 17

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