Sha256: c8a40c77b3e8847570ba87da5e29d71d341f7cabceedad2cafe41b0544aa4d3b

Contents?: true

Size: 1.8 KB

Versions: 24

Compression:

Stored size: 1.8 KB

Contents

module Steep
  module Names
    class Base
      attr_reader :namespace
      attr_reader :name
      attr_reader :location

      def initialize(namespace:, name:, location: nil)
        @namespace = namespace
        @name = name
        @location = location
      end

      def absolute?
        namespace.absolute?
      end

      def relative?
        !absolute?
      end

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

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

      alias eql? ==

      def self.parse(string)
        namespace = AST::Namespace.parse(string.to_s)
        *_, name = namespace.path
        new(namespace: namespace.parent, name: name)
      end

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

      def in_namespace(namespace)
        if absolute?
          self
        else
          self.class.new(namespace: namespace + self.namespace, name: name)
        end
      end

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

    class Module < Base
      def self.from_node(node)
        case node.type
        when :const, :casgn
          namespace = namespace_from_node(node.children[0]) or return
          name = node.children[1]
          new(namespace: namespace, name: name)
        end
      end

      def self.namespace_from_node(node)
        case node&.type
        when nil
          AST::Namespace.empty
        when :cbase
          AST::Namespace.root
        when :const
          namespace_from_node(node.children[0])&.yield_self do |parent|
            parent.append(node.children[1])
          end
        end
      end
    end

    class Interface < Base
    end

    class Alias < Base
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

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