Class: Dry::Schema::Result
- Inherits:
-
Object
- Object
- Dry::Schema::Result
- Includes:
- Monads::Result::Mixin, Extensions::Hints::ResultMethods
- Defined in:
- lib/dry/schema/result.rb,
lib/dry/schema/extensions/monads.rb
Overview
Monad extension for Result
Class Method Summary collapse
- .new {|result| ... } ⇒ Object private
Instance Method Summary collapse
-
#[](name) ⇒ Object
Read value from the output hash.
-
#add_error(node) ⇒ Object
private
Add a new error AST node.
-
#at(at_path, &block) ⇒ Result
private
Return a new result scoped to a specific path.
- #concat(other) ⇒ Object private
-
#deconstruct_keys(_) ⇒ Object
private
Pattern matching support.
-
#error?(spec) ⇒ Boolean
Check if there’s an error for the provided spec.
-
#errors(options = EMPTY_HASH) ⇒ MessageSet
Get human-readable error representation.
-
#failure? ⇒ Boolean
Check if the result is not successful.
-
#inspect ⇒ String
Return a string representation of the result.
-
#key?(name) ⇒ Boolean
Check if a given key is present in the output.
-
#message_set(options = EMPTY_HASH) ⇒ MessageSet
Return the message set.
- #new(output, **opts, &block) ⇒ Object private
-
#output ⇒ Hash
(also: #to_h)
private
Dump result to a hash returning processed and validated data.
- #path ⇒ Object private
- #replace(value) ⇒ Object private
-
#success? ⇒ Boolean
Check if the result is successful.
-
#to_monad ⇒ Dry::Monads::Success, Dry::Monads::Failure
Turn result into a monad.
- #update(hash) ⇒ Object private
Methods included from Extensions::Hints::ResultMethods
Class Method Details
.new {|result| ... } ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
33 34 35 36 37 |
# File 'lib/dry/schema/result.rb', line 33 def self.new(*, **) result = super yield(result) if block_given? result.freeze end |
Instance Method Details
#[](name) ⇒ Object
Read value from the output hash
110 111 112 |
# File 'lib/dry/schema/result.rb', line 110 def [](name) output[name] end |
#add_error(node) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Add a new error AST node
198 199 200 |
# File 'lib/dry/schema/result.rb', line 198 def add_error(node) result_ast << node end |
#at(at_path, &block) ⇒ Result
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return a new result scoped to a specific path
46 47 48 |
# File 'lib/dry/schema/result.rb', line 46 def at(at_path, &block) new(@output, path: Path.new([*path, *Path[at_path]]), &block) end |
#concat(other) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
98 99 100 101 |
# File 'lib/dry/schema/result.rb', line 98 def concat(other) result_ast.concat(other.map(&:to_ast)) self end |
#deconstruct_keys(_) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Pattern matching support
191 192 193 |
# File 'lib/dry/schema/result.rb', line 191 def deconstruct_keys(_) output end |
#error?(spec) ⇒ Boolean
Check if there’s an error for the provided spec
132 133 134 |
# File 'lib/dry/schema/result.rb', line 132 def error?(spec) .any? { |msg| Path[msg.path].include?(Path[spec]) } end |
#errors(options = EMPTY_HASH) ⇒ MessageSet
Get human-readable error representation
161 162 163 |
# File 'lib/dry/schema/result.rb', line 161 def errors( = EMPTY_HASH) () end |
#failure? ⇒ Boolean
Check if the result is not successful
150 151 152 |
# File 'lib/dry/schema/result.rb', line 150 def failure? !success? end |
#inspect ⇒ String
Return a string representation of the result
184 185 186 |
# File 'lib/dry/schema/result.rb', line 184 def inspect "#<#{self.class}#{to_h.inspect} errors=#{errors.to_h.inspect} path=#{path.keys.inspect}>" end |
#key?(name) ⇒ Boolean
Check if a given key is present in the output
121 122 123 |
# File 'lib/dry/schema/result.rb', line 121 def key?(name) output.key?(name) end |
#message_set(options = EMPTY_HASH) ⇒ MessageSet
Return the message set
175 176 177 |
# File 'lib/dry/schema/result.rb', line 175 def ( = EMPTY_HASH) .with().(result_ast) end |
#new(output, **opts, &block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
51 52 53 54 55 56 57 58 59 |
# File 'lib/dry/schema/result.rb', line 51 def new(output, **opts, &block) self.class.new( output, message_compiler: , result_ast: result_ast, **opts, &block ) end |
#output ⇒ Hash Also known as: to_h
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Dump result to a hash returning processed and validated data
77 78 79 |
# File 'lib/dry/schema/result.rb', line 77 def output path.equal?(Path::EMPTY) ? @output : @output.dig(*path) end |
#path ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
68 69 70 |
# File 'lib/dry/schema/result.rb', line 68 def path @path || Path::EMPTY end |
#replace(value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/dry/schema/result.rb', line 83 def replace(value) if value.is_a?(output.class) output.replace(value) elsif path.equal?(Path::EMPTY) @output = value else value_holder = path.keys.length > 1 ? @output.dig(*path.to_a[0..-2]) : @output value_holder[path.last] = value end self end |
#success? ⇒ Boolean
Check if the result is successful
141 142 143 |
# File 'lib/dry/schema/result.rb', line 141 def success? result_ast.empty? end |
#to_monad ⇒ Dry::Monads::Success, Dry::Monads::Failure
Turn result into a monad
This makes result objects work with dry-monads (or anything with a compatible interface)
23 24 25 26 27 28 29 |
# File 'lib/dry/schema/extensions/monads.rb', line 23 def to_monad if success? Success(self) else Failure(self) end end |
#update(hash) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
62 63 64 65 |
# File 'lib/dry/schema/result.rb', line 62 def update(hash) output.update(hash) self end |