Class: Commande::Result

Inherits:
BasicObject
Defined in:
lib/commande.rb

Constant Summary collapse

METHODS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Concrete methods

See Also:

  • #respond_to_missing?
::Hash[initialize:  true,
successful?: true,
failure?:    true,
fail!:       true,
prepare!:    true,
errors:      true,
error:       true,
logs:        true,
payload:     true].freeze

Instance Method Summary collapse

Constructor Details

#initialize(payload = {}) ⇒ Commande::Result

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.

Initialize a new result

Parameters:

  • payload (Hash) (defaults to: {})

    a payload to carry on



29
30
31
32
33
34
# File 'lib/commande.rb', line 29

def initialize(payload = {})
  @payload = payload
  @errors  = []
  @logs = []
  @success = true
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object (protected)

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.



157
158
159
# File 'lib/commande.rb', line 157

def method_missing(method_name, *)
  @payload.fetch(method_name) { super }
end

Instance Method Details

#add_error(*errors) ⇒ 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.



68
69
70
71
72
# File 'lib/commande.rb', line 68

def add_error(*errors)
  @errors << errors
  @errors.flatten!
  nil
end

#add_log(*logs) ⇒ 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.



98
99
100
101
102
# File 'lib/commande.rb', line 98

def add_log(*logs)
  @logs << logs
  @logs.flatten!
  nil
end

#classObject

Return the class for debugging purposes.



121
122
123
# File 'lib/commande.rb', line 121

def class
  (class << self; self; end).superclass
end

#errornil, String

Returns the first errors collected during an operation

Returns:

  • (nil, String)

    the error, if present

See Also:

  • #errors
  • Commande#call
  • Commande#error
  • Commande#error!


83
84
85
# File 'lib/commande.rb', line 83

def error
  errors.first
end

#errorsArray

Returns all the errors collected during an operation

Returns:

  • (Array)

    the errors

See Also:

  • #error
  • Commande#call
  • Commande#error
  • Commande#error!


63
64
65
# File 'lib/commande.rb', line 63

def errors
  @errors.dup
end

#fail!Object

Force the status to be a failure



51
52
53
# File 'lib/commande.rb', line 51

def fail!
  @success = false
end

#failure?TrueClass, FalseClass

Check if the current status is not successful

Returns:

  • (TrueClass, FalseClass)

    the result of the check



46
47
48
# File 'lib/commande.rb', line 46

def failure?
  !successful?
end

#inspectString

Bare minimum inspect for debugging purposes.

Returns:

  • (String)

    the inspect string

See Also:



132
133
134
# File 'lib/commande.rb', line 132

def inspect
  "#<#{self.class}:#{::Kernel.format('0x0000%<id>x', id: (__id__ << 1))}#{__inspect}>"
end

#logsArray

Returns all the logs collected during an operation

Returns:

  • (Array)

    the errors

See Also:

  • #log
  • Commande#call


93
94
95
# File 'lib/commande.rb', line 93

def logs
  @logs.dup
end

#object_idFixnum

Alias for __id__

Returns:

  • (Fixnum)

    the object id

See Also:



141
142
143
# File 'lib/commande.rb', line 141

def object_id
  __id__
end

#payloadObject



114
115
116
# File 'lib/commande.rb', line 114

def payload
  @payload.dup
end

#prepare!(payload) ⇒ 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.

Prepare the result before to be returned

Parameters:

  • payload (Hash)

    an updated payload



109
110
111
112
# File 'lib/commande.rb', line 109

def prepare!(payload)
  @payload.merge!(payload)
  self
end

#respond_to?(method_name, include_all = false) ⇒ TrueClass, FalseClass

Returns true if responds to the given method.

Returns:

  • (TrueClass, FalseClass)

    the result of the check

See Also:



150
151
152
# File 'lib/commande.rb', line 150

def respond_to?(method_name, include_all = false)
  respond_to_missing?(method_name, include_all)
end

#successful?TrueClass, FalseClass

Check if the current status is successful

Returns:

  • (TrueClass, FalseClass)

    the result of the check



39
40
41
# File 'lib/commande.rb', line 39

def successful?
  @success && errors.empty?
end