class Bait::Phase

Constants

POSSIBLE_HANDLERS

Public Class Methods

new(script) click to toggle source
# File lib/bait/phase.rb, line 6
def initialize script
  @script = script
  @handlers = {}
end

Public Instance Methods

handle(name, &block) click to toggle source
# File lib/bait/phase.rb, line 11
def handle name, &block
  if POSSIBLE_HANDLERS.include? name.to_s
    @handlers[name] = block
  else
    raise UnexpectedHandlerDefinition
  end
  self
end
Also aliased as: on
on(name, &block)
Alias for: handle
run!() click to toggle source
# File lib/bait/phase.rb, line 22
def run!
  if File.exists?(@script)
    handler(:init)
    zerostatus = execute_subprocess do |output_line|
      handler(:output, output_line)
    end
    handler(:done, zerostatus)
  else
    msg = "Script #{@script} was expected but is missing."
    handler(:missing, msg)
  end
end

Private Instance Methods

execute_subprocess(&block) click to toggle source
# File lib/bait/phase.rb, line 43
def execute_subprocess &block
  zerostatus = 'unknown'
  block.call("\nExecuting #{@script}\n\n")
  Open3.popen2e(@script) do |stdin, oe, wait_thr|
    oe.each {|line| block.call(line) }
    zerostatus = wait_thr.value.exitstatus == 0
  end
rescue => ex
  handler(:rescue, ex)
  zerostatus = false
ensure
  return zerostatus
end
handler(name, *args) click to toggle source
# File lib/bait/phase.rb, line 37
def handler name, *args
  if target = @handlers[name]
    target.call(*args)
  end
end