Sha256: 4f78163bf08a84b72ca93884060dcde2c786e79acb91bdfc73fc1a2b500bca16

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 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

      def root
        self
      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
      if modules.first == :root
        scope = root
        modules.unshift
      end
      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

    def root
      parent.root
    end

    protected

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

    private

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
method_log-0.0.5 lib/method_log/scope.rb
method_log-0.0.4 lib/method_log/scope.rb