bc-to_xls 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ bc-to_xls-*.gem
2
+ test/*.log
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.1.0
data/bc-to_xls.gemspec CHANGED
@@ -1,22 +1,23 @@
1
1
  # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{bc-to_xls}
8
- s.version = "0.0.1"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jo\303\243o Jesus", "Vasco Andrade e Silva"]
12
- s.date = %q{2010-02-12}
12
+ s.date = %q{2010-02-14}
13
13
  s.description = %q{to_xls from http://github.com/arydjmal/to_xls with i18n support}
14
14
  s.email = %q{info@byclosure.com}
15
15
  s.extra_rdoc_files = [
16
16
  "README.rdoc"
17
17
  ]
18
18
  s.files = [
19
- "MIT-LICENSE",
19
+ ".gitignore",
20
+ "MIT-LICENSE",
20
21
  "README.rdoc",
21
22
  "Rakefile",
22
23
  "VERSION",
@@ -55,4 +56,3 @@ Gem::Specification.new do |s|
55
56
  s.add_dependency(%q<activerecord>, [">= 1.1"])
56
57
  end
57
58
  end
58
-
data/lib/to_xls.rb CHANGED
@@ -5,27 +5,32 @@ class Array
5
5
 
6
6
  if self.any?
7
7
  instance = self.first
8
- attributes = instance.attributes.keys.map { |c| c.to_sym }
8
+ attributes = instance.attributes.keys.sort.map { |c| c.to_sym }
9
9
 
10
10
  if options[:only]
11
11
  # the "& attributes" get rid of invalid columns
12
- columns = options[:only].to_a & attributes
12
+ columns = Array(options[:only]) & attributes
13
13
  else
14
- columns = attributes - options[:except].to_a
14
+ columns = attributes - Array(options[:except])
15
15
  end
16
-
16
+
17
17
  columns += options[:methods].to_a
18
18
 
19
19
  if columns.any?
20
20
  unless options[:headers] == false
21
21
  output << "<Row>"
22
- columns.each { |column| output << "<Cell><Data ss:Type=\"String\">#{instance.class.human_attribute_name(column)}</Data></Cell>" }
22
+ columns.each do |column|
23
+ output << "<Cell><Data ss:Type=\"String\">#{instance.class.human_attribute_name(column)}</Data></Cell>"
24
+ end
23
25
  output << "</Row>"
24
26
  end
25
27
 
26
28
  self.each do |item|
27
29
  output << "<Row>"
28
- columns.each { |column| output << "<Cell><Data ss:Type=\"#{item.send(column).kind_of?(Integer) ? 'Number' : 'String'}\">#{item.send(column)}</Data></Cell>" }
30
+ columns.each do |column|
31
+ value = item.send(column)
32
+ output << "<Cell><Data ss:Type=\"#{value.kind_of?(Integer) ? 'Number' : 'String'}\">#{value}</Data></Cell>"
33
+ end
29
34
  output << "</Row>"
30
35
  end
31
36
  end
data/test/test_helper.rb CHANGED
@@ -4,7 +4,9 @@ RAILS_ROOT = plugin_test_dir
4
4
 
5
5
  require 'rubygems'
6
6
  require 'test/unit'
7
-
7
+ require 'active_support'
8
+ require 'multi_rails_init'
9
+ require "test_help"
8
10
  require plugin_test_dir + '/../rails/init.rb'
9
11
 
10
12
  TestCaseClass = ActiveSupport::TestCase rescue Test::Unit::TestCase
data/test/to_xls_test.rb CHANGED
@@ -1,7 +1,37 @@
1
1
  require 'test_helper'
2
2
 
3
- class ToXlsTest < TestCaseClass
3
+ class User < ActiveRecord::Base
4
+ def is_old?
5
+ age > 22
6
+ end
7
+ end
4
8
 
9
+ class ToXlsTest < TestCaseClass
10
+ def xls_doc(content)
11
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\">#{content}</Workbook>"
12
+ end
13
+
14
+ def worksheet(name, content)
15
+ "<Worksheet ss:Name=\"#{name}\">#{content}</Worksheet>"
16
+ end
17
+
18
+ def worksheet_doc(worksheet_name, content)
19
+ xls_doc(worksheet(worksheet_name, content))
20
+ end
21
+
22
+ def cell(type, content)
23
+ "<Cell><Data ss:Type=\"#{type}\">#{content}</Data></Cell>"
24
+ end
25
+
26
+ def row(*cells)
27
+ "<Row>#{cells.join}</Row>"
28
+ end
29
+
30
+ def table(*rows)
31
+ "<Table>#{rows.join}</Table>"
32
+ end
33
+
34
+ # *****
5
35
  def setup
6
36
  @users = []
7
37
  @users << User.new(:id => 1, :name => 'Ary', :age => 24)
@@ -9,45 +39,96 @@ class ToXlsTest < TestCaseClass
9
39
  end
10
40
 
11
41
  def test_with_empty_array
12
- assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table></Table></Worksheet></Workbook>", [].to_xls )
42
+ assert_equal( worksheet_doc("Sheet1", table), [].to_xls )
13
43
  end
14
44
 
15
45
  def test_with_no_options
16
- assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"String\">id</Data></Cell><Cell><Data ss:Type=\"String\">name</Data></Cell><Cell><Data ss:Type=\"String\">age</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">1</Data></Cell><Cell><Data ss:Type=\"String\">Ary</Data></Cell><Cell><Data ss:Type=\"Number\">24</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">2</Data></Cell><Cell><Data ss:Type=\"String\">Nati</Data></Cell><Cell><Data ss:Type=\"Number\">21</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls )
46
+ assert_equal( worksheet_doc("Sheet1", table(
47
+ row(cell("String", "Age"), cell("String", "Name")),
48
+ row(cell("Number", 24), cell("String", "Ary")),
49
+ row(cell("Number", 21), cell("String", "Nati")))),
50
+ @users.to_xls
51
+ )
17
52
  end
18
53
 
19
54
  def test_with_no_headers
20
- assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"Number\">1</Data></Cell><Cell><Data ss:Type=\"String\">Ary</Data></Cell><Cell><Data ss:Type=\"Number\">24</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">2</Data></Cell><Cell><Data ss:Type=\"String\">Nati</Data></Cell><Cell><Data ss:Type=\"Number\">21</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:headers => false) )
55
+ assert_equal(worksheet_doc("Sheet1", table(
56
+ row(cell("Number", 24), cell("String", "Ary")),
57
+ row(cell("Number", 21), cell("String", "Nati")))),
58
+ @users.to_xls(:headers => false)
59
+ )
21
60
  end
22
61
 
23
62
  def test_with_only
24
- assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"String\">name</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Ary</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Nati</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:only => :name) )
63
+ assert_equal( worksheet_doc("Sheet1", table(
64
+ row(cell("String", "Name")),
65
+ row(cell("String", "Ary")),
66
+ row(cell("String", "Nati")))),
67
+ @users.to_xls(:only => :name)
68
+ )
25
69
  end
26
70
 
27
71
  def test_with_empty_only
28
- assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table></Table></Worksheet></Workbook>", @users.to_xls(:only => "") )
72
+ assert_equal( worksheet_doc("Sheet1", table), @users.to_xls(:only => "") )
29
73
  end
30
74
 
31
75
  def test_with_only_and_wrong_column_names
32
- assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"String\">name</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Ary</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Nati</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:only => [:name, :yoyo]) )
76
+ assert_equal( worksheet_doc("Sheet1", table(
77
+ row(cell("String", "Name")),
78
+ row(cell("String", "Ary")),
79
+ row(cell("String", "Nati")))),
80
+ @users.to_xls(:only => [:name, :yoyo])
81
+ )
33
82
  end
34
83
 
35
84
  def test_with_except
36
- assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"String\">age</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">24</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">21</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:except => [:id, :name]) )
85
+ assert_equal( worksheet_doc("Sheet1", table(
86
+ row(cell("String", "Age")),
87
+ row(cell("Number", 24)),
88
+ row(cell("Number", 21)))),
89
+ @users.to_xls(:except => [:id, :name])
90
+ )
37
91
  end
38
92
 
39
93
  def test_with_except_and_only_should_listen_to_only
40
- assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"String\">name</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Ary</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Nati</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:except => [:id, :name], :only => :name) )
94
+ assert_equal( worksheet_doc("Sheet1", table(
95
+ row(cell("String", "Name")),
96
+ row(cell("String", "Ary")),
97
+ row(cell("String", "Nati")))),
98
+ @users.to_xls(:except => [:id, :name], :only => :name)
99
+ )
41
100
  end
42
101
 
43
- def test_with_except
44
- assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"String\">id</Data></Cell><Cell><Data ss:Type=\"String\">name</Data></Cell><Cell><Data ss:Type=\"String\">age</Data></Cell><Cell><Data ss:Type=\"String\">is_old?</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">1</Data></Cell><Cell><Data ss:Type=\"String\">Ary</Data></Cell><Cell><Data ss:Type=\"Number\">24</Data></Cell><Cell><Data ss:Type=\"String\">false</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">2</Data></Cell><Cell><Data ss:Type=\"String\">Nati</Data></Cell><Cell><Data ss:Type=\"Number\">21</Data></Cell><Cell><Data ss:Type=\"String\">false</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:methods => [:is_old?]) )
102
+ def test_with_methods
103
+ assert_equal( worksheet_doc("Sheet1", table(
104
+ row(cell("String", "Age"), cell("String", "Name"), cell("String", "Is old?")),
105
+ row(cell("Number", 24), cell("String", "Ary"), cell("String", "true")),
106
+ row(cell("Number", 21), cell("String", "Nati"), cell("String", "false")))),
107
+ @users.to_xls(:methods => [:is_old?])
108
+ )
45
109
  end
46
110
 
47
111
  def test_with_i18n
112
+ old_locale = I18n.locale
48
113
  I18n.locale = "pt-PT"
49
- debugger
50
- assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"String\">nome</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Ary</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Nati</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls)
114
+ I18n.backend.store_translations("pt-PT", {
115
+ "activerecord" => {
116
+ "attributes" => {
117
+ "user" => {
118
+ "name" => "Nome",
119
+ "age" => "Idade"
120
+ }
121
+ }
122
+ }
123
+ })
124
+ assert_equal( worksheet_doc("Sheet1", table(
125
+ row(cell("String", "Idade"), cell("String", "Nome")),
126
+ row(cell("Number", 24), cell("String", "Ary")),
127
+ row(cell("Number", 21), cell("String", "Nati")))),
128
+ @users.to_xls
129
+ )
130
+ ensure
131
+ I18n.locale = old_locale
51
132
  end
52
133
 
53
134
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bc-to_xls
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Jo\xC3\xA3o Jesus"
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2010-02-12 00:00:00 +00:00
13
+ date: 2010-02-14 00:00:00 +00:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -32,6 +32,7 @@ extensions: []
32
32
  extra_rdoc_files:
33
33
  - README.rdoc
34
34
  files:
35
+ - .gitignore
35
36
  - MIT-LICENSE
36
37
  - README.rdoc
37
38
  - Rakefile