Class: Cuprum::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/cuprum/result.rb

Overview

Data object that encapsulates the result of calling a Cuprum command.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil, errors: nil) ⇒ Result

Returns a new instance of Result.

Parameters:

  • value (Object) (defaults to: nil)

    The value returned by calling the command.

  • errors (Array) (defaults to: nil)

    The errors (if any) generated when the command was called.


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

#errorsArray

Returns the errors (if any) generated when the command was called.

Returns:

  • (Array)

    the errors (if any) generated when the command was called.


21
22
23
# File 'lib/cuprum/result.rb', line 21

def errors
  @errors
end

#valueObject

Returns the value returned by calling the command.

Returns:

  • (Object)

    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.

Parameters:

  • other (#value, #success?)

    An object responding to, at minimum, #value and #success?. If present, the #failure?, #errors and #halted? values will also be compared.

Returns:

  • (Boolean)

    True if all present values match the result, otherwise false.


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_errorsArray

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.

Returns:

  • (Array)

    An empty errors object.


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.

Returns:

  • (Boolean)

    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.

Returns:


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.

Returns:

  • (Boolean)

    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.

Returns:


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.

Returns:

  • (Boolean)

    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.

Returns:


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.

Returns:

  • (Boolean)

    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_resultCuprum::Result

Returns The result.

Returns:


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