class Dry::Schema::Result
Monad extension for Result
@api public
Processing result
@see Processor#call
@api public
Public Class Methods
@api private
# File lib/dry/schema/result.rb, line 38 def self.new(*, **) result = super yield(result) if block_given? result.freeze end
Public Instance Methods
Read value from the output hash
@param [Symbol] name
@return [Object]
@api public
# File lib/dry/schema/result.rb, line 92 def [](name) output[name] end
Add a new error AST node
@api private
# File lib/dry/schema/result.rb, line 182 def add_error(node) result_ast << node end
Return a new result scoped to a specific path
@param path [Symbol, Array, Path]
@return [Result]
@api private
# File lib/dry/schema/result.rb, line 51 def at(path, &block) new(Path[path].reduce(output) { |a, e| a[e] }, parent: self, &block) end
@api private
# File lib/dry/schema/result.rb, line 79 def concat(other) results.concat(other) result_ast.concat(other.map(&:to_ast)) self end
Pattern matching support
@api private
# File lib/dry/schema/result.rb, line 174 def deconstruct_keys(_) output end
Check if there's an error for the provided spec
@param [Symbol, Hash<Symbol=>Symbol>] spec
@return [Boolean]
@api public
# File lib/dry/schema/result.rb, line 114 def error?(spec) message_set.any? { |msg| Path[msg.path].include?(Path[spec]) } end
Get human-readable error representation
@see message_set
@return [MessageSet]
@api public
# File lib/dry/schema/result.rb, line 143 def errors(options = EMPTY_HASH) message_set(options) end
Check if the result is not successful
@return [Boolean]
@api public
# File lib/dry/schema/result.rb, line 132 def failure? !success? end
Return a string representation of the result
@return [String]
@api public
# File lib/dry/schema/result.rb, line 166 def inspect "#<#{self.class}#{to_h.inspect} errors=#{errors.to_h.inspect}>" end
Check if a given key is present in the output
@param [Symbol] name
@return [Boolean]
@api public
# File lib/dry/schema/result.rb, line 103 def key?(name) output.key?(name) end
Return the message set
@param [Hash] options @option options [Symbol] :locale Alternative locale (default is :en) @option options [Boolean] :hints Whether to include hint messages or not @option options [Boolean] :full Whether to generate messages that include key names
@return [MessageSet]
@api public
# File lib/dry/schema/result.rb, line 157 def message_set(options = EMPTY_HASH) message_compiler.with(options).(result_ast) end
@api private
# File lib/dry/schema/result.rb, line 56 def new(output, **opts, &block) self.class.new( output, message_compiler: message_compiler, results: results, **opts, &block ) end
@api private
# File lib/dry/schema/result.rb, line 73 def replace(hash) @output = hash self end
Check if the result is successful
@return [Boolean]
@api public
# File lib/dry/schema/result.rb, line 123 def success? result_ast.empty? end
Turn result into a monad
This makes result objects work with dry-monads (or anything with a compatible interface)
@return [Dry::Monads::Success,Dry::Monads::Failure]
@api public
# File lib/dry/schema/extensions/monads.rb, line 20 def to_monad if success? Success(self) else Failure(self) end end
@api private
# File lib/dry/schema/result.rb, line 67 def update(hash) output.update(hash) self end
Private Instance Methods
A list of failure ASTs produced by rule result objects
@api private
# File lib/dry/schema/result.rb, line 191 def result_ast @result_ast ||= results.map(&:to_ast) end