Module: Necropsy::EmbeddedRuby

Defined in:
lib/necropsy/embedded_ruby.rb

Constant Summary collapse

TAG_PATTERN =
/<%(?![%#])[-=]?(.*?)-?%>/m

Class Method Summary collapse

Class Method Details

.call_names(source) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/necropsy/embedded_ruby.rb', line 24

def call_names(source)
  result = Prism.parse(extract(source))
  return Set.new if result.failure?

  names = Set.new
  pending = [result.value]
  until pending.empty?
    node = pending.pop
    names << node.name.to_s if node.is_a?(Prism::CallNode) && node.name
    pending.concat(node.child_nodes.compact)
  end
  names
end

.extract(source) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/necropsy/embedded_ruby.rb', line 11

def extract(source)
  cursor = 0
  output = +''
  source.to_enum(:scan, TAG_PATTERN).each do
    match = Regexp.last_match
    output << blank(source[cursor...match.begin(0)])
    output << strip_ruby_comments(match[1])
    cursor = match.end(0)
  end
  output << blank(source[cursor..])
  output
end