Sha256: ca28a56ab24637a1289563d40dac4e37d12235e728ef149790c7879500e54880

Contents?: true

Size: 1.31 KB

Versions: 6

Compression:

Stored size: 1.31 KB

Contents

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

      def lookup(name)
        nil
      end
    end

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

      def names
        []
      end

      def root
        self
      end
    end

    attr_reader :parent

    def initialize(name, 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, parent = self)
    end

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

    def singleton
      Scope.new(@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

6 entries across 6 versions & 1 rubygems

Version Path
method_log-0.2.1 lib/method_log/scope.rb
method_log-0.2.0 lib/method_log/scope.rb
method_log-0.1.1 lib/method_log/scope.rb
method_log-0.1.0 lib/method_log/scope.rb
method_log-0.0.7 lib/method_log/scope.rb
method_log-0.0.6 lib/method_log/scope.rb