Class: Dry::Schema::Result
- Inherits:
-
Object
- Object
- Dry::Schema::Result
- Extended by:
- Initializer
- 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
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) ⇒ 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.
- #replace(hash) ⇒ Object private
-
#success? ⇒ Boolean
Check if the result is successful.
-
#to_monad ⇒ Dry::Monads::Success, Dry::Monads::Failure
Turn result into a monad.
Methods included from Extensions::Hints::ResultMethods
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.
35 36 37 38 39 |
# File 'lib/dry/schema/result.rb', line 35 def self.new(*args) result = super yield(result) result.freeze end |
Instance Method Details
#[](name) ⇒ Object
Read value from the output hash
61 62 63 |
# File 'lib/dry/schema/result.rb', line 61 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.
48 49 50 51 52 |
# File 'lib/dry/schema/result.rb', line 48 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
83 84 85 |
# File 'lib/dry/schema/result.rb', line 83 def error?(spec) .any? { |msg| Path[msg.path].include?(Path[spec]) } end |
#errors(options = EMPTY_HASH) ⇒ MessageSet
Get human-readable error representation
112 113 114 |
# File 'lib/dry/schema/result.rb', line 112 def errors( = EMPTY_HASH) () end |
#failure? ⇒ Boolean
Check if the result is not successful
101 102 103 |
# File 'lib/dry/schema/result.rb', line 101 def failure? !success? end |
#inspect ⇒ String
Return a string representation of the result
135 136 137 |
# File 'lib/dry/schema/result.rb', line 135 def inspect "#<#{self.class}#{to_h.inspect} errors=#{errors.to_h.inspect}>" end |
#key?(name) ⇒ Boolean
Check if a given key is present in the output
72 73 74 |
# File 'lib/dry/schema/result.rb', line 72 def key?(name) output.key?(name) end |
#message_set(options = EMPTY_HASH) ⇒ MessageSet
Return the message set
126 127 128 |
# File 'lib/dry/schema/result.rb', line 126 def ( = EMPTY_HASH) .with().(result_ast) 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.
42 43 44 45 |
# File 'lib/dry/schema/result.rb', line 42 def replace(hash) @output = hash self end |
#success? ⇒ Boolean
Check if the result is successful
92 93 94 |
# File 'lib/dry/schema/result.rb', line 92 def success? results.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)
20 21 22 23 24 25 26 |
# File 'lib/dry/schema/extensions/monads.rb', line 20 def to_monad if success? Success(self) else Failure(self) end end |