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, :server].freeze
Class Method Summary collapse
-
.console? ⇒ Boolean
returns true if this is a running console process.
-
.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.
-
.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 |
.id ⇒ Integer
returns the OS process id
90 91 92 |
# File 'lib/ruby_smart/support/thread_info.rb', line 90 def self.id $$ end |
.info ⇒ String
returns the thread type string
114 115 116 117 118 |
# File 'lib/ruby_smart/support/thread_info.rb', line 114 def self.info strs = ["$#{id}", "[##{process_object_id}]","@ #{type}"] strs << " :: #{name}" if name != '' strs.join ' ' end |
.io_console? ⇒ Boolean
returns true if this is a running IO console process
57 58 59 |
# File 'lib/ruby_smart/support/thread_info.rb', line 57 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
99 100 101 102 103 |
# File 'lib/ruby_smart/support/thread_info.rb', line 99 def self.name return Rake.application.top_level_tasks.first.to_s if rake? return Rails.application.to_s.split('::').first if rails? '' end |
.process_object_id ⇒ Integer
returns the ascertained id
82 83 84 85 86 |
# File 'lib/ruby_smart/support/thread_info.rb', line 82 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
51 52 53 |
# File 'lib/ruby_smart/support/thread_info.rb', line 51 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
45 46 47 |
# File 'lib/ruby_smart/support/thread_info.rb', line 45 def self.server? !!defined?(Rails::Server) end |
.stdout? ⇒ Boolean
return true if a log can be send to stdout
135 136 137 |
# File 'lib/ruby_smart/support/thread_info.rb', line 135 def self.stdout? console? && windowed? end |
.thread ⇒ Thread
returns the current thread
70 71 72 |
# File 'lib/ruby_smart/support/thread_info.rb', line 70 def self.thread ::Thread.current end |
.thread? ⇒ Boolean
returns true if this is a running thread process. as it always should …
64 65 66 |
# File 'lib/ruby_smart/support/thread_info.rb', line 64 def self.thread? !!thread end |
.thread_id ⇒ Integer
returns the current thread id
76 77 78 |
# File 'lib/ruby_smart/support/thread_info.rb', line 76 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
108 109 110 |
# File 'lib/ruby_smart/support/thread_info.rb', line 108 def self.type TYPE_ORDER.detect { |type| self.send("#{type}?") } || :unknown end |
.windowed? ⇒ Boolean
returns true if thread has a ‘window’
122 123 124 |
# File 'lib/ruby_smart/support/thread_info.rb', line 122 def self.windowed? winsize[1] > 0 end |
.winsize ⇒ Array<rows, columns>
returns the current windows size, if current IO has a window
128 129 130 131 132 |
# File 'lib/ruby_smart/support/thread_info.rb', line 128 def self.winsize return IO.console.winsize if io_console? return [ENV['ROWS'], ENV['COLUMNS']] unless ENV['ROWS'].nil? && ENV['COLUMNS'].nil? [0, 0] end |