Sha256: 4891f5229238461b5d11e528bd78285d5fc47d27fdf46d6cab8b39a9450c192c

Contents?: true

Size: 959 Bytes

Versions: 26

Compression:

Stored size: 959 Bytes

Contents

module Foobara
  module ThreadParent
    module ThreadClassExtensions
      def new(...)
        super.tap { |thread| thread.instance_variable_set("@foobara_parent", Thread.current) }
      end
    end
  end
end

class Thread
  class << self
    prepend(Foobara::ThreadParent::ThreadClassExtensions)

    def foobara_var_get(...)
      Thread.current.foobara_var_get(...)
    end

    def foobara_var_set(...)
      Thread.current.foobara_var_set(...)
    end
  end

  attr_reader :foobara_parent

  # NOTE: because there's not a way to unset a thread variable, storing nil is used as deletion.
  # this means that a thread local variable with nil can't have any semantic meaning and should be
  # treated the same as if #thread_variable? had returned false.
  def foobara_var_get(...)
    value = thread_variable_get(...)

    value.nil? ? foobara_parent&.foobara_var_get(...) : value
  end

  def foobara_var_set(...)
    thread_variable_set(...)
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
foobara-0.0.26 projects/thread_parent/src/thread_parent.rb
foobara-0.0.25 projects/thread_parent/src/thread_parent.rb
foobara-0.0.24 projects/thread_parent/src/thread_parent.rb
foobara-0.0.23 projects/thread_parent/src/thread_parent.rb
foobara-0.0.22 projects/thread_parent/src/thread_parent.rb
foobara-0.0.21 projects/thread_parent/src/thread_parent.rb
foobara-0.0.20 projects/thread_parent/src/thread_parent.rb
foobara-0.0.19 projects/thread_parent/src/thread_parent.rb
foobara-0.0.18 projects/thread_parent/src/thread_parent.rb
foobara-0.0.17 projects/thread_parent/src/thread_parent.rb
foobara-0.0.16 projects/thread_parent/src/thread_parent.rb
foobara-0.0.15 projects/thread_parent/src/thread_parent.rb
foobara-0.0.14 projects/thread_parent/src/thread_parent.rb
foobara-0.0.13 projects/thread_parent/src/thread_parent.rb
foobara-0.0.12 projects/thread_parent/src/thread_parent.rb
foobara-0.0.11 projects/thread_parent/src/thread_parent.rb
foobara-0.0.10 projects/thread_parent/src/thread_parent.rb
foobara-0.0.9 projects/thread_parent/src/thread_parent.rb
foobara-0.0.8 projects/thread_parent/src/thread_parent.rb
foobara-0.0.7 projects/thread_parent/src/thread_parent.rb