Sha256: b74dd35b8f69800e25f0e50274e23c0e9b35255504a870c906f76fea65b0585d

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module MethodLog
  class Scope
    class Null < Scope
      def initialize
        super(name: 'null')
      end

      def lookup(name)
        nil
      end
    end

    class Root < Scope
      def initialize
        super(name: 'root', parent: Scope::Null.new)
      end

      def names
        []
      end
    end

    attr_reader :parent

    def initialize(name: nil, parent: nil, singleton: false)
      @name = name
      @parent = parent
      @singleton = singleton
      @modules = {}
    end

    def for(modules)
      scope = self
      modules.each do |mod|
        scope = scope.lookup(mod) || scope.define(mod)
      end
      scope
    end

    def define(name)
      @modules[name] = Scope.new(name: name, parent: self)
    end

    def lookup(name)
      @modules.fetch(name) { @parent.lookup(name) }
    end

    def singleton
      Scope.new(name: @name, parent: self, singleton: true)
    end

    def method_identifier(name)
      [names.join('::'), separator, name].join
    end

    protected

    def names
      names = @singleton ? [] : [@name]
      names = @parent.names + names
    end

    private

    def separator
      @singleton ? '.' : '#'
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
method_log-0.0.3 lib/method_log/scope.rb