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.



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

def initialize(io)
  @_dc_obj = io
  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_thread_local_dispatcher(collector) ⇒ Object Also known as: set_thread_collector, add_dispatcher



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

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:



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

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

#dispatched_method(method, *args) ⇒ Object

:nodoc:



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

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



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

def relevant_collector
  Thread.current[thread_stack_index] || 
    Thread.main[thread_stack_index]
end

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

Unregisters the dispatcher.



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

def remove_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



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

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