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

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

.idInteger

returns the OS process id

Returns:

  • (Integer)

    id



90
91
92
# File 'lib/ruby_smart/support/thread_info.rb', line 90

def self.id
  $$
end

.infoString

returns the thread type string

Returns:

  • (String)

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

Returns:

  • (Boolean)


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

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:



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_idInteger

returns the ascertained id

Returns:

  • (Integer)

    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

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)


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

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)


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

Returns:

  • (Boolean)


135
136
137
# File 'lib/ruby_smart/support/thread_info.rb', line 135

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

.threadThread

returns the current thread

Returns:

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

Returns:

  • (Boolean)


64
65
66
# File 'lib/ruby_smart/support/thread_info.rb', line 64

def self.thread?
  !!thread
end

.thread_idInteger

returns the current thread id

Returns:

  • (Integer)

    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

.typenil, Symbol

returns the current thread by logical order defined through const TYPE_ORDER

Returns:

  • (nil, Symbol)


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’

Returns:

  • (Boolean)


122
123
124
# File 'lib/ruby_smart/support/thread_info.rb', line 122

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



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