class Riverbed::Flow

Attributes

data[RW]
logger[RW]

Public Class Methods

new(input, options) click to toggle source
# File lib/riverbed/flow.rb, line 5
def initialize(input, options)
  @logger = options.fetch(:logger, Logger.new($stdout))

  @data = options.fetch(:data_object, Riverbed::Data.new(input))
end
run(input = nil, options: {}) click to toggle source
# File lib/riverbed/flow.rb, line 11
def self.run(input = nil, options: {})
  new(input, options).run
end

Public Instance Methods

always() click to toggle source
# File lib/riverbed/flow.rb, line 19
def always
  []
end
run() click to toggle source
# File lib/riverbed/flow.rb, line 23
def run
  begin
    run_steps(steps)
  rescue StandardError => e
    on_error(e)
    data.add_error(e)
  end

  run_steps(always)

  data.last_result
end
steps() click to toggle source
# File lib/riverbed/flow.rb, line 15
def steps
  raise NotImplementedError
end

Private Instance Methods

log_execution(step_name) { || ... } click to toggle source
# File lib/riverbed/flow.rb, line 53
def log_execution(step_name)
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  yield
  end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  logger&.info("#{log_id} Step #{step_name} executed in #{((end_time - start_time) * 1000).round(2)} ms")
end
log_id() click to toggle source
# File lib/riverbed/flow.rb, line 60
def log_id
  @log_id ||= SecureRandom.urlsafe_base64(8)
end
on_error(error) click to toggle source
# File lib/riverbed/flow.rb, line 64
def on_error(error)
  logger&.error(error)
end
run_steps(steps) click to toggle source
# File lib/riverbed/flow.rb, line 40
def run_steps(steps)
  steps.each do |step_class|
    step = step_class.new(data, logger)
    next if step.skip?

    log_execution(step.name) do
      data.add_result(step.name, step.execute)
    end

    break if step.break_flow?
  end
end