Sha256: 486cb56155b102f9226aa83a622ebbd3f871c04347f2ba498921eb28d5e722b6

Contents?: true

Size: 1.6 KB

Versions: 6

Compression:

Stored size: 1.6 KB

Contents

module Steep
  module AST
    class Namespace
      attr_reader :path

      def initialize(path:, absolute:)
        @path = path
        @absolute = absolute
      end

      def self.empty
        new(path: [], absolute: false)
      end

      def self.root
        new(path: [], absolute: true)
      end

      def +(other)
        if other.absolute?
          other
        else
          self.class.new(path: path + other.path, absolute: absolute?)
        end
      end

      def append(component)
        self.class.new(path: path + [component], absolute: absolute?)
      end

      def parent
        raise "Parent with empty namespace" if empty?
        self.class.new(path: path.take(path.size - 1), absolute: absolute?)
      end

      def absolute?
        @absolute
      end

      def relative?
        !absolute?
      end

      def absolute!
        self.class.new(path: path, absolute: true)
      end

      def empty?
        path.empty?
      end

      def ==(other)
        other.is_a?(Namespace) && other.path == path && other.absolute? == absolute?
      end

      alias eql? ==

      def hash
        self.class.hath ^ path.hash ^ absolute?.hash
      end

      def to_s
        if empty?
          absolute? ? "::" : ""
        else
          s = path.join("::")
          absolute? ? "::#{s}::" : "#{s}::"
        end
      end

      def self.parse(string)
        if string.start_with?("::")
          new(path: string.split("::").drop(1).map(&:to_sym), absolute: true)
        else
          new(path: string.split("::").map(&:to_sym), absolute: false)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
steep-0.8.2 lib/steep/ast/namespace.rb
steep-0.8.1 lib/steep/ast/namespace.rb
steep-0.8.0 lib/steep/ast/namespace.rb
steep-0.7.1 lib/steep/ast/namespace.rb
steep-0.7.0 lib/steep/ast/namespace.rb
steep-0.6.0 lib/steep/ast/namespace.rb