Class: Bait::Phase

Inherits:
Object
  • Object
show all
Defined in:
lib/bait/phase.rb

Defined Under Namespace

Classes: UnexpectedHandlerDefinition

Constant Summary collapse

POSSIBLE_HANDLERS =
%w(init output rescue missing done)

Instance Method Summary collapse

Constructor Details

#initialize(script) ⇒ Phase

Returns a new instance of Phase.



6
7
8
9
# File 'lib/bait/phase.rb', line 6

def initialize script
  @script = script
  @handlers = {}
end

Instance Method Details

#handle(name, &block) ⇒ Object Also known as: on



11
12
13
14
15
16
17
18
# 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

#run!Object



22
23
24
25
26
27
28
29
30
31
32
33
# 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