Module: Expectr::Adopt

Includes:
Child
Defined in:
lib/expectr/adopt.rb,
lib/expectr/errstr.rb

Overview

Public: The Expectr::Adopt Module defines the interface for interacting with child processes without spawning them through Expectr.

Defined Under Namespace

Modules: Errstr

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Child

#kill!, #output_loop, #send, #winsize

Methods included from Interface

#interact_thread, #prepare_interact_interface

Class Method Details

.spawn(stdout, stdin, pid = 1, args = {}) ⇒ Object

Public: Present a streamlined interface to create a new Expectr instance using the Adopt interface.

stdout - IO object open for reading. stdin - IO object open for writing. pid - FixNum corresponding to the PID of the process being adopted

(default: 1)

args - A Hash used to specify options for the new object, per

Expectr#initialize.

Returns a new Expectr object



52
53
54
55
56
57
58
# File 'lib/expectr/adopt.rb', line 52

def self.spawn(stdout, stdin, pid = 1, args = {})
  args[:interface] = :adopt
  args[:stdin] = stdin
  args[:stdout] = stdout
  args[:pid] = pid
  Expectr.new(args)
end

Instance Method Details

#init_interface(args) ⇒ Object

Public: Initialize the Expectr interface, adopting IO objects to act on them as if they were produced by spawning a child process. IO Objects are named in such a way as to maintain interoperability with the methods from the Expectr::Child module.

args - Hash containing IO Objects and optionally a PID to watch.

stdin  - IO object open for writing.
stdout - IO object open for reading.
pid    - FixNum corresponding to the PID of the process being
         adopted. (default: 1)

Returns nothing. Raises TypeError if args or args aren’t of type IO.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/expectr/adopt.rb', line 24

def init_interface(args)
  unless args[:stdin].kind_of?(IO) && args[:stdout].kind_of?(IO)
    raise(TypeError, Errstr::IO_EXPECTED)
  end
  @stdin = args[:stdin]
  @stdout = args[:stdout]
  @stdout.winsize = $stdout.winsize if $stdout.tty?
  @pid = args[:pid] || 0

  if @pid > 0
    Thread.new do
      Process.wait @pid
      @pid = 0
    end
  end
end