class AdHocTemplate::DataLoader

Attributes

record[R]
tag_formatter[R]

Public Class Methods

format(template, record, tag_formatter=DefaultTagFormatter.new) click to toggle source
# File lib/ad_hoc_template.rb, line 13
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) click to toggle source
# File lib/ad_hoc_template.rb, line 20
def self.format_multi_records(template, records,
                              tag_formatter=DefaultTagFormatter.new)
  records.map do |record|
    new(record, tag_formatter).format(template)
  end.join
end
new(record, tag_formatter=DefaultTagFormatter.new) click to toggle source
# File lib/ad_hoc_template.rb, line 29
def initialize(record, tag_formatter=DefaultTagFormatter.new)
  @record = record
  @tag_formatter = tag_formatter
end

Public Instance Methods

format(tree, memo=nil) click to toggle source
# File lib/ad_hoc_template.rb, line 62
def format(tree, memo=nil)
  tree.accept(self, memo).join
end
format_iteration_tag(iteration_tag_node, data_loader, memo) click to toggle source
# File lib/ad_hoc_template.rb, line 49
def format_iteration_tag(iteration_tag_node, data_loader, memo)
  tag_node = iteration_tag_node.cast

  prepare_sub_records(iteration_tag_node, data_loader).map do |record|
    format_sub_nodes(tag_node, record, data_loader, memo)
  end
end
format_value_tag(tag_node, data_loader, memo) click to toggle source
# File lib/ad_hoc_template.rb, line 57
def format_value_tag(tag_node, data_loader, memo)
  leafs = tag_node.format_sub_nodes(data_loader, memo).strip
  data_loader.tag_formatter.format(tag_node.type, leafs, data_loader.record)
end
new_with_record(record) click to toggle source
# File lib/ad_hoc_template.rb, line 66
def new_with_record(record)
  self.class.new(record, @tag_formatter)
end
visit(tree, memo) click to toggle source
# File lib/ad_hoc_template.rb, line 34
def visit(tree, memo)
  case tree
  when Parser::IterationNode
    format_iteration_tag(tree, self, memo)
  when Parser::FallbackNode
    ''
  when Parser::ValueNode
    format_value_tag(tree, self, memo)
  when Parser::Leaf
    tree.join
  else
    tree.map {|node| node.accept(self, memo) }
  end
end

Protected Instance Methods

sub_records(tag_node) click to toggle source
# File lib/ad_hoc_template.rb, line 72
def sub_records(tag_node)
  @record[tag_node.type] || [@record]
end

Private Instance Methods

format_fallback_tags(fallback_nodes, data_loader, memo) click to toggle source
# File lib/ad_hoc_template.rb, line 109
def format_fallback_tags(fallback_nodes, data_loader, memo)
  fallback_nodes.map do |fallback_node|
    fallback_node.format_sub_nodes(data_loader, memo)
  end
end
format_sub_nodes(tag_node, record, data_loader, memo) click to toggle source
# File lib/ad_hoc_template.rb, line 98
def format_sub_nodes(tag_node, record, data_loader, memo)
  if tag_node.contains_any_value_assigned_tag_node?(record)
    tag_node.format_sub_nodes(data_loader.new_with_record(record), memo)
  elsif tag_node.contains_any_fallback_tag?
    format_fallback_tags(tag_node.select_fallback_nodes,
                         data_loader.new_with_record(record), memo)
  else
    ''
  end
end
merge_inner_iteration_records(record, inner_labels, data_loader) click to toggle source
# File lib/ad_hoc_template.rb, line 86
def merge_inner_iteration_records(record, inner_labels, data_loader)
  new_record = nil
  inner_labels.each do |label|
    inner_data = data_loader.record[label.full_label(record)]
    if inner_data
      new_record ||= record.dup
      new_record[label.inner_label] = inner_data
    end
  end
  new_record || record
end
prepare_sub_records(tag_node, data_loader) click to toggle source
# File lib/ad_hoc_template.rb, line 78
def prepare_sub_records(tag_node, data_loader)
  inner_labels = tag_node.inner_labels
  return data_loader.sub_records(tag_node) unless inner_labels
  data_loader.sub_records(tag_node).map do |record|
    merge_inner_iteration_records(record, inner_labels, data_loader)
  end
end