Class: Cuprum::Result
- Inherits:
-
Object
- Object
- Cuprum::Result
- Defined in:
- lib/cuprum/result.rb
Overview
Data object that encapsulates the result of calling a Cuprum command.
Instance Attribute Summary collapse
-
#errors ⇒ Array
The errors (if any) generated when the command was called.
-
#value ⇒ Object
The value returned by calling the command.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares the other object to the result.
-
#build_errors ⇒ Array
Generates an empty errors object.
-
#empty? ⇒ Boolean
True if the result is empty, i.e.
-
#failure! ⇒ Cuprum::Result
Marks the result as a failure, whether or not the command generated any errors.
-
#failure? ⇒ Boolean
False if the command did not generate any errors, otherwise true.
-
#halt! ⇒ Cuprum::Result
Marks the result as halted.
-
#halted? ⇒ Boolean
True if the command has been halted, and will not run any subsequent chained commands.
-
#initialize(value = nil, errors: nil) ⇒ Result
constructor
A new instance of Result.
-
#success! ⇒ Cuprum::Result
Marks the result as a success, whether or not the command generated any errors.
-
#success? ⇒ Boolean
True if the command did not generate any errors, otherwise false.
-
#to_result ⇒ Cuprum::Result
The result.
- #update(other_result) ⇒ Object private
Constructor Details
#initialize(value = nil, errors: nil) ⇒ Result
Returns a new instance of Result.
9 10 11 12 13 14 |
# File 'lib/cuprum/result.rb', line 9 def initialize value = nil, errors: nil @value = value @errors = errors.nil? ? build_errors : errors @status = nil @halted = false end |
Instance Attribute Details
#errors ⇒ Array
Returns the errors (if any) generated when the command was called.
21 22 23 |
# File 'lib/cuprum/result.rb', line 21 def errors @errors end |
#value ⇒ Object
Returns the value returned by calling the command.
17 18 19 |
# File 'lib/cuprum/result.rb', line 17 def value @value end |
Instance Method Details
#==(other) ⇒ Boolean
Compares the other object to the result.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/cuprum/result.rb', line 36 def == other return false unless other.respond_to?(:value) && other.value == value unless other.respond_to?(:success?) && other.success? == success? return false end # unless if other.respond_to?(:failure?) && other.failure? != failure? return false end # if if other.respond_to?(:errors) && other.errors != errors return false end # if if other.respond_to?(:halted?) && other.halted? != halted? return false end # if true end |
#build_errors ⇒ Array
Generates an empty errors object. When the command is called, the result will have its #errors property initialized to the value returned by #build_errors. By default, this is an array. If you want to use a custom errors object type, override this method in a subclass.
150 151 152 |
# File 'lib/cuprum/result.rb', line 150 def build_errors [] end |
#empty? ⇒ Boolean
Returns true if the result is empty, i.e. has no value or errors and does not have its status set or is halted.
64 65 66 |
# File 'lib/cuprum/result.rb', line 64 def empty? value.nil? && errors.empty? && @status.nil? && !halted? end |
#failure! ⇒ Cuprum::Result
Marks the result as a failure, whether or not the command generated any errors.
72 73 74 75 76 |
# File 'lib/cuprum/result.rb', line 72 def failure! @status = :failure self end |
#failure? ⇒ Boolean
Returns false if the command did not generate any errors, otherwise true.
80 81 82 |
# File 'lib/cuprum/result.rb', line 80 def failure? @status == :failure || (@status.nil? && !errors.empty?) end |
#halt! ⇒ Cuprum::Result
Marks the result as halted. Any subsequent chained commands will not be
run.
88 89 90 91 92 |
# File 'lib/cuprum/result.rb', line 88 def halt! @halted = true self end |
#halted? ⇒ Boolean
Returns true if the command has been halted, and will not run any subsequent chained commands.
96 97 98 |
# File 'lib/cuprum/result.rb', line 96 def halted? @halted end |
#success! ⇒ Cuprum::Result
Marks the result as a success, whether or not the command generated any errors.
104 105 106 107 108 |
# File 'lib/cuprum/result.rb', line 104 def success! @status = :success self end |
#success? ⇒ Boolean
Returns true if the command did not generate any errors, otherwise false.
112 113 114 |
# File 'lib/cuprum/result.rb', line 112 def success? @status == :success || (@status.nil? && errors.empty?) end |
#to_result ⇒ Cuprum::Result
Returns The result.
117 118 119 |
# File 'lib/cuprum/result.rb', line 117 def to_result self end |
#update(other_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.
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/cuprum/result.rb', line 122 def update other_result return self if other_result.nil? self.value = other_result.value update_status(other_result) update_errors(other_result) halt! if other_result.halted? self end |