odf-report 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,17 +1,15 @@
1
1
  module ODFReport
2
2
 
3
3
  class Table
4
- include Fields, Nested
5
-
6
- attr_accessor :fields, :rows, :name, :collection_field, :data, :header, :parent, :tables
4
+ include Nested
7
5
 
8
6
  def initialize(opts)
9
7
  @name = opts[:name]
10
8
  @collection_field = opts[:collection_field]
11
9
  @collection = opts[:collection]
12
- @parent = opts[:parent]
13
10
 
14
11
  @fields = []
12
+ @texts = []
15
13
  @tables = []
16
14
 
17
15
  @template_rows = []
@@ -19,49 +17,31 @@ class Table
19
17
  @skip_if_empty = opts[:skip_if_empty] || false
20
18
  end
21
19
 
22
- def add_column(name, data_field=nil, &block)
23
- opts = {:name => name, :data_field => data_field}
24
- field = Field.new(opts, &block)
25
- @fields << field
26
-
27
- end
28
-
29
- def add_table(table_name, collection_field, opts={}, &block)
30
- opts.merge!(:name => table_name, :collection_field => collection_field, :parent => self)
31
- tab = Table.new(opts)
32
- @tables << tab
20
+ def replace!(doc, row = nil)
33
21
 
34
- yield(tab)
35
- end
22
+ return unless table = find_table_node(doc)
36
23
 
37
- def populate!(row)
38
- @collection = get_collection_from_item(row, @collection_field) if row
39
- end
24
+ @template_rows = table.xpath("table:table-row")
40
25
 
41
- def replace!(doc, row = nil)
26
+ @header = table.xpath("table:table-header-rows").empty? ? @header : false
42
27
 
43
- return unless table = find_table_node(doc)
44
28
 
45
- populate!(row)
29
+ @collection = get_collection_from_item(row, @collection_field) if row
46
30
 
47
- if (@skip_if_empty || !@header) && @collection.empty?
31
+ if @skip_if_empty && @collection.empty?
48
32
  table.remove
49
33
  return
50
34
  end
51
35
 
52
- @template_rows = table.xpath("table:table-row")
53
-
54
- @header = table.xpath("table:table-header-rows").empty? ? @header : false
55
-
56
36
  @collection.each do |data_item|
57
37
 
58
38
  new_node = get_next_row
59
39
 
60
- replace_fields!(new_node, data_item)
40
+ @tables.each { |t| t.replace!(new_node, data_item) }
41
+
42
+ @texts.each { |t| t.replace!(new_node, data_item) }
61
43
 
62
- @tables.each do |t|
63
- t.replace!(new_node, data_item)
64
- end
44
+ @fields.each { |f| f.replace!(new_node, data_item) }
65
45
 
66
46
  table.add_child(new_node)
67
47
 
@@ -91,10 +71,6 @@ private
91
71
  @header ? 1 : 0
92
72
  end
93
73
 
94
- def reset
95
- @row_cursor = get_start_node
96
- end
97
-
98
74
  def template_length
99
75
  @tl ||= @template_rows.size
100
76
  end
@@ -40,4 +40,4 @@ module ODFReport
40
40
 
41
41
  end
42
42
 
43
- end
43
+ end
@@ -1,3 +1,3 @@
1
1
  module ODFReport
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.2"
3
3
  end
@@ -21,8 +21,11 @@ Gem::Specification.new do |s|
21
21
 
22
22
  s.add_development_dependency "bundler", "~> 1.6"
23
23
  s.add_development_dependency "rake"
24
+ s.add_development_dependency "rspec", "~> 3.0.0"
25
+ s.add_development_dependency "faker"
26
+ s.add_development_dependency "launchy"
24
27
 
25
- s.add_runtime_dependency('rubyzip', "~> 1.1.0")
28
+ s.add_runtime_dependency('rubyzip', "~> 1.2.0")
26
29
  s.add_runtime_dependency('nokogiri', ">= 1.5.0")
27
30
 
28
31
  end
