Class: Diecut::TemplateReducer
- Inherits:
-
Object
- Object
- Diecut::TemplateReducer
- Defined in:
- lib/diecut/template-reducer.rb
Instance Method Summary collapse
- #fields ⇒ Object
-
#initialize(tokens) ⇒ TemplateReducer
constructor
A new instance of TemplateReducer.
- #leaf_fields ⇒ Object
- #leaves_and_nodes ⇒ Object
- #node_fields ⇒ Object
- #partials ⇒ Object
- #process(prefix, tokens) ⇒ Object
- #sections ⇒ Object
- #unknown ⇒ Object
-
#validate ⇒ Object
XXX Used as a section and as a field is different from used as a field and as parent of a field.
Constructor Details
#initialize(tokens) ⇒ TemplateReducer
Returns a new instance of TemplateReducer.
3 4 5 |
# File 'lib/diecut/template-reducer.rb', line 3 def initialize(tokens) @tokens = tokens end |
Instance Method Details
#fields ⇒ Object
7 8 9 10 |
# File 'lib/diecut/template-reducer.rb', line 7 def fields process([], [@tokens]) if @fields.nil? @fields.keys end |
#leaf_fields ⇒ Object
27 28 29 30 |
# File 'lib/diecut/template-reducer.rb', line 27 def leaf_fields leaves_and_nodes if @leaf_fields.nil? return @leaf_fields end |
#leaves_and_nodes ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/diecut/template-reducer.rb', line 37 def leaves_and_nodes @node_fields, @leaf_fields = fields.partition.with_index do |field, idx| fields.drop(idx + 1).any? do |other| field.zip(other).all? {|l,r| l==r} end end end |
#node_fields ⇒ Object
32 33 34 35 |
# File 'lib/diecut/template-reducer.rb', line 32 def node_fields leaves_and_nodes if @node_fields.nil? return @node_fields end |
#partials ⇒ Object
17 18 19 20 |
# File 'lib/diecut/template-reducer.rb', line 17 def partials process([], [@tokens]) if @partials.nil? @partials.keys end |
#process(prefix, tokens) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/diecut/template-reducer.rb', line 56 def process(prefix, tokens) @fields ||= {} @partials ||= {} @unknown ||= [] @sections ||= {} tokens.each do |token| case token[0] when :multi process(prefix, token[1..-1]) when :static when :mustache case token[1] when :etag, :utag process(prefix, [token[2]]) when :section, :inverted_section @sections[prefix + token[2][2]] = true process(prefix, [token[2]]) process(prefix + token[2][2], [token[4]]) when :partial @partials[[token[2], prefix]] = true when :fetch @fields[prefix + token[2]] = true else @unknown << token end else @unknown << token end end end |
#sections ⇒ Object
12 13 14 15 |
# File 'lib/diecut/template-reducer.rb', line 12 def sections process([], [@tokens]) if @sections.nil? @sections.keys end |
#unknown ⇒ Object
22 23 24 25 |
# File 'lib/diecut/template-reducer.rb', line 22 def unknown process([], [@tokens]) if @unknown.nil? @unknown end |
#validate ⇒ Object
XXX Used as a section and as a field is different from used as a field and as parent of a field. Tricky tricky
48 49 50 51 52 53 54 |
# File 'lib/diecut/template-reducer.rb', line 48 def validate not_sections = node_fields.find_all {|node| !sections.include?(node)} unless not_sections.empty? warn "These fields are referenced directly, and as the parent of another field:\n" + not_sections.map{|sec| sec.join(".")}.join("\n") end end |