Sha256: e885fc252f328f5d4c49f3cacedf29e482deb321caef3ec52cc49d43f56e10f7

Contents?: true

Size: 1.63 KB

Versions: 25

Compression:

Stored size: 1.63 KB

Contents

module RBS
  class TypeName
    attr_reader :namespace
    attr_reader :name
    attr_reader :kind

    def initialize(namespace:, name:)
      @namespace = namespace
      @name = name
      @kind = case name.to_s[0,1]
              when /[A-Z]/
                :class
              when /[a-z]/
                :alias
              when "_"
                :interface
              else
                # Defaults to :class
                :class
              end
    end

    def ==(other)
      other.is_a?(self.class) && other.namespace == namespace && other.name == name
    end

    alias eql? ==

    def hash
      self.class.hash ^ namespace.hash ^ name.hash
    end

    def to_s
      "#{namespace.to_s}#{name}"
    end

    def to_json(state = _ = nil)
      to_s.to_json(state)
    end

    def to_namespace
      namespace.append(self.name)
    end

    def class?
      kind == :class
    end

    def alias?
      kind == :alias
    end

    def absolute!
      self.class.new(namespace: namespace.absolute!, name: name)
    end

    def absolute?
      namespace.absolute?
    end

    def relative!
      self.class.new(namespace: namespace.relative!, name: name)
    end

    def interface?
      kind == :interface
    end

    def with_prefix(namespace)
      self.class.new(namespace: namespace + self.namespace, name: name)
    end
  end
end

module Kernel
  def TypeName(string)
    absolute = string.start_with?("::")

    *path, name = string.delete_prefix("::").split("::").map(&:to_sym)
    raise unless name

    RBS::TypeName.new(
      name: name,
      namespace: RBS::Namespace.new(path: path, absolute: absolute)
    )
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
rbs-2.1.0 lib/rbs/type_name.rb
rbs-2.0.0 lib/rbs/type_name.rb
rbs-2.0.0.pre2 lib/rbs/type_name.rb
rbs-2.0.0.pre1 lib/rbs/type_name.rb
rbs-1.8.1 lib/rbs/type_name.rb
rbs-1.8.0 lib/rbs/type_name.rb
rbs-1.7.1 lib/rbs/type_name.rb
rbs-1.7.0 lib/rbs/type_name.rb
rbs-1.7.0.beta.5 lib/rbs/type_name.rb
rbs-1.7.0.beta.4 lib/rbs/type_name.rb
rbs-1.7.0.beta.3 lib/rbs/type_name.rb
rbs-1.7.0.beta.2 lib/rbs/type_name.rb
rbs-1.7.0.beta.1 lib/rbs/type_name.rb
rbs-1.6.2 lib/rbs/type_name.rb
rbs-1.6.1 lib/rbs/type_name.rb
rbs-1.6.0 lib/rbs/type_name.rb
rbs-1.5.1 lib/rbs/type_name.rb
rbs-1.5.0 lib/rbs/type_name.rb
rbs-1.4.0 lib/rbs/type_name.rb
rbs-1.3.3 lib/rbs/type_name.rb