Class: Command::OutputStandin

Inherits:
IO
  • Object
show all
Defined in:
lib/command-set/results.rb

Overview

Wraps an IO using DelegateClass. Dispatches all calls to the IO, until a Collector is registered, at which point, methods that the Collector handles will get sent to it.

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ OutputStandin

Returns a new instance of OutputStandin.



52
53
54
55
56
57
58
# File 'lib/command-set/results.rb', line 52

def initialize(io)
  @_dc_obj = io
  @dispatch_stack = nil
  unless io.fileno.nil?
    super(io.fileno,"w")
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object

:nodoc:



60
61
62
63
64
65
# File 'lib/command-set/results.rb', line 60

def method_missing(m, *args)  # :nodoc:
  unless @_dc_obj.respond_to?(m)
    super(m, *args)
  end
  @_dc_obj.__send__(m, *args)
end

Instance Method Details

#__getobj__Object

:nodoc:



72
73
74
# File 'lib/command-set/results.rb', line 72

def __getobj__  # :nodoc:
  @_dc_obj
end

#__setobj__(obj) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


76
77
78
79
# File 'lib/command-set/results.rb', line 76

def __setobj__(obj)  # :nodoc:
  raise ArgumentError, "cannot delegate to self" if self.equal?(obj)
  @_dc_obj = obj
end

#add_dispatcher(collector) ⇒ Object Also known as: set_default_collector

Puts the dispatcher in place to handle normal IO methods.



138
139
140
141
# File 'lib/command-set/results.rb', line 138

def add_dispatcher(collector)
  @dispatch_stack = collector
  define_dispatch_methods(collector)
end

#add_thread_local_dispatcher(collector) ⇒ Object Also known as: set_thread_collector



131
132
133
134
# File 'lib/command-set/results.rb', line 131

def add_thread_local_dispatcher(collector)
  Thread.current[thread_stack_index]=collector
  define_dispatch_methods(collector)
end

#cloneObject

:nodoc:



81
82
83
84
# File 'lib/command-set/results.rb', line 81

def clone  # :nodoc:
  super
  __setobj__(__getobj__.clone)
end

#define_dispatch_methods(dispatcher) ⇒ Object

:nodoc:



144
145
146
147
148
149
150
151
152
# File 'lib/command-set/results.rb', line 144

def define_dispatch_methods(dispatcher)# :nodoc:
  dispatcher.dispatches.each do |dispatch|
    (class << self; self; end).module_eval "      def \#{dispatch}(*args)\n        dispatched_method(:\#{dispatch.to_s}, *args)\n      end\n    EOS\n  end\nend\n"

#dispatched_method(method, *args) ⇒ Object

:nodoc:



123
124
125
126
127
128
129
# File 'lib/command-set/results.rb', line 123

def dispatched_method(method, *args)# :nodoc:
  collector = relevant_collector
  if not collector.nil? and collector.respond_to?(method)
    return collector.__send__(method, *args)
  end
  return __getobj__.__send__(method, *args)
end

#dupObject

:nodoc:



86
87
88
89
# File 'lib/command-set/results.rb', line 86

def dup  # :nodoc:
  super
  __setobj__(__getobj__.dup)
end

#relevant_collectorObject



119
120
121
# File 'lib/command-set/results.rb', line 119

def relevant_collector
  Thread.current[thread_stack_index] || @dispatch_stack
end

#remove_dispatcher(dispatcher) ⇒ Object Also known as: remove_collector

Unregisters the dispatcher.



155
156
157
# File 'lib/command-set/results.rb', line 155

def remove_dispatcher(dispatcher)
  @dispatch_stack = nil if @dispatch_stack == dispatcher
end

#remove_thread_local_dispatcher(dispatcher) ⇒ Object Also known as: remove_thread_collector



160
161
162
163
164
# File 'lib/command-set/results.rb', line 160

def remove_thread_local_dispatcher(dispatcher)
  if Thread.current[thread_stack_index] == dispatcher
    Thread.current[thread_stack_index] = nil
  end
end

#respond_to?(m) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/command-set/results.rb', line 67

def respond_to?(m)  # :nodoc:
  return true if super
  return @_dc_obj.respond_to?(m)
end

#thread_stack_indexObject



115
116
117
# File 'lib/command-set/results.rb', line 115

def thread_stack_index
  "standin_dispatch_stack_#{self.object_id}"
end