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
# File 'lib/command-set/results.rb', line 52

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:



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_thread_local_dispatcher(collector) ⇒ Object Also known as: set_thread_collector, add_dispatcher



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

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:



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

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:



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

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
117
# File 'lib/command-set/results.rb', line 114

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.



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

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)


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