class Cuprum::Result
Data object that encapsulates the result of calling a Cuprum
command.
Constants
- STATUSES
Enumerates the default permitted values for a
Result#status
.
Attributes
@return [Object] the error (if any) generated when the command was
called.
@return [Symbol] the status of the result, either :success or :failure.
@return [Object] the value returned by calling the command.
Public Class Methods
@param value [Object] The value returned by calling the command. @param error [Object] The error (if any) generated when the command was
called. Can be a Cuprum::Error, a model errors object, etc.
@param status [String, Symbol] The status of the result. Must be :success,
:failure, or nil.
# File lib/cuprum/result.rb, line 16 def initialize(value: nil, error: nil, status: nil) @value = value @error = error @status = resolve_status(status) end
Public Instance Methods
Compares the other object to the result.
@param other [#value, success?
] An object responding to, at minimum,
#value and #success?. If present, the #failure? and #error values will also be compared.
@return [Boolean] True if all present values match the result, otherwise
false.
# File lib/cuprum/result.rb, line 40 def ==(other) return false unless other.respond_to?(:value) && other.value == value return false unless other.respond_to?(:status) && other.status == status return false unless other.respond_to?(:error) && other.error == error true end
@return [Boolean] true if the result status is :failure, otherwise false.
# File lib/cuprum/result.rb, line 49 def failure? @status == :failure end
@return [Boolean] true if the result status is :success, otherwise false.
# File lib/cuprum/result.rb, line 54 def success? @status == :success end
@return [Cuprum::Result] The result.
# File lib/cuprum/result.rb, line 59 def to_cuprum_result self end
Private Instance Methods
# File lib/cuprum/result.rb, line 65 def defined_statuses self.class::STATUSES end
# File lib/cuprum/result.rb, line 69 def normalize_status(status) return status unless status.is_a?(String) || status.is_a?(Symbol) tools.string_tools.underscore(status).intern end
# File lib/cuprum/result.rb, line 75 def resolve_status(status) return error.nil? ? :success : :failure if status.nil? normalized = normalize_status(status) return normalized if defined_statuses.include?(normalized) raise ArgumentError, "invalid status #{status.inspect}" end
# File lib/cuprum/result.rb, line 85 def tools SleepingKingStudios::Tools::Toolbelt.instance end