class Patir::RubyCommand

This class allows you to wrap Ruby blocks and handle them like Command

Provide a block to RubyCommand#new and you can execute the block using RubyCommand#run

The block receives the instance of RubyCommand so you can set the output and error output.

If the block runs to the end the command is considered successful.

Raising an exception in the block will set the command status to :error.

The exception message will be appended to the error output of the command

Examples

An example (using the excellent HighLine lib) of a CLI prompt as a RubyCommand

RubyCommand.new("prompt") do |cmd|  
  cmd.output=""
  cmd.error=""
  unless HighLine.agree("#{step.text}?")
    cmd.error="Why not?"
    raise "You did not agree" 
  end
end

Attributes

cmd[R]
context[R]
working_directory[R]

Public Class Methods

new(name,working_directory=nil,&block) click to toggle source
# File lib/patir/command.rb, line 482
def initialize name,working_directory=nil,&block
  @name=name
  @working_directory=working_directory||"."
  if block_given?
    @cmd=block 
  else
    raise "You need to provide a block"
  end
end

Public Instance Methods

run(context=nil) click to toggle source

Runs the associated block

# File lib/patir/command.rb, line 492
def run context=nil
  @run=true
  @context=context
  @error=""
  @output=""
  begin
    t1=Time.now
    Dir.chdir(@working_directory) do
      @cmd.call(self)
      @status=:success
    end
  rescue StandardError
    @error<<"\n#{$!.message}"
    @error<<"\n#{$!.backtrace}" if $DEBUG
    @status=:error
  ensure
    @exec_time=Time.now-t1
  end
  @context=nil
  return @status
end