Class: BusinessFlow::Step::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/business_flow/step.rb

Overview

Represents the result of a step, and allows setting response values on an object, and merging error data into the same object.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result, output_map, output) ⇒ Result

Returns a new instance of Result.



42
43
44
45
46
# File 'lib/business_flow/step.rb', line 42

def initialize(result, output_map, output)
  @result = result
  @output = output
  @output_map = output_map
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



40
41
42
# File 'lib/business_flow/step.rb', line 40

def output
  @output
end

Class Method Details

.process_output(object, output, output_setter) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/business_flow/step.rb', line 77

def self.process_output(object, output, output_setter)
  case output_setter
  when Symbol
    object.send("#{output_setter}=", output)
  when Proc
    object.instance_exec(output, &output_setter)
  end
end

Instance Method Details

#errors?Boolean

:reek:ManualDispatch Checking respond_to? is signficantly faster than eating the NoMethodError when grabbing our error object.

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/business_flow/step.rb', line 60

def errors?
  if @result.respond_to?(:errors?)
    @result.errors?
  # This is here to support ActiveRecord. We don't want to call valid?
  # because that will run validations and a step may return a partially
  # constructed model. By instead pulling out the errors instance variable
  # we'll only merge errors if validations have already been run.
  elsif @result.class.ancestors.include?(ActiveModel::Validations) &&
        @result.instance_variable_defined?(:@errors)
    @result.instance_variable_get(:@errors).present?
  elsif @result.respond_to?(:errors)
    @result.errors.present?
  else
    false
  end
end

#executed?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/business_flow/step.rb', line 54

def executed?
  true
end

#merge_into(object) ⇒ Object



48
49
50
51
52
# File 'lib/business_flow/step.rb', line 48

def merge_into(object)
  merge_errors_into(object) if mergeable_errors?
  merge_outputs_into(object) if @output_map
  output
end