class BusinessFlow::Step::Result

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

Attributes

output[R]

Public Class Methods

new(result, output_map, output) click to toggle source
# File lib/business_flow/step.rb, line 40
def initialize(result, output_map, output)
  @result = result
  @output = output
  @output_map = output_map
end
process_output(object, output, output_setter) click to toggle source
# File lib/business_flow/step.rb, line 75
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

Public Instance Methods

errors?() click to toggle source

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

# File lib/business_flow/step.rb, line 58
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?() click to toggle source
# File lib/business_flow/step.rb, line 52
def executed?
  true
end
merge_into(object) click to toggle source
# File lib/business_flow/step.rb, line 46
def merge_into(object)
  merge_errors_into(object) if mergeable_errors?
  merge_outputs_into(object) if @output_map
  output
end

Private Instance Methods

merge_errors_into(object) click to toggle source
# File lib/business_flow/step.rb, line 92
def merge_errors_into(object)
  @result.errors.each do |attribute, message|
    (object.errors[attribute] << message).uniq!
  end
  throw :halt_step
end
merge_outputs_into(object) click to toggle source
# File lib/business_flow/step.rb, line 99
def merge_outputs_into(object)
  @output_map.each do |(output_name, output_setter)|
    output = @result.public_send(output_name)
    Result.process_output(object, output, output_setter)
  end
end
mergeable_errors?() click to toggle source

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

# File lib/business_flow/step.rb, line 88
def mergeable_errors?
  @result.respond_to?(:errors) && errors?
end