Module: RubySmart::Support::ThreadInfo
- Defined in:
- lib/ruby_smart/support/thread_info.rb
Constant Summary collapse
- TYPE_ORDER =
defines a array of types which will be checked by resolving the current thread type
[:rake, :console, :sidekiq, :server].freeze
Class Method Summary collapse
-
.console? ⇒ Boolean
returns true if this is a running console process.
-
.debugger? ⇒ Boolean
returns true, if this runs in a debug environment (like: ruby-debug-ide / Device).
-
.id ⇒ Integer
returns the OS process id.
-
.info ⇒ String
returns the thread type string.
-
.io_console? ⇒ Boolean
returns true if this is a running IO console process.
-
.irb? ⇒ Boolean
returns true if this is a running IRB process.
-
.name ⇒ String
returns the current thread name - for rake tasks the task name - for rails the application name.
-
.process_object_id ⇒ Integer
returns the ascertained id.
-
.pry? ⇒ Boolean
returns true if this is a running Pry process.
-
.rails? ⇒ Boolean
returns true if this is a running rails process.
-
.rails_console? ⇒ Boolean
returns true if this is a running rails console process.
-
.rake? ⇒ Boolean
returns true if this is a running rake process.
-
.server? ⇒ Boolean
returns true if this is a running server process.
-
.sidekiq? ⇒ Boolean
returns true if this is a running 'sidekiq' server process.
-
.stdout? ⇒ Boolean
return true if a log can be send to stdout.
-
.thread ⇒ Thread
returns the current thread.
-
.thread? ⇒ Boolean
returns true if this is a running thread process.
-
.thread_id ⇒ Integer
returns the current thread id.
-
.type ⇒ nil, Symbol
returns the current thread by logical order defined through const TYPE_ORDER.
-
.windowed? ⇒ Boolean
returns true if thread has a 'window'.
-
.winsize ⇒ Array<rows, columns>
returns the current windows size, if current IO has a window.
Class Method Details
.console? ⇒ Boolean
returns true if this is a running console process
26 27 28 |
# File 'lib/ruby_smart/support/thread_info.rb', line 26 def self.console? irb? || pry? || io_console? end |
.debugger? ⇒ Boolean
returns true, if this runs in a debug environment (like: ruby-debug-ide / Device)
57 58 59 |
# File 'lib/ruby_smart/support/thread_info.rb', line 57 def self.debugger? !!defined?(Debugger.handler) end |
.id ⇒ Integer
returns the OS process id
102 103 104 |
# File 'lib/ruby_smart/support/thread_info.rb', line 102 def self.id $$ end |
.info ⇒ String
returns the thread type string
126 127 128 |
# File 'lib/ruby_smart/support/thread_info.rb', line 126 def self.info "$(#{id}) -> [##{process_object_id}] @ #{type} :: #{self.name}" end |
.io_console? ⇒ Boolean
returns true if this is a running IO console process
69 70 71 |
# File 'lib/ruby_smart/support/thread_info.rb', line 69 def self.io_console? !!defined?(IO.console) && !!IO.console end |
.irb? ⇒ Boolean
returns true if this is a running IRB process
32 33 34 |
# File 'lib/ruby_smart/support/thread_info.rb', line 32 def self.irb? !!defined?(IRB) end |
.name ⇒ String
returns the current thread name
- for rake tasks the task name
- for rails the application name
111 112 113 114 115 |
# File 'lib/ruby_smart/support/thread_info.rb', line 111 def self.name return Rake.application.top_level_tasks.first.to_s if rake? return Rails.application.class.name if rails? '' end |
.process_object_id ⇒ Integer
returns the ascertained id
94 95 96 97 98 |
# File 'lib/ruby_smart/support/thread_info.rb', line 94 def self.process_object_id return Rake.application.top_level_tasks.first.object_id if rake? return Rails.application.object_id if rails? thread_id end |
.pry? ⇒ Boolean
returns true if this is a running Pry process
38 39 40 |
# File 'lib/ruby_smart/support/thread_info.rb', line 38 def self.pry? !!defined?(Pry) end |
.rails? ⇒ Boolean
returns true if this is a running rails process
20 21 22 |
# File 'lib/ruby_smart/support/thread_info.rb', line 20 def self.rails? !!defined?(Rails.application) end |
.rails_console? ⇒ Boolean
returns true if this is a running rails console process
63 64 65 |
# File 'lib/ruby_smart/support/thread_info.rb', line 63 def self.rails_console? console? && !!(defined?(Rails::Console) || ENV['RAILS_ENV']) end |
.rake? ⇒ Boolean
returns true if this is a running rake process
14 15 16 |
# File 'lib/ruby_smart/support/thread_info.rb', line 14 def self.rake? !!defined?(Rake.application) && Rake.application.top_level_tasks.any? end |
.server? ⇒ Boolean
returns true if this is a running server process. currently only detects rails
51 52 53 |
# File 'lib/ruby_smart/support/thread_info.rb', line 51 def self.server? !!defined?(Rails::Server) end |
.sidekiq? ⇒ Boolean
returns true if this is a running 'sidekiq' server process.
44 45 46 |
# File 'lib/ruby_smart/support/thread_info.rb', line 44 def self.sidekiq? !!defined?(Sidekiq) && Sidekiq.server? end |
.stdout? ⇒ Boolean
return true if a log can be send to stdout
145 146 147 |
# File 'lib/ruby_smart/support/thread_info.rb', line 145 def self.stdout? console? && windowed? end |
.thread ⇒ Thread
returns the current thread
82 83 84 |
# File 'lib/ruby_smart/support/thread_info.rb', line 82 def self.thread ::Thread.current end |
.thread? ⇒ Boolean
returns true if this is a running thread process. as it always should ...
76 77 78 |
# File 'lib/ruby_smart/support/thread_info.rb', line 76 def self.thread? !!thread end |
.thread_id ⇒ Integer
returns the current thread id
88 89 90 |
# File 'lib/ruby_smart/support/thread_info.rb', line 88 def self.thread_id thread? ? thread.object_id : 0 end |
.type ⇒ nil, Symbol
returns the current thread by logical order defined through const TYPE_ORDER
120 121 122 |
# File 'lib/ruby_smart/support/thread_info.rb', line 120 def self.type TYPE_ORDER.detect { |type| self.send("#{type}?") } || :unknown end |
.windowed? ⇒ Boolean
returns true if thread has a 'window'
132 133 134 |
# File 'lib/ruby_smart/support/thread_info.rb', line 132 def self.windowed? winsize[1] > 0 end |
.winsize ⇒ Array<rows, columns>
returns the current windows size, if current IO has a window
138 139 140 141 142 |
# File 'lib/ruby_smart/support/thread_info.rb', line 138 def self.winsize return IO.console.winsize if io_console? return [ENV['ROWS'].to_i, ENV['COLUMNS'].to_i] unless ENV['ROWS'].nil? || ENV['COLUMNS'].nil? [0, 0] end |