Sha256: dde9cfdf043d737482c6b1383876f9e6e64e6905863cf0d5a7cd3570bcacce01

Contents?: true

Size: 1.6 KB

Versions: 24

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.hash ^ 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

24 entries across 24 versions & 1 rubygems

Version Path
steep-0.28.0 lib/steep/ast/namespace.rb
steep-0.27.0 lib/steep/ast/namespace.rb
steep-0.25.0 lib/steep/ast/namespace.rb
steep-0.24.0 lib/steep/ast/namespace.rb
steep-0.23.0 lib/steep/ast/namespace.rb
steep-0.22.0 lib/steep/ast/namespace.rb
steep-0.21.0 lib/steep/ast/namespace.rb
steep-0.20.0 lib/steep/ast/namespace.rb
steep-0.19.0 lib/steep/ast/namespace.rb
steep-0.18.0 lib/steep/ast/namespace.rb
steep-0.17.1 lib/steep/ast/namespace.rb
steep-0.17.0 lib/steep/ast/namespace.rb
steep-0.16.3 lib/steep/ast/namespace.rb
steep-0.16.2 lib/steep/ast/namespace.rb
steep-0.16.1 lib/steep/ast/namespace.rb
steep-0.16.0 lib/steep/ast/namespace.rb
steep-0.15.0 lib/steep/ast/namespace.rb
steep-0.14.0 lib/steep/ast/namespace.rb
steep-0.13.0 lib/steep/ast/namespace.rb
steep-0.12.0 lib/steep/ast/namespace.rb