Class: Fasten::StdThreadProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/fasten/std_thread_proxy.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fasten_original) ⇒ StdThreadProxy

Returns a new instance of StdThreadProxy.



5
6
7
# File 'lib/fasten/std_thread_proxy.rb', line 5

def initialize(fasten_original)
  @fasten_original = fasten_original
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



21
22
23
24
25
26
# File 'lib/fasten/std_thread_proxy.rb', line 21

def method_missing(method, *args, &block)
  target = Thread.current[:FASTEN_STD_THREAD_PROXY] || @fasten_original
  target.send method, *args, &block
rescue StandardError => e
  raise e
end

Instance Attribute Details

#fasten_originalObject (readonly)

Returns the value of attribute fasten_original.



3
4
5
# File 'lib/fasten/std_thread_proxy.rb', line 3

def fasten_original
  @fasten_original
end

Class Method Details

.installObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fasten/std_thread_proxy.rb', line 29

def install
  return if @installed

  oldverbose = $VERBOSE
  $VERBOSE = nil

  Object.const_set :STDOUT, StdThreadProxy.new(STDOUT) # rubocop:disable Style/GlobalStdStream
  Object.const_set :STDERR, StdThreadProxy.new(STDERR) # rubocop:disable Style/GlobalStdStream

  $stdout = StdThreadProxy.new $stdout
  $stderr = StdThreadProxy.new $stderr

  @installed = true
ensure
  $VERBOSE = oldverbose
end

.thread_ioObject



50
51
52
# File 'lib/fasten/std_thread_proxy.rb', line 50

def thread_io
  Thread.current[:FASTEN_STD_THREAD_PROXY]
end

.thread_io=(io) ⇒ Object



46
47
48
# File 'lib/fasten/std_thread_proxy.rb', line 46

def thread_io=(io)
  Thread.current[:FASTEN_STD_THREAD_PROXY] = io
end

.uninstallObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fasten/std_thread_proxy.rb', line 54

def uninstall
  return unless @installed

  oldverbose = $VERBOSE
  $VERBOSE = nil

  Object.const_set :STDOUT, STDOUT.fasten_original if STDOUT.is_a? StdThreadProxy # rubocop:disable Style/GlobalStdStream
  Object.const_set :STDERR, STDERR.fasten_original if STDERR.is_a? StdThreadProxy # rubocop:disable Style/GlobalStdStream

  $stdout = $stdout.fasten_original if $stdout.is_a? StdThreadProxy
  $stderr = $stderr.fasten_original if $stderr.is_a? StdThreadProxy

  @installed = nil
ensure
  $VERBOSE = oldverbose
end

Instance Method Details

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
# File 'lib/fasten/std_thread_proxy.rb', line 9

def respond_to?(*args)
  target = Thread.current[:FASTEN_STD_THREAD_PROXY] || @fasten_original
  target.send :respond_to?, *args
end