Class: AdHocTemplate::DataLoader

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, tag_formatter = DefaultTagFormatter.new) ⇒ DataLoader

Returns a new instance of DataLoader.



29
30
31
32
# File 'lib/ad_hoc_template.rb', line 29

def initialize(record, tag_formatter=DefaultTagFormatter.new)
  @record = record
  @tag_formatter = tag_formatter
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



27
28
29
# File 'lib/ad_hoc_template.rb', line 27

def record
  @record
end

#tag_formatterObject (readonly)

Returns the value of attribute tag_formatter.



27
28
29
# File 'lib/ad_hoc_template.rb', line 27

def tag_formatter
  @tag_formatter
end

Class Method Details

.format(template, record, tag_formatter = DefaultTagFormatter.new) ⇒ Object



13
14
15
16
17
18
# 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) ⇒ Object



20
21
22
23
24
25
# 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

Instance Method Details

#format(tree, memo = nil) ⇒ Object



62
63
64
# 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) ⇒ Object



49
50
51
52
53
54
55
# 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) ⇒ Object



57
58
59
60
# 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) ⇒ Object



66
67
68
# File 'lib/ad_hoc_template.rb', line 66

def new_with_record(record)
  self.class.new(record, @tag_formatter)
end

#visit(tree, memo) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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