Class: RailsRouteChecker::Parsers::HamlParser::Tree::Node

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rails-route-checker/parsers/haml_parser/tree/node.rb

Direct Known Subclasses

FilterNode, RootNode, ScriptNode, SilentScriptNode, TagNode

Defined Under Namespace

Classes: Siblings

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, parse_node) ⇒ Node

Returns a new instance of Node.



13
14
15
16
17
18
# File 'lib/rails-route-checker/parsers/haml_parser/tree/node.rb', line 13

def initialize(document, parse_node)
  @line = parse_node.line
  @document = document
  @value = parse_node.value
  @type = parse_node.type
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



10
11
12
# File 'lib/rails-route-checker/parsers/haml_parser/tree/node.rb', line 10

def children
  @children
end

#lineObject (readonly)

Returns the value of attribute line.



11
12
13
# File 'lib/rails-route-checker/parsers/haml_parser/tree/node.rb', line 11

def line
  @line
end

#parentObject

Returns the value of attribute parent.



10
11
12
# File 'lib/rails-route-checker/parsers/haml_parser/tree/node.rb', line 10

def parent
  @parent
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/rails-route-checker/parsers/haml_parser/tree/node.rb', line 11

def type
  @type
end

Instance Method Details

#directivesObject



30
31
32
33
34
# File 'lib/rails-route-checker/parsers/haml_parser/tree/node.rb', line 30

def directives
  directives = []
  directives << predecessor.directives if predecessor
  directives.flatten
end

#eachObject



20
21
22
23
24
25
26
27
28
# File 'lib/rails-route-checker/parsers/haml_parser/tree/node.rb', line 20

def each
  return to_enum(__callee__) unless block_given?

  node = self
  loop do
    yield node
    break unless (node = node.next_node)
  end
end

#inspectObject



49
50
51
# File 'lib/rails-route-checker/parsers/haml_parser/tree/node.rb', line 49

def inspect
  "#<#{self.class.name}>"
end

#line_numbersObject



59
60
61
62
63
# File 'lib/rails-route-checker/parsers/haml_parser/tree/node.rb', line 59

def line_numbers
  return (line..line) unless @value && text

  (line..line + lines.count)
end

#linesObject



53
54
55
56
57
# File 'lib/rails-route-checker/parsers/haml_parser/tree/node.rb', line 53

def lines
  return [] unless @value && text

  text.split(/\r\n|\r|\n/)
end

#next_nodeObject



76
77
78
# File 'lib/rails-route-checker/parsers/haml_parser/tree/node.rb', line 76

def next_node
  children.first || successor
end

#predecessorObject



65
66
67
# File 'lib/rails-route-checker/parsers/haml_parser/tree/node.rb', line 65

def predecessor
  siblings.previous(self) || parent
end

#source_codeObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rails-route-checker/parsers/haml_parser/tree/node.rb', line 36

def source_code
  next_node_line =
    if next_node
      next_node.line - 1
    else
      @document.source_lines.count + 1
    end

  @document.source_lines[@line - 1...next_node_line]
           .join("\n")
           .gsub(/^\s*\z/m, '')
end

#subsequentsObject



80
81
82
# File 'lib/rails-route-checker/parsers/haml_parser/tree/node.rb', line 80

def subsequents
  siblings.subsequents(self)
end

#successorObject



69
70
71
72
73
74
# File 'lib/rails-route-checker/parsers/haml_parser/tree/node.rb', line 69

def successor
  next_sibling = siblings.next(self)
  return next_sibling if next_sibling

  parent&.successor
end

#textObject



84
85
86
# File 'lib/rails-route-checker/parsers/haml_parser/tree/node.rb', line 84

def text
  @value[:text].to_s
end