module Pantheios::Util::ThreadUtil::ThreadName

Inclusion module for giving the included type the thread_name attribute

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

Public Class Methods

included(receiver) click to toggle source
# File lib/pantheios/util/thread_util.rb, line 42
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