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

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

Overview

The built-in Hash type has constructors that you can use to define hashes with explicit schemas and coercible values using the built-in types.

Basic Schema evaluates default values for keys missing in input hash (see #resolve_missing_value)

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

Returns a new instance of Schema.

Parameters:

  • _primitive (Class)
  • options (Hash)

Options Hash (options):



19
20
21
22
# File 'lib/dry/types/hash/schema.rb', line 19

def initialize(_primitive, options)
  @member_types = options.fetch(:member_types)
  super
end

Instance Attribute Details

#member_typesHash{Symbol => Definition} (readonly)

Returns:



14
15
16
# File 'lib/dry/types/hash/schema.rb', line 14

def member_types
  @member_types
end

Instance Method Details

#call(hash) ⇒ Hash{Symbol => Object} Also known as: []

Parameters:

Returns:

  • (Hash{Symbol => Object})


26
27
28
# File 'lib/dry/types/hash/schema.rb', line 26

def call(hash)
  coerce(hash)
end

#coerce(hash) ⇒ Hash{Symbol => Object} (private)

Parameters:

Returns:

  • (Hash{Symbol => Object})


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

def coerce(hash)
  resolve(hash) do |type, key, value|
    begin
      type.call(value)
    rescue ConstraintError
      raise SchemaError.new(key, value)
    end
  end
end

#resolve(hash) ⇒ Hash{Symbol => Object} (private)

Parameters:

Returns:

  • (Hash{Symbol => Object})


84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dry/types/hash/schema.rb', line 84

def resolve(hash)
  result = {}
  member_types.each do |key, type|
    if hash.key?(key)
      result[key] = yield(type, key, hash[key])
    else
      resolve_missing_value(result, key, type)
    end
  end
  result
end

#resolve_missing_value(result, key, type) ⇒ Object (private)

Parameters:

Returns:

  • (Object)

See Also:



102
103
104
105
106
107
108
# File 'lib/dry/types/hash/schema.rb', line 102

def resolve_missing_value(result, key, type)
  if type.default?
    result[key] = type.evaluate
  else
    super
  end
end

#try(hash, &block) {|failure| ... } ⇒ Result

Parameters:

Yield Parameters:

  • failure (Failure)

Yield Returns:

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dry/types/hash/schema.rb', line 36

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

#try_coerce(hash) ⇒ Hash{Symbol => Object} (private)

Parameters:

Returns:

  • (Hash{Symbol => Object})


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

def try_coerce(hash)
  resolve(hash) do |type, key, value|
    yield(key, type.try(value))
  end
end