Class: RubyJard::ThreadInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_jard/thread_info.rb

Overview

A wrapper for thread object to prevent direc access to thread data

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(thread) ⇒ ThreadInfo

Returns a new instance of ThreadInfo.

Raises:



32
33
34
35
36
37
38
# File 'lib/ruby_jard/thread_info.rb', line 32

def initialize(thread)
  raise RubyJard::Error, 'Expected Thread object or nil' if !thread.is_a?(::Thread) && !thread.nil?

  @thread = thread
  @id = thread&.object_id
  @label = self.class.generate_label_for(@id)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



30
31
32
# File 'lib/ruby_jard/thread_info.rb', line 30

def id
  @id
end

#labelObject (readonly)

Returns the value of attribute label.



30
31
32
# File 'lib/ruby_jard/thread_info.rb', line 30

def label
  @label
end

Class Method Details

.clear_labelsObject



12
13
14
15
# File 'lib/ruby_jard/thread_info.rb', line 12

def clear_labels
  @labels = {}
  @next_label = 0
end

.generate_label_for(id) ⇒ Object



22
23
24
25
26
27
# File 'lib/ruby_jard/thread_info.rb', line 22

def generate_label_for(id)
  return '' if id.nil?
  return labels[id] if labels[id]

  labels[id] = next_label.to_s
end

.labelsObject



8
9
10
# File 'lib/ruby_jard/thread_info.rb', line 8

def labels
  @labels ||= {}
end

.next_labelObject



17
18
19
20
# File 'lib/ruby_jard/thread_info.rb', line 17

def next_label
  @next_label ||= 0
  @next_label += 1
end

Instance Method Details

#==(other) ⇒ Object

rubocop:disable Style/CaseLikeIf



58
59
60
61
62
63
64
65
66
# File 'lib/ruby_jard/thread_info.rb', line 58

def ==(other)
  if other.is_a?(::Thread)
    @thread == other
  elsif other.is_a?(ThreadInfo)
    @id == other.id
  else
    raise RubyJard::Error, 'Invalid comparation'
  end
end

#alive?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/ruby_jard/thread_info.rb', line 49

def alive?
  @thread&.alive? || false
end

#backtrace_locationsObject



53
54
55
# File 'lib/ruby_jard/thread_info.rb', line 53

def backtrace_locations
  @thread&.backtrace_locations || []
end

#nameObject



40
41
42
# File 'lib/ruby_jard/thread_info.rb', line 40

def name
  @thread&.name
end

#statusObject



44
45
46
47
# File 'lib/ruby_jard/thread_info.rb', line 44

def status
  s = @thread&.status
  s == false ? 'exited' : s
end