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

Class Method Details

.console?Boolean

returns true if this is a running console process

Returns:

  • (Boolean)


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)

Returns:

  • (Boolean)


57
58
59
# File 'lib/ruby_smart/support/thread_info.rb', line 57

def self.debugger?
  !!defined?(Debugger.handler)
end

.idInteger

returns the OS process id

Returns:

  • (Integer)

    id



102
103
104
# File 'lib/ruby_smart/support/thread_info.rb', line 102

def self.id
  $$
end

.infoString

returns the thread type string

Returns:

  • (String)

    thread-info 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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


32
33
34
# File 'lib/ruby_smart/support/thread_info.rb', line 32

def self.irb?
  !!defined?(IRB)
end

.nameString

returns the current thread name

  • for rake tasks the task name
  • for rails the application name

Returns:



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_idInteger

returns the ascertained id

Returns:

  • (Integer)

    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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


145
146
147
# File 'lib/ruby_smart/support/thread_info.rb', line 145

def self.stdout?
  console? && windowed?
end

.threadThread

returns the current thread

Returns:

  • (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 ...

Returns:

  • (Boolean)


76
77
78
# File 'lib/ruby_smart/support/thread_info.rb', line 76

def self.thread?
  !!thread
end

.thread_idInteger

returns the current thread id

Returns:

  • (Integer)

    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

.typenil, Symbol

returns the current thread by logical order defined through const TYPE_ORDER

Returns:

  • (nil, Symbol)


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'

Returns:

  • (Boolean)


132
133
134
# File 'lib/ruby_smart/support/thread_info.rb', line 132

def self.windowed?
  winsize[1] > 0
end

.winsizeArray<rows, columns>

returns the current windows size, if current IO has a window

Returns:

  • (Array<rows, columns>)

    winsize



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