Class: AdHocTemplate::DataLoader
- Inherits:
-
Object
- Object
- AdHocTemplate::DataLoader
- Defined in:
- lib/ad_hoc_template.rb
Class Method Summary collapse
- .format(template, record, tag_formatter = DefaultTagFormatter.new) ⇒ Object
- .format_multi_records(template, records, tag_formatter = DefaultTagFormatter.new) ⇒ Object
Instance Method Summary collapse
- #format(tree) ⇒ Object
- #format_iteration_tag(tag_node) ⇒ Object
- #format_tag(tag_node) ⇒ Object
-
#initialize(record, tag_formatter = DefaultTagFormatter.new) ⇒ DataLoader
constructor
A new instance of DataLoader.
- #visit(tree) ⇒ Object
Constructor Details
#initialize(record, tag_formatter = DefaultTagFormatter.new) ⇒ DataLoader
44 45 46 47 |
# File 'lib/ad_hoc_template.rb', line 44 def initialize(record, tag_formatter=DefaultTagFormatter.new) @record = record @tag_formatter = tag_formatter end |
Class Method Details
.format(template, record, tag_formatter = DefaultTagFormatter.new) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/ad_hoc_template.rb', line 30 def self.format(template, record, tag_formatter=DefaultTagFormatter.new) if record.kind_of? Array return format_multi_records(template, record, tag_formatter) end new(record, tag_formatter).format(template) end |
.format_multi_records(template, records, tag_formatter = DefaultTagFormatter.new) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/ad_hoc_template.rb', line 37 def self.format_multi_records(template, records, tag_formatter=DefaultTagFormatter.new) records.map do |record| new(record, tag_formatter).format(template) end.join end |
Instance Method Details
#format(tree) ⇒ Object
81 82 83 |
# File 'lib/ad_hoc_template.rb', line 81 def format(tree) tree.accept(self).join end |
#format_iteration_tag(tag_node) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ad_hoc_template.rb', line 62 def format_iteration_tag(tag_node) sub_records = @record[tag_node.type]||[@record] tag_node = Parser::TagNode.new.concat(tag_node.clone) sub_records.map do |record| if tag_node.contains_any_value_assigned_tag_node?(record) data_loader = AdHocTemplate::DataLoader.new(record, @tag_formatter) tag_node.map {|leaf| leaf.accept(data_loader) }.join else "".freeze end end end |
#format_tag(tag_node) ⇒ Object
76 77 78 79 |
# File 'lib/ad_hoc_template.rb', line 76 def format_tag(tag_node) leafs = tag_node.map {|leaf| leaf.accept(self) } @tag_formatter.format(tag_node.type, leafs.join.strip, @record) end |
#visit(tree) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/ad_hoc_template.rb', line 49 def visit(tree) case tree when Parser::IterationTagNode format_iteration_tag(tree) when Parser::TagNode format_tag(tree) when Parser::Leaf tree.join else tree.map {|node| node.accept(self) } end end |