@@ -0,0 +1,77 @@
1
+ RSpec.describe "Fields" do
2
+
3
+ before(:context) do
4
+
5
+ @field_01 = Faker::Company.name
6
+ @field_02 = Faker::Name.name
7
+
8
+ @itens_01 = Item.get_list(3)
9
+
10
+ report = ODFReport::Report.new("spec/specs.odt") do |r|
11
+
12
+ r.add_field(:field_01, @field_01)
13
+ r.add_field(:field_02, @field_02)
14
+
15
+ r.add_table('TABLE_01', @itens_01) do |t|
16
+ t.add_column(:column_01, :id)
17
+ t.add_column(:column_02, :name)
18
+ end
19
+
20
+ r.add_section('SECTION_01', @itens_01) do |t|
21
+ t.add_field(:s01_field_01, :id)
22
+ t.add_field(:s01_field_02, :name)
23
+ end
24
+
25
+ end
26
+
27
+ report.generate("spec/result/specs.odt")
28
+
29
+ @data = Inspector.new("spec/result/specs.odt")
30
+
31
+ end
32
+
33
+
34
+ it "simple fields replacement" do
35
+
36
+ expect(@data.text).not_to match(/\[FIELD_01\]/)
37
+ expect(@data.text).not_to match(/\[FIELD_02\]/)
38
+
39
+ expect(@data.text).to match @field_01
40
+ expect(@data.text).to match @field_02
41
+
42
+ end
43
+
44
+ it "table columns replacement" do
45
+
46
+ table = @data.xml.xpath(".//table:table[@table:name='TABLE_01']").to_s
47
+
48
+ @itens_01.each do |item|
49
+
50
+ expect(table).not_to match(/\[COLUMN_01\]/)
51
+ expect(table).not_to match(/\[COLUMN_02\]/)
52
+ expect(table).to match(/\[COLUMN_03\]/)
53
+
54
+ expect(table).to match(item.id)
55
+ expect(table).to match(item.name)
56
+
57
+ end
58
+
59
+ end
60
+
61
+ it "section fields replacement" do
62
+
63
+ expect(@data.text).not_to match(/\[S01_FIELD_01\]/)
64
+ expect(@data.text).not_to match(/\[S01_FIELD_02\]/)
65
+
66
+ @itens_01.each_with_index do |item, idx|
67
+
68
+ section = @data.xml.xpath(".//text:section[@text:name='SECTION_01_#{idx+1}']").to_s
69
+
70
+ expect(section).to match(item.id)
71
+ expect(section).to match(item.name)
72
+
73
+ end
74
+
75
+ end
76
+
77
+ end
@@ -0,0 +1,45 @@
1
+ require './lib/odf-report'
2
+ require 'faker'
3
+
4
+ I18n.enforce_available_locales = false
5
+
6
+ class Item
7
+ attr_accessor :id, :name, :subs
8
+ def initialize(_id, _name, _subs=[])
9
+ @name = _name
10
+ @id = _id
11
+ @subs = _subs
12
+ end
13
+
14
+ def self.get_list(quant = 3)
15
+ r = []
16
+ (1..quant).each do |i|
17
+ r << Item.new(Faker::Number.number(10), Faker::Name.name)
18
+ end
19
+ r
20
+ end
21
+
22
+ end
23
+
24
+ class Inspector
25
+
26
+ def initialize(file)
27
+ @content = nil
28
+ Zip::File.open(file) do |f|
29
+ @content = f.get_entry('content.xml').get_input_stream.read
30
+ end
31
+ end
32
+
33
+ def xml
34
+ @xml ||= Nokogiri::XML(@content)
35
+ end
36
+
37
+ def text
38
+ @text ||= xml.to_s
39
+ end
40
+
41
+ end
42
+
43
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
44
+ RSpec.configure do |config|
45
+ end
Binary file
@@ -0,0 +1,39 @@
1
+ RSpec.describe "Tables" do
2
+
3
+ before(:context) do
4
+
5
+ report = ODFReport::Report.new("spec/specs.odt") do |r|
6
+
7
+ r.add_table('TABLE_02', []) do |t|
8
+ t.add_column(:column_01, :id)
9
+ t.add_column(:column_02, :name)
10
+ end
11
+
12
+ r.add_table('TABLE_03', [], skip_if_empty: true) do |t|
13
+ t.add_column(:column_01, :id)
14
+ t.add_column(:column_02, :name)
15
+ end
16
+
17
+ end
18
+
19
+ report.generate("spec/result/specs.odt")
20
+
21
+ @data = Inspector.new("spec/result/specs.odt")
22
+
23
+ end
24
+
25
+ context "with Empty collection" do
26
+
27
+ it "should not remove table" do
28
+ table2 = @data.xml.xpath(".//table:table[@table:name='TABLE_02']")
29
+ expect(table2).not_to be_empty
30
+ end
31
+
32
+ it "should remove table if required" do
33
+ table3 = @data.xml.xpath(".//table:table[@table:name='TABLE_03']")
34
+ expect(table3).to be_empty
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,38 @@
1
+ # coding: UTF-8
2
+ require './lib/odf-report'
3
+ require 'faker'
4
+
5
+
6
+ @html = <<-HTML
7
+ <p>Uniquely promote installed base total linkage via emerging deliverables com <strong>[EVENTO_TEXTO_CARTA]</strong>, unleash cross-media collaboration <strong>[FUNCAO]</strong> [EVENTO_NOME].</p>
8
+
9
+ <p>Progressively fashion diverse portals nº <strong>[NUMERO_SECAO]</strong> do local <strong>[NOME_LOCAL]</strong>, situado na <strong>[ENDERECO_LOCAL]</strong> methodologies </p>
10
+
11
+ <p>Assertively orchestrate market positioning bandwidth rather than fully researched total linkage. Interactively architect granular e-markets via clicks-and-mortar ROI. Uniquely aggregate compelling.</p>
12
+
13
+ <p>Compellingly facilitate functionalized interfaces before wireless models. Compellingly morph parallel systems whereas combinado com o artigo 63, § 2º da Lei nº 9.504/97, abaixo mencionados:</p>
14
+
15
+ <p style="margin-left:1.76cm;"><em>Art. 120 - § 1º Compellingly envisioneer high standards in niches without best-of-breed leadership. Phosfluorescently unleash go forward methodologies after bricks-and-clicks niches. Authoritatively. </em></p>
16
+
17
+ <p style="margin-left:1.76cm;"><em>Art. 63 - [...] § 2º Enthusiastically parallel task user friendly functionalities whereas exceptional leadership. </em></p>
18
+
19
+ <p>Credibly enable multifunctional strategic theme areas and premium communities.</p>
20
+
21
+ HTML
22
+
23
+
24
+
25
+ report = ODFReport::Report.new("test/templates/test_fields_inside_text.odt") do |r|
26
+
27
+ r.add_text(:body, @html)
28
+
29
+ r.add_field('EVENTO_TEXTO_CARTA', Faker::Lorem.sentence)
30
+ r.add_field('FUNCAO', Faker::Lorem.word)
31
+ r.add_field('EVENTO_NOME', Faker::Company.name)
32
+
33
+ r.add_field('NUMERO_SECAO', Faker::Number.number(3))
34
+ r.add_field('NOME_LOCAL', Faker::Company.name)
35
+ r.add_field('ENDERECO_LOCAL', Faker::Address.street_address)
36
+ end
37
+
38
+ report.generate("test/result/test_fields_inside_text.odt")
@@ -0,0 +1,43 @@
1
+ # coding: UTF-8
2
+ require './lib/odf-report'
3
+ require 'faker'
4
+ require 'launchy'
5
+
6
+
7
+ class Item
8
+ attr_accessor :name, :sid, :children
9
+ def initialize(_name, _sid, _children=[])
10
+ @name=_name
11
+ @sid=_sid
12
+ @children=_children
13
+ end
14
+ end
15
+
16
+ @items = []
17
+ @items << Item.new("LOST", '007', %w(sawyer juliet hurley locke jack freckles))
18
+ @items << Item.new("ALIAS", '302', %w(sidney sloane jack michael marshal))
19
+ @items << Item.new("GREY'S ANATOMY", '220', %w(meredith christina izzie alex george))
20
+ @items << Item.new("BREAKING BAD", '556', %w(pollos gus mike heisenberg))
21
+
22
+
23
+ report = ODFReport::Report.new("test/templates/test_nested_tables.odt") do |r|
24
+
25
+ r.add_field("TAG_01", Time.now)
26
+ r.add_field("TAG_02", "TAG-2 -> New tag")
27
+
28
+ r.add_table("TABLE_MAIN", @items) do |s|
29
+
30
+ s.add_column('NAME') { |i| i.name }
31
+
32
+ s.add_column('SID', :sid)
33
+
34
+ s.add_table('TABLE_S1', :children, :header=>true) do |t|
35
+ t.add_column('NAME1') { |item| "-> #{item}" }
36
+ t.add_column('INV') { |item| item.to_s.reverse.upcase }
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+
43
+ report.generate("test/result/test_nested_tables.odt")
@@ -0,0 +1,44 @@
1
+ require './lib/odf-report'
2
+ require 'ostruct'
3
+ require 'faker'
4
+ require 'launchy'
5
+
6
+
7
+ class Item
8
+ attr_accessor :name, :sid, :children
9
+ def initialize(_name, _sid, _children=[])
10
+ @name=_name
11
+ @sid=_sid
12
+ @children=_children
13
+ end
14
+ end
15
+
16
+
17
+ @items = []
18
+ @items << Item.new("LOST", '007', %w(sawyer juliet hurley locke jack freckles))
19
+ @items << Item.new("ALIAS", '302', %w(sidney sloane jack michael marshal))
20
+ @items << Item.new("GREY'S ANATOMY", '220', %w(meredith christina izzie alex george))
21
+ @items << Item.new("BREAKING BAD", '556', %w(pollos gus mike heisenberg))
22
+
23
+
24
+ report = ODFReport::Report.new("test/templates/test_sections.odt") do |r|
25
+
26
+ r.add_field("TAG_01", Time.now)
27
+ r.add_field("TAG_02", "TAG-2 -> New tag")
28
+
29
+ r.add_section("SECTION_01", @items) do |s|
30
+
31
+ s.add_field('NAME') { |i| i.name }
32
+
33
+ s.add_field('SID', :sid)
34
+
35
+ s.add_table('TABLE_S1', :children, :header=>true) do |t|
36
+ t.add_column('NAME1') { |item| "-> #{item}" }
37
+ t.add_column('INV') { |item| item.to_s.reverse.upcase }
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+
44
+ report.generate("test/result/test_sections.odt")
@@ -0,0 +1,58 @@
1
+ require './lib/odf-report'
2
+ require 'ostruct'
3
+ require 'faker'
4
+ require 'launchy'
5
+
6
+
7
+ class Item
8
+ attr_accessor :name, :sid, :children, :subs
9
+ def initialize(_name, _sid, _children=[], _subs=[])
10
+ @name=_name
11
+ @sid=_sid
12
+ @children=_children
13
+ @subs=_subs
14
+ end
15
+ end
16
+
17
+
18
+
19
+ subs1 = []
20
+ subs1 << Item.new("SAWYER", 1, %w(Your bones don't break))
21
+ subs1 << Item.new("HURLEY", 2, %w(Your cells react to bacteria and viruses))
22
+ subs1 << Item.new("LOCKE", 3, %W(Do you see any Teletubbies in here))
23
+
24
+ subs2 = []
25
+ subs2 << Item.new("SLOANE", 21, %w(Praesent hendrerit lectus sit amet))
26
+ subs2 << Item.new("JACK", 22, %w(Donec nec est eget dolor laoreet))
27
+ subs2 << Item.new("MICHAEL", 23, %W(Integer elementum massa at nulla placerat varius))
28
+
29
+ @items = []
30
+ @items << Item.new("LOST", '007', [], subs1)
31
+ @items << Item.new("GREY'S ANATOMY", '220', nil)
32
+ @items << Item.new("ALIAS", '302', nil, subs2)
33
+ @items << Item.new("BREAKING BAD", '556', [])
34
+
35
+
36
+ report = ODFReport::Report.new("test/templates/test_sub_sections.odt") do |r|
37
+
38
+ r.add_field("TAG_01", Time.now)
39
+ r.add_field("TAG_02", "TAG-2 -> New tag")
40
+
41
+ r.add_section("SECTION_01", @items) do |s|
42
+
43
+ s.add_field('NAME') { |i| i.name }
44
+
45
+ s.add_field('SID', :sid)
46
+
47
+ s.add_section('SUB_01', :subs) do |r|
48
+ r.add_field("FIRST_NAME", :name)
49
+ r.add_table('IPSUM_TABLE', :children, :header => true) do |t|
50
+ t.add_column('IPSUM_ITEM') { |i| i }
51
+ end
52
+ end
53
+
54
+ end
55
+
56
+ end
57
+
58
+ report.generate("test/result/test_sub_sections.odt")