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.



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

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

Instance Attribute Details

#errorsObject (readonly)

Acumulator of errors in validation + steps



15
16
17
# File 'lib/opera/operation/result.rb', line 15

def errors
  @errors
end

#executionsObject (readonly)

Acumulator of errors in validation + steps



15
16
17
# File 'lib/opera/operation/result.rb', line 15

def executions
  @executions
end

#informationObject (readonly)

Acumulator of errors in validation + steps



15
16
17
# File 'lib/opera/operation/result.rb', line 15

def information
  @information
end

#outputObject

in case of success, it contains the resulting value



19
20
21
# File 'lib/opera/operation/result.rb', line 19

def output
  @output
end

Instance Method Details

#add_error(field, message) ⇒ Object

rubocop:disable Metrics/MethodLength



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/opera/operation/result.rb', line 53

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



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

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

#add_execution(step) ⇒ Object



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

def add_execution(step)
  @executions << step
end

#add_information(hash) ⇒ Object



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

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

#failure?Boolean

Returns:

  • (Boolean)


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

def failure?
  errors.any?
end

#failuresObject



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

def failures
  errors
end

#output!Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/opera/operation/result.rb', line 40

def output!
  if failure?
    errors_combined = errors.map do |key, messages|
      "- #{key}: #{messages.join('; ')}"
    end.join("\n")

    raise OutputError.new("Operation failed — output cannot be retrieved.\n#{errors_combined}", errors)
  end

  output
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  !failure?
end