class Expectr::Interpreter

Internal: Provide an interface to Expectr in a manner designed to mimic Expect’s original functionality.

Constants

DEFAULT_TIMEOUT

Attributes

filename[RW]

Public: Filename of currently executing script.

Public Class Methods

new(source) click to toggle source

Public: Initialize a new Expectr Interpreter interface.

source - String containing the source to be executed.

# File lib/expectr/interpreter.rb, line 15
def initialize(source)
  @source = source
  @variables = { timeout: DEFAULT_TIMEOUT }
  @expect = nil
end

Public Instance Methods

close() click to toggle source

Public: Terminate any process associated with the Interpreter.

Returns nothing.

# File lib/expectr/interpreter.rb, line 94
def close
  @expect.kill!(:KILL) if @expect.respond_to?(:kill!)
rescue Expectr::ProcessError
ensure
  @expect = nil
end
expect(*args) click to toggle source

Public: Provide an interface to Expectr#expect.

args - Arguments to be passed through to the Expectr object.

Returns per Expectr#expect.

# File lib/expectr/interpreter.rb, line 73
def expect(*args)
  @expect.expect(*args)
end
log_user(enable) click to toggle source

Public: Set whether output from the active Expectr object should be echoed to the user.

enable - Boolean value denoting whether output to the screen should be

enabled.
In order to keep compatibility with Expect, a Fixnum can be
passed, such that any number greater than 0 is evaluated as
true, and 0 or less is false.

Returns nothing. Raises TypeError if enable is not a boolean.

# File lib/expectr/interpreter.rb, line 48
def log_user(enable)
  if enable.is_a?(Numeric)
    enable = (enable > 0)
  end
  unless enable.is_a?(TrueClass) || enable.is_a?(FalseClass)
    raise(TypeError, Errstr::BOOLEAN_OR_FIXNUM % enable.class.name)
  end

  set(:flush_buffer, enable)
end
run() click to toggle source

Public: Run the source associated with the Interface object.

Returns nothing.

# File lib/expectr/interpreter.rb, line 24
def run
  eval(@source, binding, (@filename || "(expectr)"), 0)
end
send(str) click to toggle source

Public: Send a String to the process referenced by the active Expectr object.

str - String to send to the process.

Returns nothing. Raises NotRunningError if no Expectr object is active.

# File lib/expectr/interpreter.rb, line 84
def send(str)
  if @expect.nil?
    raise(NotRunningError, Errstr::PROCESS_NOT_RUNNING)
  end
  @expect.send(str)
end
send_user(*str) click to toggle source

Public: Print one or more messages to $stdout.

str - String or Array of Strings to print to $stdout.

Returns nothing.

# File lib/expectr/interpreter.rb, line 33
def send_user(*str)
  str.each { |line| $stdout.print line }
end
spawn(cmd) click to toggle source

Public: Spawn an instance of a command via Expectr.

cmd - String referencing the application to spawn.

Returns nothing.

# File lib/expectr/interpreter.rb, line 64
def spawn(cmd)
  @expect = Expectr.new(cmd, @variables)
end

Private Instance Methods

set(variable_name, value) click to toggle source

Internal: Set value in internal Hash and update attr_accessor value in the active Expectr object if applicable.

variable_name - Name of key to set in the internal Hash. value - Value to associate with the key.

Returns nothing.

# File lib/expectr/interpreter.rb, line 110
def set(variable_name, value)
  @variables[variable_name] = value
  method_name = (variable_name.to_s + "=").to_sym
  if @expect.methods.include?(method_name)
    @expect.method(method_name).call(value)
  end
end