Class: Metz::TemplateRubyExtractor::Walker

Inherits:
Object
  • Object
show all
Defined in:
lib/metz/template_ruby_extractor.rb

Overview

Walks lines of a template, dispatching each line to the owner module's match patterns. Holds the running byte offset, the line index, and the accumulating snippet list so the owner module can stay stateless.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, processed_source) ⇒ Walker

Returns a new instance of Walker.



53
54
55
56
57
58
# File 'lib/metz/template_ruby_extractor.rb', line 53

def initialize(owner, processed_source)
  @owner = owner
  @processed_source = processed_source
  @cursor = Cursor.new(processed_source.raw_source.lines)
  walk
end

Instance Attribute Details

#snippetsObject (readonly)

Returns the value of attribute snippets.



60
61
62
# File 'lib/metz/template_ruby_extractor.rb', line 60

def snippets
  @snippets
end

Instance Method Details

#build_filter_snippet(body_lines, body_offset) ⇒ Object



114
115
116
117
118
# File 'lib/metz/template_ruby_extractor.rb', line 114

def build_filter_snippet(body_lines, body_offset)
  return nil if body_lines.empty?

  @owner.build_snippet(body_lines.join, body_offset, @processed_source)
end

#collect_filter_body(base_indent) ⇒ Object



97
98
99
100
101
# File 'lib/metz/template_ruby_extractor.rb', line 97

def collect_filter_body(base_indent)
  body = []
  body << @cursor.peek(body.size + 1) while filter_member?(body.size + 1, base_indent)
  body
end

#consume_filter(match) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/metz/template_ruby_extractor.rb', line 89

def consume_filter(match)
  body_offset = @cursor.offset + @cursor.line.length
  body_lines = collect_filter_body(match[:indent].length)
  snippet = build_filter_snippet(body_lines, body_offset)
  @cursor.advance(1 + body_lines.size)
  snippet
end

#consume_inline(match) ⇒ Object



76
77
78
79
80
# File 'lib/metz/template_ruby_extractor.rb', line 76

def consume_inline(match)
  snippet = inline_snippet(match)
  @cursor.advance(1)
  snippet
end

#filter_member?(lookahead, base_indent) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
107
108
# File 'lib/metz/template_ruby_extractor.rb', line 103

def filter_member?(lookahead, base_indent)
  line = @cursor.peek(lookahead)
  return false if line.nil?

  line.match?(/\A\s*\z/) || leading_indent(line) > base_indent
end

#inline_snippet(match) ⇒ Object



82
83
84
85
86
87
# File 'lib/metz/template_ruby_extractor.rb', line 82

def inline_snippet(match)
  return nil if match.nil?

  body_offset = @cursor.offset + match.begin(:body)
  @owner.build_snippet(match[:body], body_offset, @processed_source)
end

#leading_indent(line) ⇒ Object



110
111
112
# File 'lib/metz/template_ruby_extractor.rb', line 110

def leading_indent(line)
  line[/\A[ \t]*/].length
end

#next_snippetObject



68
69
70
71
72
73
74
# File 'lib/metz/template_ruby_extractor.rb', line 68

def next_snippet
  line = @cursor.line
  filter = line.match(@owner::FILTER_LINE)
  return consume_filter(filter) if filter

  consume_inline(line.match(@owner::EXPR_LINE) || line.match(@owner::STMT_LINE))
end

#walkObject



62
63
64
65
66
# File 'lib/metz/template_ruby_extractor.rb', line 62

def walk
  @snippets = []
  @snippets << next_snippet until @cursor.done?
  @snippets.compact!
end