Class: ODFReport::Section

Inherits:
Object
  • Object
show all
Includes:
HashGsub, Nested
Defined in:
lib/odf-report/section.rb

Constant Summary

Constants included from HashGsub

HashGsub::HTML_ESCAPE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HashGsub

#hash_gsub!, #html_escape, #node_hash_gsub!, #odf_linebreak

Methods included from Nested

#get_collection_from_item, #get_fields_with_values, #replace_values!

Constructor Details

#initialize(opts) ⇒ Section

Returns a new instance of Section.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/odf-report/section.rb', line 8

def initialize(opts)
  @name             = opts[:name]
  @collection_field = opts[:collection_field]
  @collection       = opts[:collection]
  @parent           = opts[:parent]

  @fields = {}

  @tables = []
  @sections = []
end

Instance Attribute Details

#collection_fieldObject

Returns the value of attribute collection_field.



6
7
8
# File 'lib/odf-report/section.rb', line 6

def collection_field
  @collection_field
end

#dataObject

Returns the value of attribute data.



6
7
8
# File 'lib/odf-report/section.rb', line 6

def data
  @data
end

#fieldsObject

Returns the value of attribute fields.



6
7
8
# File 'lib/odf-report/section.rb', line 6

def fields
  @fields
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/odf-report/section.rb', line 6

def name
  @name
end

#parentObject

Returns the value of attribute parent.



6
7
8
# File 'lib/odf-report/section.rb', line 6

def parent
  @parent
end

#tablesObject

Returns the value of attribute tables.



6
7
8
# File 'lib/odf-report/section.rb', line 6

def tables
  @tables
end

Instance Method Details

#add_field(name, field = nil, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/odf-report/section.rb', line 20

def add_field(name, field=nil, &block)
  if field
    @fields[name] = lambda { |item| item.send(field)}
  elsif block_given?
    @fields[name] = block
  else
    @fields[name] = lambda { |item| item.send(name)}
  end
end

#add_section(section_name, collection_field, opts = {}) {|sec| ... } ⇒ Object

Yields:

  • (sec)


38
39
40
41
42
43
44
# File 'lib/odf-report/section.rb', line 38

def add_section(section_name, collection_field, opts={}, &block)
  opts.merge!(:name => section_name, :collection_field => collection_field, :parent => self)
  sec = Section.new(opts)
  @sections << sec

  yield(sec)
end

#add_table(table_name, collection_field, opts = {}) {|tab| ... } ⇒ Object

Yields:

  • (tab)


30
31
32
33
34
35
36
# File 'lib/odf-report/section.rb', line 30

def add_table(table_name, collection_field, opts={}, &block)
  opts.merge!(:name => table_name, :collection_field => collection_field, :parent => self)
  tab = Table.new(opts)
  @tables << tab

  yield(tab)
end

#populate!(row) ⇒ Object



46
47
48
# File 'lib/odf-report/section.rb', line 46

def populate!(row)
  @collection = get_collection_from_item(row, @collection_field) if row
end

#replace!(doc, row = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/odf-report/section.rb', line 50

def replace!(doc, row = nil)

  return unless section = find_section_node(doc)

  template = section.dup

  populate!(row)

  @collection.each do |data_item|
    new_section = template.dup

    replace_values!(new_section, data_item)

    @tables.each do |t|
      t.replace!(new_section, data_item)
    end

    @sections.each do |s|
      s.replace!(new_section, data_item)
    end

    section.before(new_section)

  end

  section.remove

end