Module: Docscribe::Infer::Names

Defined in:
lib/docscribe/infer/names.rb

Overview

Constant-name helpers for turning AST nodes into fully qualified names.

Class Method Summary collapse

Class Method Details

.build_const_full_name(node) ⇒ String

Note:

module_function: when included, also defines # (instance visibility: private)

Build the fully qualified name from a ‘:const` node.

Parameters:

  • node (Parser::AST::Node)

    a ‘:const` node

Returns:

  • (String)


38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/docscribe/infer/names.rb', line 38

def build_const_full_name(node)
  scope, name = *node
  scope_name = const_full_name(scope)

  if scope_name && !scope_name.empty?
    "#{scope_name}::#{name}"
  elsif scope_name == ''
    "::#{name}"
  else
    name.to_s
  end
end

.const_full_name(node) ⇒ String?

Note:

module_function: when included, also defines #const_full_name (instance visibility: private)

Convert a ‘:const` / `:cbase` AST node into a fully qualified constant name.

Examples:

  • ‘Foo` => `“Foo”`

  • ‘Foo::Bar` => `“Foo::Bar”`

  • ‘::Foo::Bar` => `“::Foo::Bar”`

Returns nil for unsupported nodes.

Parameters:

  • node (Parser::AST::Node, nil)

    constant-like AST node

Returns:

  • (String, nil)


21
22
23
24
25
26
27
28
29
30
# File 'lib/docscribe/infer/names.rb', line 21

def const_full_name(node)
  return nil unless node.is_a?(Parser::AST::Node)

  case node.type
  when :const
    build_const_full_name(node)
  when :cbase
    ''
  end
end