Class: MountainBerryFields::Parser
- Inherits:
-
Erubis::Eruby
- Object
- Erubis::Eruby
- MountainBerryFields::Parser
- Defined in:
- lib/mountain_berry_fields/parser.rb
Overview
This class takes a document with erb in it It parses the erb and gives you back a file of source code When that source code is evaluated, you get the file out. The evaluation environment is expected to provide a ‘document` method or local variable, which is a string that the template will be appended to.
It differs from normal ERB in that you can pass it :visible and :invisible commands lists. Code inside of a block around an invisible command (method name) will not be added to the document. Any command invoked at the start of an erb block (<% and <%=) will be checked against the visible and invisible lists, if they are on either list, their block will return the text in the template that was written in the block.
Defined Under Namespace
Classes: Recording
Instance Attribute Summary collapse
-
#invisible_commands ⇒ Object
Returns the value of attribute invisible_commands.
-
#known_commands ⇒ Object
Returns the value of attribute known_commands.
-
#recordings ⇒ Object
Returns the value of attribute recordings.
-
#visible_commands ⇒ Object
Returns the value of attribute visible_commands.
Instance Method Summary collapse
- #add_expr_literal(src, code) ⇒ Object
- #add_postamble(src) ⇒ Object
- #add_preamble(src) ⇒ Object
- #add_stmt(src, code) ⇒ Object
- #add_text(src, text) ⇒ Object
- #end_command?(code_with_command) ⇒ Boolean
- #init_generator(properties = {}) ⇒ Object
- #known_command?(code_with_command) ⇒ Boolean
- #manage_recording(src, code) ⇒ Object
- #parse ⇒ Object
- #visible_command?(code_with_command) ⇒ Boolean
Instance Attribute Details
#invisible_commands ⇒ Object
Returns the value of attribute invisible_commands.
40 41 42 |
# File 'lib/mountain_berry_fields/parser.rb', line 40 def invisible_commands @invisible_commands end |
#known_commands ⇒ Object
Returns the value of attribute known_commands.
40 41 42 |
# File 'lib/mountain_berry_fields/parser.rb', line 40 def known_commands @known_commands end |
#recordings ⇒ Object
Returns the value of attribute recordings.
40 41 42 |
# File 'lib/mountain_berry_fields/parser.rb', line 40 def recordings @recordings end |
#visible_commands ⇒ Object
Returns the value of attribute visible_commands.
40 41 42 |
# File 'lib/mountain_berry_fields/parser.rb', line 40 def visible_commands @visible_commands end |
Instance Method Details
#add_expr_literal(src, code) ⇒ Object
85 86 87 88 |
# File 'lib/mountain_berry_fields/parser.rb', line 85 def add_expr_literal(src, code) manage_recording src, code super end |
#add_postamble(src) ⇒ Object
54 55 56 57 |
# File 'lib/mountain_berry_fields/parser.rb', line 54 def add_postamble(src) src << "#{@bufvar} << %(\\n) unless #{@bufvar}.end_with? %(\\n);" src << "document << #{@bufvar};" end |
#add_preamble(src) ⇒ Object
50 51 52 |
# File 'lib/mountain_berry_fields/parser.rb', line 50 def add_preamble(src) super end |
#add_stmt(src, code) ⇒ Object
80 81 82 83 |
# File 'lib/mountain_berry_fields/parser.rb', line 80 def add_stmt(src, code) manage_recording src, code super end |
#add_text(src, text) ⇒ Object
63 64 65 66 |
# File 'lib/mountain_berry_fields/parser.rb', line 63 def add_text(src, text) recordings.last.record text super if recordings.last.visible? end |
#end_command?(code_with_command) ⇒ Boolean
76 77 78 |
# File 'lib/mountain_berry_fields/parser.rb', line 76 def end_command?(code_with_command) code_with_command =~ /\A\s*(end|})/ end |
#init_generator(properties = {}) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/mountain_berry_fields/parser.rb', line 42 def init_generator(properties={}) self.recordings = [Recording.new(false)] self.visible_commands = (properties.delete(:visible) || []).map &:to_s self.invisible_commands = (properties.delete(:invisible) || []).map &:to_s self.known_commands = visible_commands + invisible_commands super end |
#known_command?(code_with_command) ⇒ Boolean
68 69 70 |
# File 'lib/mountain_berry_fields/parser.rb', line 68 def known_command?(code_with_command) known_commands.include? code_with_command[/\w+/] end |
#manage_recording(src, code) ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/mountain_berry_fields/parser.rb', line 90 def manage_recording(src, code) if known_command? code recordings << Recording.new(true, visible_command?(code)) elsif end_command? code recording = recordings.pop src << recording.recorded.inspect << ";" if recording.command? else recordings << Recording.new(false) end end |
#parse ⇒ Object
59 60 61 |
# File 'lib/mountain_berry_fields/parser.rb', line 59 def parse src end |
#visible_command?(code_with_command) ⇒ Boolean
72 73 74 |
# File 'lib/mountain_berry_fields/parser.rb', line 72 def visible_command?(code_with_command) visible_commands.include? code_with_command[/\w+/] end |