Module: Pantheios::Util::ThreadUtil::ThreadName

Defined in:
lib/pantheios/util/thread_util.rb

Overview

Inclusion module for giving the included type the thread_name attribute

If included into a thread type, or a thread instance, then

Class Method Summary collapse

Class Method Details

.included(receiver) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pantheios/util/thread_util.rb', line 40

def self.included receiver

  if receiver < ::Thread

    receiver.instance_eval do

      define_method(:thread_name) { @thread_name || self.to_s }
      define_method(:thread_name=) { |name| @thread_name = name }
    end
  else

    receiver.instance_eval do

      define_method :thread_name do |name = (name_not_given_ = true)|

        t = Thread.current

        has_tn = t.respond_to? :thread_name

        if name_not_given_

          return t.thread_name if has_tn

          t.to_s
        else

          class << t; attr_accessor :thread_name; end unless has_tn

          t.thread_name = name
        end
      end

      define_method(:thread_name=) { |name| thread_name name }
    end
  end
end