Sha256: b12217089516b63a52ad2091d8e031bf3b0fc3cef3d9dc270b65efaa51678e47

Contents?: true

Size: 1.85 KB

Versions: 3

Compression:

Stored size: 1.85 KB

Contents

module Ruby
  module Signature
    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 relative!
        self.class.new(path: path, absolute: false)
      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 split
        [parent, path.last]
      end

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

      def to_type_name
        parent, name = split
        TypeName.new(name: name, namespace: parent)
      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

3 entries across 3 versions & 1 rubygems

Version Path
steep-0.14.0 vendor/ruby-signature/lib/ruby/signature/namespace.rb
steep-0.13.0 vendor/ruby-signature/lib/ruby/signature/namespace.rb
steep-0.12.0 vendor/ruby-signature/lib/ruby/signature/namespace.rb