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

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

Overview

Weak schema provides safe types for every type given in schema hash

See Also:

Direct Known Subclasses

Symbolized

Instance Attribute Summary

Attributes inherited from Schema

#member_types

Attributes inherited from Definition

#options, #primitive

Attributes included from Options

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Schema

#call, #coerce, #initialize, #resolve, #resolve_missing_value, #to_ast, #try_coerce

Methods included from MaybeTypes

#resolve_missing_value

Methods inherited from Dry::Types::Hash

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

Methods inherited from Definition

[], #call, #constrained?, #default?, #failure, #initialize, #name, #optional?, #primitive?, #result, #success, #to_ast

Methods included from Builder

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

Methods included from Options

#initialize, #meta, #pristine, #with

Constructor Details

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

Class Method Details

.new(primitive, options) ⇒ Object

Parameters:

  • primitive (Class)
  • options (Hash)

See Also:



211
212
213
214
215
216
217
# File 'lib/dry/types/hash/schema.rb', line 211

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

#hash_typeObject (private)



236
237
238
# File 'lib/dry/types/hash/schema.rb', line 236

def hash_type
  :weak
end

#try(value, &block) {|failure| ... } ⇒ Object, ...

Parameters:

  • value (Object)
  • block (#call, nil)

Yield Parameters:

  • failure (Failure)

Yield Returns:

Returns:

  • (Object)

    if block given

  • (Result, Logic::Result)

    otherwise



225
226
227
228
229
230
231
232
# File 'lib/dry/types/hash/schema.rb', line 225

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