Class: RightScale::RightPopen::TargetProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/right_popen/target_proxy.rb

Overview

proxies calls to target to simplify popen3 implementation code and to make the proxied callbacks slightly more efficient.

Constant Summary collapse

HANDLER_NAME_TO_PARAMETER_COUNT =
{
  :exit_handler            => 1,
  :pid_handler             => 1,
  :size_limit_handler      => 0,
  :stderr_handler          => 1,
  :stdout_handler          => 1,
  :timeout_handler         => 0,
  :watch_handler           => 1,
  :async_exception_handler => 1,
}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TargetProxy

Returns a new instance of TargetProxy.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/right_popen/target_proxy.rb', line 45

def initialize(options = {})
  if options[:target].nil? &&
     !(options.keys & HANDLER_NAME_TO_PARAMETER_COUNT.keys).empty?
    raise ::ArgumentError, 'Missing target'
  end
  @target = options[:target]  # hold target reference (if any) against GC

  # define an instance method for each handler that either proxies
  # directly to the target method (with parameters) or else does nothing.
  HANDLER_NAME_TO_PARAMETER_COUNT.each do |handler_name, parameter_count|
    parameter_list = (1..parameter_count).map { |i| "p#{i}" }.join(', ')
    instance_eval <<EOF
if @target && options[#{handler_name.inspect}]
  @#{handler_name.to_s}_method = @target.method(options[#{handler_name.inspect}])
  def #{handler_name.to_s}(#{parameter_list})
    @#{handler_name.to_s}_method.call(#{parameter_list})
  end
else
  def #{handler_name.to_s}(#{parameter_list}); true; end
end
EOF
    end
end