Class: Opera::Operation::Result

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

Defined Under Namespace

Classes: OutputError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output: nil, errors: {}) ⇒ Result

Returns a new instance of Result.



15
16
17
18
19
20
21
# File 'lib/opera/operation/result.rb', line 15

def initialize(output: nil, errors: {})
  @errors = errors
  @exceptions = {}
  @information = {}
  @executions = []
  @output = output
end

Instance Attribute Details

#errorsObject (readonly)

Acumulator of errors in validation + steps



8
9
10
# File 'lib/opera/operation/result.rb', line 8

def errors
  @errors
end

#exceptionsObject (readonly)

Acumulator of errors in validation + steps



8
9
10
# File 'lib/opera/operation/result.rb', line 8

def exceptions
  @exceptions
end

#executionsObject (readonly)

Acumulator of errors in validation + steps



8
9
10
# File 'lib/opera/operation/result.rb', line 8

def executions
  @executions
end

#informationObject (readonly)

Acumulator of errors in validation + steps



8
9
10
# File 'lib/opera/operation/result.rb', line 8

def information
  @information
end

#outputObject

in case of success, it contains the resulting value



13
14
15
# File 'lib/opera/operation/result.rb', line 13

def output
  @output
end

Instance Method Details

#add_error(field, message) ⇒ Object

rubocop:disable Metrics/MethodLength



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/opera/operation/result.rb', line 42

def add_error(field, message)
  @errors[field] ||= []
  if message.is_a?(Hash)
    if @errors[field].first&.is_a?(Hash)
      @errors[field].first.merge!(message)
    else
      @errors[field].push(message)
    end
  else
    @errors[field].concat(Array(message))
  end
  @errors[field].uniq!
end

#add_errors(errors) ⇒ Object

rubocop:enable Metrics/MethodLength



57
58
59
60
61
# File 'lib/opera/operation/result.rb', line 57

def add_errors(errors)
  errors.to_hash.each_pair do |key, value|
    add_error(key, value)
  end
end

#add_exception(method, message, classname: nil) ⇒ Object



63
64
65
66
67
# File 'lib/opera/operation/result.rb', line 63

def add_exception(method, message, classname: nil)
  key = [classname, Array(method).first].compact.join('#')
  @exceptions[key] ||= []
  @exceptions[key].push(message)
end

#add_exceptions(exceptions) ⇒ Object



69
70
71
72
73
# File 'lib/opera/operation/result.rb', line 69

def add_exceptions(exceptions)
  exceptions.each_pair do |key, value|
    add_exception(key, value)
  end
end

#add_execution(step) ⇒ Object



79
80
81
# File 'lib/opera/operation/result.rb', line 79

def add_execution(step)
  @executions << step
end

#add_information(hash) ⇒ Object



75
76
77
# File 'lib/opera/operation/result.rb', line 75

def add_information(hash)
  @information.merge!(hash)
end

#failure?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/opera/operation/result.rb', line 23

def failure?
  errors.any? || exceptions.any?
end

#failuresObject



31
32
33
# File 'lib/opera/operation/result.rb', line 31

def failures
  errors.merge(exceptions)
end

#output!Object

Raises:



35
36
37
38
39
# File 'lib/opera/operation/result.rb', line 35

def output!
  raise OutputError, 'Cannot retrieve output from a Failure.' if failure?

  output
end

#success?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/opera/operation/result.rb', line 27

def success?
  !failure?
end