Class: Dry::Schema::Result
- Inherits:
-
Object
- Object
- Dry::Schema::Result
- Extended by:
- Initializer
- Includes:
- Monads::Result::Mixin
- Defined in:
- lib/dry/schema/result.rb,
lib/dry/schema/extensions/monads.rb
Overview
Processing result
Class Method Summary collapse
Instance Method Summary collapse
-
#[](name) ⇒ Object
Read value from the output hash.
- #concat(other) ⇒ Object private
-
#error?(spec) ⇒ Boolean
Check if there’s an error for the provided spec.
-
#errors(options = EMPTY_HASH) ⇒ Hash<Symbol=>Array>
Get human-readable error representation.
-
#failure? ⇒ Boolean
Check if the result is not successful.
-
#hints(options = EMPTY_HASH) ⇒ Hash<Symbol=>Array>
Get hints exclusively without errors.
-
#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.
-
#messages(options = EMPTY_HASH) ⇒ Hash<Symbol=>Array>
Get all messages including hints.
- #replace(hash) ⇒ Object private
-
#success? ⇒ Boolean
Check if the result is successful.
- #to_monad(options = EMPTY_HASH) ⇒ Object (also: #to_result)
Class Method Details
.new(*args) {|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.
30 31 32 33 34 |
# File 'lib/dry/schema/result.rb', line 30 def self.new(*args) result = super yield(result) result.freeze end |
Instance Method Details
#[](name) ⇒ Object
Read value from the output hash
56 57 58 |
# File 'lib/dry/schema/result.rb', line 56 def [](name) output[name] 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.
43 44 45 46 47 |
# File 'lib/dry/schema/result.rb', line 43 def concat(other) results.concat(other) result_ast.concat(other.map(&:to_ast)) self end |
#error?(spec) ⇒ Boolean
Check if there’s an error for the provided spec
78 79 80 |
# File 'lib/dry/schema/result.rb', line 78 def error?(spec) .any? { |msg| Path[spec] == msg.path } end |
#errors(options = EMPTY_HASH) ⇒ Hash<Symbol=>Array>
Get human-readable error representation
107 108 109 |
# File 'lib/dry/schema/result.rb', line 107 def errors( = EMPTY_HASH) (.merge(hints: false)).dump end |
#failure? ⇒ Boolean
Check if the result is not successful
96 97 98 |
# File 'lib/dry/schema/result.rb', line 96 def failure? !success? end |
#hints(options = EMPTY_HASH) ⇒ Hash<Symbol=>Array>
Get hints exclusively without errors
129 130 131 |
# File 'lib/dry/schema/result.rb', line 129 def hints( = EMPTY_HASH) (.merge(failures: false)).dump end |
#inspect ⇒ String
Return a string representation of the result
152 153 154 |
# File 'lib/dry/schema/result.rb', line 152 def inspect "#<#{self.class}#{to_h.inspect} errors=#{errors.inspect}>" end |
#key?(name) ⇒ Boolean
Check if a given key is present in the output
67 68 69 |
# File 'lib/dry/schema/result.rb', line 67 def key?(name) output.key?(name) end |
#message_set(options = EMPTY_HASH) ⇒ MessageSet
Return the message set
143 144 145 |
# File 'lib/dry/schema/result.rb', line 143 def ( = EMPTY_HASH) .with().(result_ast) end |
#messages(options = EMPTY_HASH) ⇒ Hash<Symbol=>Array>
Get all messages including hints
118 119 120 |
# File 'lib/dry/schema/result.rb', line 118 def ( = EMPTY_HASH) (.merge(hints: true)).dump end |
#replace(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.
37 38 39 40 |
# File 'lib/dry/schema/result.rb', line 37 def replace(hash) @output = hash self end |
#success? ⇒ Boolean
Check if the result is successful
87 88 89 |
# File 'lib/dry/schema/result.rb', line 87 def success? results.empty? end |
#to_monad(options = EMPTY_HASH) ⇒ Object Also known as: to_result
8 9 10 11 12 13 14 |
# File 'lib/dry/schema/extensions/monads.rb', line 8 def to_monad( = EMPTY_HASH) if success? Success(output) else Failure(()) end end |