lib/expectr/adopt.rb in expectr-2.0.0 vs lib/expectr/adopt.rb in expectr-2.0.1

- old
+ new

@@ -1,37 +1,47 @@ require 'expectr' require 'expectr/interface' require 'expectr/child' class Expectr - # Internal: The Expectr::Adopt Class contains the interface to interacting - # with child processes not spawned by Expectr. - # - # All methods with the prefix 'interface_' in their name will return a Proc - # designed to be defined as an instance method in the primary Expectr object. - # These methods will all be documented as if they are the Proc in question. - class Adopt < Expectr::Child - # Public: Initialize a new Expectr::Adopt object. + # Public: The Expectr::Adopt Module defines the interface for interacting + # with child processes without spawning them through Expectr. + module Adopt + include Expectr::Child + + # 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 class. + # the methods from the Expectr::Child module. # - # 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) + # 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) # - # Raises TypeError if arguments are of type other than IO. - def initialize(stdin, stdout) - unless stdin.kind_of?(IO) && stdout.kind_of?(IO) + # Returns nothing. + # Raises TypeError if args[:stdin] or args[:stdout] aren't of type IO. + def init_interface(args) + unless args[:stdin].kind_of?(IO) && args[:stdout].kind_of?(IO) raise(TypeError, Errstr::IO_EXPECTED) end - @stdin = stdin - @stdout = stdout + @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 - # Public: Present a streamlined interface to create a new Expectr instance. + # 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) @@ -42,9 +52,9 @@ def self.spawn(stdout, stdin, pid = 1, args = {}) args[:interface] = :adopt args[:stdin] = stdin args[:stdout] = stdout args[:pid] = pid - Expectr.new('', args) + Expectr.new(args) end end end