Class: Rucoa::Nodes::ConstNode

Inherits:
Base
  • Object
show all
Defined in:
lib/rucoa/nodes/const_node.rb

Instance Method Summary collapse

Methods inherited from Base

#ancestors, #child_nodes, #descendant_nodes, #each_ancestor, #each_child_node, #each_descendant_node, #include_position?, #initialize, #namespace, #next_sibling_nodes, #parent, #parent=, #previous_sibling_nodes, #updated

Constructor Details

This class inherits a constructor from Rucoa::Nodes::Base

Instance Method Details

#chained_nameString

Examples:

returns “A” for “A”

node = Rucoa::Source.new(
  content: <<~RUBY,
    A
  RUBY
  uri: 'file:///path/to/a.rb'
).root_node
expect(node.chained_name).to eq('A')

returns “A::B” for “A::B”

node = Rucoa::Source.new(
  content: <<~RUBY,
    A::B
  RUBY
  uri: 'file:///path/to/a.rb'
).node_at(
  Rucoa::Position.new(
    column: 4,
    line: 1
  )
)
expect(node.chained_name).to eq('A::B')

Returns:

  • (String)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rucoa/nodes/const_node.rb', line 28

def chained_name
  case receiver
  when Nodes::CbaseNode
    [
      '',
      name
    ].join('::')
  when Nodes::ConstNode
    [
      receiver.chained_name,
      name
    ].join('::')
  else
    name
  end
end

#module_nestingArray<String>

Examples:

return [“Bar::Foo”, “Foo”] for class Foo::Bar::Baz

node = Rucoa::Source.new(
  content: <<~RUBY,
    module Foo
      module Bar
        module Baz
        end
      end
    end
  RUBY
  uri: 'file:///path/to/foo/bar/baz.rb'
).node_at(
  Rucoa::Position.new(
    column: 4,
    line: 3
  )
)
expect(node.module_nesting).to eq(['Foo::Bar', 'Foo'])

Returns:

  • (Array<String>)


64
65
66
# File 'lib/rucoa/nodes/const_node.rb', line 64

def module_nesting
  each_ancestor(:class, :module).map(&:qualified_name)
end

#nameString

Examples:

returns “A” for “A”

node = Rucoa::Source.new(
  content: <<~RUBY,
    A
  RUBY
  uri: 'file:///path/to/a.rb'
).root_node
expect(node.name).to eq('A')

returns “B” for “A::B”

node = Rucoa::Source.new(
  content: <<~RUBY,
    A::B
  RUBY
  uri: 'file:///path/to/a.rb'
).node_at(
  Rucoa::Position.new(
    column: 4,
    line: 1
  )
)
expect(node.name).to eq('B')

Returns:

  • (String)


90
91
92
# File 'lib/rucoa/nodes/const_node.rb', line 90

def name
  children[1].to_s
end