class Commande::Result
Constants
- METHODS
Concrete methods
@api private
Public Class Methods
Initialize a new result
@param payload [Hash] a payload to carry on
@return [Commande::Result]
@api private
# File lib/commande.rb, line 29 def initialize(payload = {}) @payload = payload @errors = [] @logs = [] @success = true end
Public Instance Methods
@api private
# File lib/commande.rb, line 68 def add_error(*errors) @errors << errors @errors.flatten! nil end
@api private
# File lib/commande.rb, line 98 def add_log(*logs) @logs << logs @logs.flatten! nil end
Return the class for debugging purposes.
@see ruby-doc.org/core/Object.html#method-i-class
# File lib/commande.rb, line 121 def class (class << self; self; end).superclass end
Returns the first errors collected during an operation
@return [nil,String] the error, if present
@see Commande::Result#errors
@see Commande#call @see Commande#error
@see Commande#error!
# File lib/commande.rb, line 83 def error errors.first end
Returns all the errors collected during an operation
@return [Array] the errors
@see Commande::Result#error
@see Commande#call @see Commande#error
@see Commande#error!
# File lib/commande.rb, line 63 def errors @errors.dup end
Force the status to be a failure
# File lib/commande.rb, line 51 def fail! @success = false end
Check if the current status is not successful
@return [TrueClass,FalseClass] the result of the check
# File lib/commande.rb, line 46 def failure? !successful? end
Bare minimum inspect for debugging purposes.
@return [String] the inspect string
@see ruby-doc.org/core/Object.html#method-i-inspect
# File lib/commande.rb, line 132 def inspect "#<#{self.class}:#{::Kernel.format('0x0000%<id>x', id: (__id__ << 1))}#{__inspect}>" end
Returns all the logs collected during an operation
@return [Array] the errors
@see Commande::Result#log @see Commande#call
# File lib/commande.rb, line 93 def logs @logs.dup end
Alias for __id__
@return [Fixnum] the object id
@see ruby-doc.org/core/Object.html#method-i-object_id
# File lib/commande.rb, line 141 def object_id __id__ end
# File lib/commande.rb, line 114 def payload @payload.dup end
Prepare the result before to be returned
@param payload [Hash] an updated payload
@api private
# File lib/commande.rb, line 109 def prepare!(payload) @payload.merge!(payload) self end
Returns true if responds to the given method.
@return [TrueClass,FalseClass] the result of the check
@see ruby-doc.org/core-2.2.1/Object.html#method-i-respond_to-3F
# File lib/commande.rb, line 150 def respond_to?(method_name, include_all = false) respond_to_missing?(method_name, include_all) end
Check if the current status is successful
@return [TrueClass,FalseClass] the result of the check
# File lib/commande.rb, line 39 def successful? @success && errors.empty? end
Protected Instance Methods
@api private
# File lib/commande.rb, line 168 def __inspect " @success=#{@success} @payload=#{@payload.inspect}" end
@api private
# File lib/commande.rb, line 157 def method_missing(method_name, *) @payload.fetch(method_name) { super } end
@api private
# File lib/commande.rb, line 162 def respond_to_missing?(method_name, _include_all) method_name = method_name.to_sym METHODS[method_name] || @payload.key?(method_name) end