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.



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

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:



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

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:



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

def __getobj__  # :nodoc:
  @_dc_obj
end

#__setobj__(obj) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


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

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.



133
134
135
136
# File 'lib/command-set/results.rb', line 133

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

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



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

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

#cloneObject

:nodoc:



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

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

#define_dispatch_methods(dispatcher) ⇒ Object

:nodoc:



139
140
141
142
143
144
145
146
147
# File 'lib/command-set/results.rb', line 139

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:



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

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:



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

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

#relevant_collectorObject



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

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

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

Unregisters the dispatcher.



150
151
152
# File 'lib/command-set/results.rb', line 150

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

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



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

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)


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

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

#thread_stack_indexObject



110
111
112
# File 'lib/command-set/results.rb', line 110

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