Class: ODFReport::Section
- Inherits:
-
Object
- Object
- ODFReport::Section
- Defined in:
- lib/odf-report/section.rb
Constant Summary
Constants included from HashGsub
Instance Attribute Summary collapse
-
#collection_field ⇒ Object
Returns the value of attribute collection_field.
-
#data ⇒ Object
Returns the value of attribute data.
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#tables ⇒ Object
Returns the value of attribute tables.
Instance Method Summary collapse
- #add_field(name, field = nil, &block) ⇒ Object
- #add_section(section_name, collection_field, opts = {}) {|sec| ... } ⇒ Object
- #add_table(table_name, collection_field, opts = {}) {|tab| ... } ⇒ Object
-
#initialize(opts) ⇒ Section
constructor
A new instance of Section.
- #populate!(row) ⇒ Object
- #replace!(doc, row = nil) ⇒ Object
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_field ⇒ Object
Returns the value of attribute collection_field.
6 7 8 |
# File 'lib/odf-report/section.rb', line 6 def collection_field @collection_field end |
#data ⇒ Object
Returns the value of attribute data.
6 7 8 |
# File 'lib/odf-report/section.rb', line 6 def data @data end |
#fields ⇒ Object
Returns the value of attribute fields.
6 7 8 |
# File 'lib/odf-report/section.rb', line 6 def fields @fields end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/odf-report/section.rb', line 6 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
6 7 8 |
# File 'lib/odf-report/section.rb', line 6 def parent @parent end |
#tables ⇒ Object
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
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
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 |