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



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/opera/operation/result.rb', line 47

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



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

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

#add_execution(step) ⇒ Object



72
73
74
# File 'lib/opera/operation/result.rb', line 72

def add_execution(step)
  @executions << step
end

#add_information(hash) ⇒ Object



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

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

Raises:



40
41
42
43
44
# File 'lib/opera/operation/result.rb', line 40

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

  output
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  !failure?
end