Class: Yoda::Parsing::NodeObjects::ConstNode

Inherits:
Object
  • Object
show all
Defined in:
lib/yoda/parsing/node_objects/const_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ ConstNode

Returns a new instance of ConstNode.

Parameters:



9
10
11
12
# File 'lib/yoda/parsing/node_objects/const_node.rb', line 9

def initialize(node)
  fail ArgumentError, node unless node.is_a?(AST::Node) && node.type == :const
  @node = node
end

Instance Attribute Details

#nodeObject (readonly)

Parameters:



6
7
8
# File 'lib/yoda/parsing/node_objects/const_node.rb', line 6

def node
  @node
end

Instance Method Details

#absolute?true, false

Returns:

  • (true, false)


20
21
22
# File 'lib/yoda/parsing/node_objects/const_node.rb', line 20

def absolute?
  node.children.first == :cbase
end

#just_after_separator?(location) ⇒ true, false

Parameters:

Returns:

  • (true, false)


26
27
28
29
# File 'lib/yoda/parsing/node_objects/const_node.rb', line 26

def just_after_separator?(location)
  return false unless node.location.double_colon
  location == Location.of_ast_location(node.location.double_colon.end)
end

#parent_constConstNode?

Returns:



15
16
17
# File 'lib/yoda/parsing/node_objects/const_node.rb', line 15

def parent_const
  node.children.first && node.children.first.type == :const ? ConstNode.new(node.children.first) : nil
end

#to_pathModel::Path

Returns:



32
33
34
# File 'lib/yoda/parsing/node_objects/const_node.rb', line 32

def to_path
  Model::Path.new(to_s)
end

#to_s(base = nil) ⇒ String

Parameters:

  • base (String, Symbol, nil) (defaults to: nil)

Returns:

  • (String)


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/yoda/parsing/node_objects/const_node.rb', line 38

def to_s(base = nil)
  fail ArgumentError, base unless !base || base.is_a?(String) || base.is_a?(Symbol)
  paths = []
  looking_node = node
  while true
    return (base ? base.to_s + '::' : '') + paths.join('::') unless looking_node
    return '::' + paths.join('::') if looking_node.type == :cbase
    paths.unshift(looking_node.children[1])
    looking_node = looking_node.children[0]
  end
end