simplexls 0.1.5 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +11 -5
- data/Rakefile +8 -0
- data/lib/simplexls.rb +21 -11
- data/test/test_simple_xls.rb +92 -0
- metadata +5 -5
data/README.markdown
CHANGED
@@ -5,13 +5,19 @@ This gem builds HTML tables which makes easy import data into Excel, OpenOffice
|
|
5
5
|
|
6
6
|
## Example
|
7
7
|
|
8
|
-
|
9
8
|
xls = SimpleXLS.new ['header1','header2','header3']
|
10
|
-
|
9
|
+
|
11
10
|
xls.push [1,2,3]
|
12
11
|
xls.push [4,5]
|
13
12
|
xls.push [6,7,8]
|
14
13
|
|
15
|
-
File.open('output.xls', 'w+') { |f|
|
16
|
-
|
17
|
-
|
14
|
+
File.open('output.xls', 'w+') { |f| f.puts xls }
|
15
|
+
|
16
|
+
### Add Collection
|
17
|
+
|
18
|
+
xls = SimpleXLS.new ['a','b','c']
|
19
|
+
|
20
|
+
xls.add([[1,2,3], [4,5,6]])
|
21
|
+
xls.add([ { :a => 1, :b => 2, :c => 3}, { :a => 4, :b => 5, :c => 6}])
|
22
|
+
|
23
|
+
The `add` method will accept an array of arrays or an array of hashes. When hashes are used, it will attempt to extract the values from the `Hash` using the header values as keys (first trying as a `String` then again as a `Symbol`). Since the headers are enumerated, order is preseved.
|
data/Rakefile
ADDED
data/lib/simplexls.rb
CHANGED
@@ -2,25 +2,35 @@ class SimpleXLS
|
|
2
2
|
attr_accessor :headers, :rows
|
3
3
|
|
4
4
|
def initialize(headers)
|
5
|
-
raise
|
5
|
+
raise ArgumentError.new('headers must be an Array') unless Array === headers
|
6
6
|
|
7
|
-
@headers = headers.
|
7
|
+
@headers = headers.dup
|
8
8
|
@rows = []
|
9
9
|
end
|
10
|
-
|
10
|
+
# TODO fill out array here is needed rather than when we build the string
|
11
|
+
# also, slice the array is longer than headers
|
11
12
|
def push(row)
|
12
|
-
raise
|
13
|
-
|
13
|
+
raise ArgumentError.new('row must be an Array') unless Array === row
|
14
|
+
|
15
|
+
row = row.dup
|
16
|
+
row += Array.new(headers.size - row.size) if row.size < headers.size
|
17
|
+
|
18
|
+
@rows.push row
|
19
|
+
end
|
20
|
+
|
21
|
+
alias :<< :push
|
22
|
+
|
23
|
+
# TODO test this!
|
24
|
+
def add(collection)
|
25
|
+
collection.each do |obj|
|
26
|
+
self << (Hash === obj ? headers.map { |key| obj[key] || obj[key.to_sym] } : obj.to_a)
|
27
|
+
end
|
14
28
|
end
|
15
29
|
|
16
30
|
def to_s
|
17
31
|
output = "<table><thead><tr>"
|
18
32
|
output << @headers.collect { |hr| "<th>#{hr}</th>" }.join << "</tr></thead><tbody>"
|
19
|
-
output << @rows.collect { |row|
|
20
|
-
# even out the row if needed
|
21
|
-
row += Array.new(headers.size - row.size) if row.size != headers.size
|
22
|
-
"<tr>#{row.collect { |cell| "<td>#{cell}</td>"}.join}</tr>"
|
23
|
-
}.join
|
33
|
+
output << @rows.collect { |row| "<tr>#{row.collect { |cell| "<td>#{cell}</td>"}.join}</tr>" }.join
|
24
34
|
output << "</tbody></table>"
|
25
35
|
end
|
26
|
-
end
|
36
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'simplexls'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class SimpleXlsTest < Test::Unit::TestCase
|
5
|
+
def test_initialize
|
6
|
+
headers = ['a', 'b', 'c']
|
7
|
+
xls = SimpleXLS.new(headers)
|
8
|
+
|
9
|
+
assert_equal headers, xls.headers
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_initialize_dups_headers
|
13
|
+
headers = ['a', 'b', 'c']
|
14
|
+
xls = SimpleXLS.new(headers)
|
15
|
+
|
16
|
+
assert_not_same headers, xls.headers
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_row_push
|
20
|
+
xls = SimpleXLS.new(['a', 'b', 'c'])
|
21
|
+
row = [1,2,3]
|
22
|
+
|
23
|
+
xls.push(row)
|
24
|
+
|
25
|
+
assert_equal row, xls.rows.first
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_row_push_alias
|
29
|
+
xls = SimpleXLS.new(['a','b','c'])
|
30
|
+
|
31
|
+
assert_equal xls.method(:push), xls.method(:<<)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_evens_out_row
|
35
|
+
xls = SimpleXLS.new(['a','b','c'])
|
36
|
+
row = [1,2]
|
37
|
+
|
38
|
+
xls.push(row)
|
39
|
+
|
40
|
+
assert_equal 3, xls.rows.first.size
|
41
|
+
assert_equal [1,2,nil], xls.rows.first
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_row_push_dups_row
|
45
|
+
xls = SimpleXLS.new(['a', 'b', 'c'])
|
46
|
+
row = [1,2,3]
|
47
|
+
|
48
|
+
xls.push(row)
|
49
|
+
|
50
|
+
assert_not_same row, xls.rows.first
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_add_collection
|
54
|
+
xls = SimpleXLS.new(['a', 'b', 'c'])
|
55
|
+
rows = [[1,2,3], [4,5,6]]
|
56
|
+
|
57
|
+
xls.add(rows)
|
58
|
+
|
59
|
+
assert_equal rows.first, xls.rows.first
|
60
|
+
assert_equal rows.last, xls.rows.last
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_add_collection_with_hash
|
64
|
+
xls = SimpleXLS.new(['a', 'b', 'c'])
|
65
|
+
|
66
|
+
rows = [{:a => 1, :b => 2, :c => 3}, {:a => 4, :b => 5, :d => 7}]
|
67
|
+
xls.add(rows)
|
68
|
+
|
69
|
+
assert_equal rows.first.values, xls.rows.first
|
70
|
+
assert_equal [4,5,nil], xls.rows.last
|
71
|
+
|
72
|
+
rows = [{:a => 1, :b => 2, :c => 3}, {:a => 4, :c => 5, :d => 7}]
|
73
|
+
xls.add(rows)
|
74
|
+
|
75
|
+
assert_equal rows.first.values, xls.rows.first
|
76
|
+
assert_equal [4,nil,5], xls.rows.last
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_output
|
80
|
+
xls = SimpleXLS.new(['a', 'b', 'c'])
|
81
|
+
xls.push([1,2,3])
|
82
|
+
|
83
|
+
output = xls.to_s
|
84
|
+
|
85
|
+
assert output.include?('<table><thead>')
|
86
|
+
assert output.include?('<th>a</th><th>b</th><th>c</th>')
|
87
|
+
assert output.include?('<td>1</td><td>2</td><td>3</td>')
|
88
|
+
assert output.include?('</tbody></table>')
|
89
|
+
|
90
|
+
assert_equal 2, output.scan(/<tr>/).size
|
91
|
+
end
|
92
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simplexls
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
13
|
-
default_executable:
|
12
|
+
date: 2012-02-29 00:00:00.000000000Z
|
14
13
|
dependencies: []
|
15
14
|
description:
|
16
15
|
email: mikeycgto@gmail.com
|
@@ -18,9 +17,10 @@ executables: []
|
|
18
17
|
extensions: []
|
19
18
|
extra_rdoc_files: []
|
20
19
|
files:
|
20
|
+
- Rakefile
|
21
21
|
- README.markdown
|
22
22
|
- lib/simplexls.rb
|
23
|
-
|
23
|
+
- test/test_simple_xls.rb
|
24
24
|
homepage: http://github.com/mikeycgto/simple-xls
|
25
25
|
licenses: []
|
26
26
|
post_install_message:
|
@@ -41,7 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
41
41
|
version: '0'
|
42
42
|
requirements: []
|
43
43
|
rubyforge_project:
|
44
|
-
rubygems_version: 1.
|
44
|
+
rubygems_version: 1.8.10
|
45
45
|
signing_key:
|
46
46
|
specification_version: 3
|
47
47
|
summary: Simple XLS
|