Class: RailsRouteChecker::Parsers::HamlParser::RubyExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-route-checker/parsers/haml_parser/ruby_extractor.rb

Defined Under Namespace

Classes: RubySource

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ RubyExtractor

Returns a new instance of RubyExtractor.



13
14
15
# File 'lib/rails-route-checker/parsers/haml_parser/ruby_extractor.rb', line 13

def initialize(document)
  @document = document
end

Class Method Details

.extract(document) ⇒ Object



9
10
11
# File 'lib/rails-route-checker/parsers/haml_parser/ruby_extractor.rb', line 9

def self.extract(document)
  new(document).extract
end

Instance Method Details

#extractObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rails-route-checker/parsers/haml_parser/ruby_extractor.rb', line 17

def extract
  @source_lines = []
  @source_map = {}
  @line_count = 0
  @indent_level = 0
  @output_count = 0

  visit_children(document.tree)

  RubySource.new(@source_lines.join("\n"), @source_map)
end

#visit(node) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rails-route-checker/parsers/haml_parser/ruby_extractor.rb', line 71

def visit(node)
  block_called = false

  block = lambda do |descend = :children|
    block_called = true
    visit_children(node) if descend == :children
  end

  case node.type
  when :tag
    visit_tag(node)
  when :script, :silent_script
    visit_script(node, &block)
  when :filter
    visit_filter(node)
  end

  visit_children(node) unless block_called
end

#visit_children(parent) ⇒ Object



91
92
93
# File 'lib/rails-route-checker/parsers/haml_parser/ruby_extractor.rb', line 91

def visit_children(parent)
  parent.children.each { |node| visit(node) }
end

#visit_filter(node) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/rails-route-checker/parsers/haml_parser/ruby_extractor.rb', line 63

def visit_filter(node)
  return unless node.filter_type == 'ruby'

  node.text.split("\n").each_with_index do |line, index|
    add_line(line, node.line + index + 1, false)
  end
end

#visit_script(node) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rails-route-checker/parsers/haml_parser/ruby_extractor.rb', line 47

def visit_script(node)
  code = node.text
  add_line(code.strip, node)

  start_block = anonymous_block?(code) || start_block_keyword?(code)

  @indent_level += 1 if start_block

  yield

  return unless start_block

  @indent_level -= 1
  add_line('end', node)
end

#visit_tag(node) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rails-route-checker/parsers/haml_parser/ruby_extractor.rb', line 29

def visit_tag(node)
  additional_attributes = node.dynamic_attributes_sources

  additional_attributes.each do |attributes_code|
    attributes_code = attributes_code.gsub(/\s*\n\s*/, ' ').strip
    add_line("{}.merge(#{attributes_code.strip})", node)
  end

  if node.hash_attributes? && node.dynamic_attributes_sources.empty?
    normalized_attr_source = node.dynamic_attributes_source[:hash].gsub(/\s*\n\s*/, ' ')

    add_line(normalized_attr_source, node)
  end

  code = node.script.strip
  add_line(code, node) unless code.empty?
end