Navigation Menu

Skip to content

Commit

Permalink
Abstract all the field/column logic into a Field class
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrods committed Dec 28, 2011
1 parent e8b043b commit 9c7a48e
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 54 deletions.
5 changes: 3 additions & 2 deletions lib/odf-report.rb
Expand Up @@ -4,8 +4,9 @@
require 'nokogiri'

require File.expand_path('../odf-report/images', __FILE__)
require File.expand_path('../odf-report/file', __FILE__)
require File.expand_path('../odf-report/hash_gsub', __FILE__)
require File.expand_path('../odf-report/field', __FILE__)
require File.expand_path('../odf-report/file', __FILE__)
require File.expand_path('../odf-report/fields', __FILE__)
require File.expand_path('../odf-report/nested', __FILE__)
require File.expand_path('../odf-report/section', __FILE__)
require File.expand_path('../odf-report/table', __FILE__)
Expand Down
41 changes: 41 additions & 0 deletions lib/odf-report/field.rb
@@ -0,0 +1,41 @@
module ODFReport

class Field

DELIMITERS = ['[', ']']

def initialize(opts, &block)
@name = opts[:name]

unless @value = opts[:value]

if data_field = opts[:data_field]
@block = lambda { |item| item.send(data_field)}
elsif block_given?
@block = block
else
@block = lambda { |item| item.send(@name)}
end

end

end

def get_value(data_item = nil)

raise "Unable to retrieve value" unless @value || data_item

@value || @block.call(data_item) || ''
end

def to_placeholder
if DELIMITERS.is_a?(Array)
"#{DELIMITERS[0]}#{@name.to_s.upcase}#{DELIMITERS[1]}"
else
"#{DELIMITERS}#{@name.to_s.upcase}#{DELIMITERS}"
end
end

end

end
31 changes: 19 additions & 12 deletions lib/odf-report/hash_gsub.rb → lib/odf-report/fields.rb
@@ -1,13 +1,26 @@
module ODFReport

module HashGsub
module Fields

def hash_gsub!(_text, hash_of_values)
hash_of_values.each do |key, val|
txt = html_escape(val)
txt = odf_linebreak(txt)
_text.gsub!("[#{key.to_s.upcase}]", txt)
def field_replace!(_node, data_item = nil)

txt = _node.inner_html

@fields.each do |f|
val = f.get_value(data_item)
txt.gsub!(f.to_placeholder, sanitize(val))
end

_node.inner_html = txt

end

private

def sanitize(txt)
txt = html_escape(txt)
txt = odf_linebreak(txt)
txt
end

HTML_ESCAPE = { '&' => '&amp;', '>' => '&gt;', '<' => '&lt;', '"' => '&quot;' }
Expand All @@ -22,12 +35,6 @@ def odf_linebreak(s)
s.to_s.gsub("\n", "<text:line-break/>")
end

def node_hash_gsub!(_node, hash_of_values)
txt = _node.inner_html
hash_gsub!(txt, hash_of_values)
_node.inner_html = txt
end

end

end
14 changes: 2 additions & 12 deletions lib/odf-report/nested.rb
Expand Up @@ -2,18 +2,8 @@ module ODFReport

module Nested

def replace_values!(new_section, data_item)
node_hash_gsub!(new_section, get_fields_with_values(data_item))
end

def get_fields_with_values(data_item)

fields_with_values = {}
@fields.each do |field_name, block1|
fields_with_values[field_name] = block1.call(data_item) || ''
end

fields_with_values
def replace_fields!(new_section, data_item)
field_replace!(new_section, data_item)
end

def get_collection_from_item(item, collection_field)
Expand Down
15 changes: 9 additions & 6 deletions lib/odf-report/report.rb
@@ -1,15 +1,15 @@
module ODFReport

class Report
include HashGsub, Images
include Fields, Images

attr_accessor :values, :tables, :images, :sections, :file
attr_accessor :fields, :tables, :images, :sections, :file

def initialize(template_name, &block)

@file = ODFReport::File.new(template_name)

@values = {}
@fields = []
@tables = []
@images = {}
@image_names_replacements = {}
Expand All @@ -19,8 +19,11 @@ def initialize(template_name, &block)

end

def add_field(field_tag, value='')
@values[field_tag] = value
def add_field(field_tag, value='', &block)
opts = {:name => field_tag, :value => value}
field = Field.new(opts, &block)
@fields << field

end

def add_table(table_name, collection, opts={}, &block)
Expand Down Expand Up @@ -76,7 +79,7 @@ def parse_document(txt)
end

def replace_fields!(content)
node_hash_gsub!(content, @values)
field_replace!(content)
end

def replace_tables!(content)
Expand Down
19 changes: 8 additions & 11 deletions lib/odf-report/section.rb
@@ -1,7 +1,7 @@
module ODFReport

class Section
include HashGsub, Nested
include Fields, Nested

attr_accessor :fields, :tables, :data, :name, :collection_field, :parent

Expand All @@ -11,20 +11,17 @@ def initialize(opts)
@collection = opts[:collection]
@parent = opts[:parent]

@fields = {}
@fields = []

@tables = []
@sections = []
end

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
def add_field(name, data_field=nil, &block)
opts = {:name => name, :data_field => data_field}
field = Field.new(opts, &block)
@fields << field

end

def add_table(table_name, collection_field, opts={}, &block)
Expand Down Expand Up @@ -58,7 +55,7 @@ def replace!(doc, row = nil)
@collection.each do |data_item|
new_section = template.dup

replace_values!(new_section, data_item)
replace_fields!(new_section, data_item)

@tables.each do |t|
t.replace!(new_section, data_item)
Expand Down
19 changes: 8 additions & 11 deletions lib/odf-report/table.rb
@@ -1,7 +1,7 @@
module ODFReport

class Table
include HashGsub, Nested
include Fields, Nested

attr_accessor :fields, :rows, :name, :collection_field, :data, :header, :parent, :tables

Expand All @@ -11,22 +11,19 @@ def initialize(opts)
@collection = opts[:collection]
@parent = opts[:parent]

@fields = {}
@fields = []
@tables = []

@template_rows = []
@header = opts[:header] || false
@skip_if_empty = opts[:skip_if_empty] || false
end

def add_column(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
def add_column(name, data_field=nil, &block)
opts = {:name => name, :data_field => data_field}
field = Field.new(opts, &block)
@fields << field

end

def add_table(table_name, collection_field, opts={}, &block)
Expand Down Expand Up @@ -58,7 +55,7 @@ def replace!(doc, row = nil)

new_node = get_next_row

replace_values!(new_node, data_item)
replace_fields!(new_node, data_item)

@tables.each do |t|
t.replace!(new_node, data_item)
Expand Down

0 comments on commit 9c7a48e

Please sign in to comment.