Class: Dry::Types::Hash::Schema
- Inherits:
-
Dry::Types::Hash
- Object
- Definition
- Dry::Types::Hash
- Dry::Types::Hash::Schema
- Includes:
- MaybeTypes
- Defined in:
- lib/dry/types/hash/schema.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#member_types ⇒ Object
readonly
Returns the value of attribute member_types.
Attributes inherited from Definition
Attributes included from Options
Instance Method Summary collapse
- #call(hash) ⇒ Object (also: #[])
-
#initialize(_primitive, options) ⇒ Schema
constructor
A new instance of Schema.
- #try(hash, &block) ⇒ Object
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
Constructor Details
#initialize(_primitive, options) ⇒ Schema
7 8 9 10 |
# File 'lib/dry/types/hash/schema.rb', line 7 def initialize(_primitive, ) @member_types = .fetch(:member_types) super end |
Instance Attribute Details
#member_types ⇒ Object (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 |