Sha256: 949428b096c6e470894e5dcef7890afe41091fc8c85934d832cf1697131cb111

Contents?: true

Size: 1.12 KB

Versions: 14

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

require 'set'

module Tapioca
  # Registers a TracePoint immediately upon load to track points at which
  # classes and modules are opened for definition. This is used to track
  # correspondence between classes/modules and files, as this information isn't
  # available in the ruby runtime without extra accounting.
  module ConstantLocator
    @class_files = {}

    NAME = Module.instance_method(:name)
    private_constant :NAME

    # Immediately activated upon load. Observes class/module definition.
    TracePoint.trace(:class) do |tp|
      unless tp.self.singleton_class?
        key = NAME.bind(tp.self).call
        @class_files[key] ||= Set.new
        @class_files[key] << tp.path
      end
    end

    # Returns the files in which this class or module was opened. Doesn't know
    # about situations where the class was opened prior to +require+ing,
    # or where metaprogramming was used via +eval+, etc.
    def files_for(klass)
      name = String === klass ? klass : NAME.bind(klass).call
      files = @class_files[name]
      files || Set.new
    end
    module_function :files_for
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
tapioca-0.4.0 lib/tapioca/constant_locator.rb
tapioca-0.3.1 lib/tapioca/constant_locator.rb
tapioca-0.3.0 lib/tapioca/constant_locator.rb
tapioca-0.2.8 lib/tapioca/constant_locator.rb
tapioca-0.2.7 lib/tapioca/constant_locator.rb
tapioca-0.2.6 lib/tapioca/constant_locator.rb
tapioca-0.2.5 lib/tapioca/constant_locator.rb
tapioca-0.2.4 lib/tapioca/constant_locator.rb
tapioca-0.2.3 lib/tapioca/constant_locator.rb
tapioca-0.2.2 lib/tapioca/constant_locator.rb
tapioca-0.2.1 lib/tapioca/constant_locator.rb
tapioca-0.2.0 lib/tapioca/constant_locator.rb
tapioca-0.1.5 lib/tapioca/constant_locator.rb
tapioca-0.1.4 lib/tapioca/constant_locator.rb