table_fu 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. data/.gitignore +4 -0
  2. data/LICENSE +20 -0
  3. data/README +37 -0
  4. data/Rakefile +58 -0
  5. data/VERSION.yml +5 -0
  6. data/doc/TableFu/Datum.html +743 -0
  7. data/doc/TableFu/Formatting.html +469 -0
  8. data/doc/TableFu/Header.html +198 -0
  9. data/doc/TableFu/Row.html +508 -0
  10. data/doc/TableFu.html +1806 -0
  11. data/doc/_index.html +154 -0
  12. data/doc/class_list.html +36 -0
  13. data/doc/css/common.css +1 -0
  14. data/doc/css/full_list.css +50 -0
  15. data/doc/css/style.css +268 -0
  16. data/doc/file.README.html +89 -0
  17. data/doc/file_list.html +38 -0
  18. data/doc/frames.html +13 -0
  19. data/doc/index.html +89 -0
  20. data/doc/js/app.js +99 -0
  21. data/doc/js/full_list.js +106 -0
  22. data/doc/js/jquery.js +19 -0
  23. data/doc/method_list.html +363 -0
  24. data/doc/top-level-namespace.html +85 -0
  25. data/documentation/css/dawn.css +121 -0
  26. data/documentation/css/styles.css +63 -0
  27. data/documentation/images/proplogo.png +0 -0
  28. data/documentation/index.html.erb +148 -0
  29. data/examples/columns.rb +7 -0
  30. data/examples/columns_hidden.rb +5 -0
  31. data/examples/faceting.rb +2 -0
  32. data/examples/formatting_options.rb +6 -0
  33. data/examples/last_name.rb +4 -0
  34. data/examples/link.rb +11 -0
  35. data/examples/only.rb +6 -0
  36. data/examples/rails_helpers.rb +3 -0
  37. data/examples/sort_by_column.rb +11 -0
  38. data/examples/sort_by_number.rb +0 -0
  39. data/examples/totals.rb +2 -0
  40. data/examples/zap_joyce.rb +3 -0
  41. data/index.html +210 -0
  42. data/lib/table_fu/formatting.rb +52 -0
  43. data/lib/table_fu.rb +386 -0
  44. data/spec/assets/sample.csv +476 -0
  45. data/spec/assets/test.csv +8 -0
  46. data/spec/assets/test_macro.csv +8 -0
  47. data/spec/rcov.opts +2 -0
  48. data/spec/readme_example_spec.rb +39 -0
  49. data/spec/spec.opts +4 -0
  50. data/spec/spec_helper.rb +4 -0
  51. data/spec/table_fu_spec.rb +221 -0
  52. data/table_fu.gemspec +112 -0
  53. metadata +141 -0
@@ -0,0 +1,221 @@
1
+ require 'spec'
2
+ require 'spec/spec_helper'
3
+ require 'fastercsv'
4
+
5
+
6
+ describe TableFu do
7
+
8
+ before :all do
9
+ csv = FasterCSV.parse(File.open('spec/assets/test.csv'))
10
+ @spreadsheet = TableFu.new(csv, :style => {'URL' => 'text-align: left;'})
11
+ end
12
+
13
+ it 'should give me back a Row object' do
14
+ @spreadsheet.rows.each do |r|
15
+ r.class.should eql TableFu::Row
16
+ end
17
+ end
18
+
19
+ it 'should give me back a column by it\'s header name' do
20
+ @spreadsheet.rows[0].column_for("State").to_s.should == "Alabama"
21
+ end
22
+
23
+ it 'should sort rows' do
24
+ @spreadsheet.sorted_by = {'State' => {"order" => 'descending'}}
25
+ @spreadsheet.rows[0].column_for("State").to_s.should eql "Wyoming"
26
+ @spreadsheet.sorted_by = {'Party' => {"order" => 'descending'}}
27
+ @spreadsheet.rows[0].column_for("Party").to_s.should eql "Republican"
28
+ @spreadsheet.sorted_by = {'Representative' => {"order" => 'ascending', "format" => 'last_name'}}
29
+ @spreadsheet.rows[2].column_for("Representative").to_s.should eql "Jo Bonner"
30
+
31
+
32
+ @spreadsheet.col_opts[:columns] = {'State', 'Party', 'Total Appropriations', 'URL'}
33
+ @spreadsheet.rows.each do |row|
34
+ row.columns.each do |column|
35
+ if column.column_name == 'URL'
36
+ column.style.should eql 'text-align: left;'
37
+ column.to_s.should eql ''
38
+ column.value.should be_nil
39
+ end
40
+ column.style.should_not be_nil
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ it 'should sort rows with numerals' do
47
+ @spreadsheet.sorted_by = {'Projects' => {"order" => 'ascending'}}
48
+ sorted = @spreadsheet.rows.map do |row|
49
+ row.column_for("Projects").value
50
+ end
51
+ sorted.should eql [4, 5, 10, 12, 20, 49, nil]
52
+ end
53
+
54
+ it 'should be able to contain only the rows I want, after sorting' do
55
+ @spreadsheet.sorted_by = {'State' => {"order" => 'ascending'}}
56
+ @spreadsheet.only!(1..2)
57
+ @spreadsheet.rows[0].column_for('State').to_s.should eql 'Arizona'
58
+ @spreadsheet.rows[1].column_for('State').to_s.should eql 'California'
59
+ @spreadsheet.deleted_rows.length.should eql 5
60
+ @spreadsheet.rows.length.should eql 2
61
+ end
62
+ end
63
+
64
+ describe TableFu, 'with a complicated setup' do
65
+
66
+ before :all do
67
+ csv = FasterCSV.parse(File.open('spec/assets/test.csv'))
68
+ @spreadsheet = TableFu.new(csv)
69
+ @spreadsheet.col_opts[:formatting] = {'Total Appropriation' => :currency,
70
+ "Representative" => :last_name_first_name}
71
+ @spreadsheet.col_opts[:style] = {'Leadership' => "text-align: left;", 'URL' => 'text-align: right;'}
72
+ @spreadsheet.col_opts[:invisible] = ['URL']
73
+ @spreadsheet.delete_rows! [8]
74
+ @spreadsheet.sorted_by = {'State' => {"order" => 'descending'}}
75
+ @spreadsheet.col_opts[:columns] = ['State', 'Leadership', 'Total Appropriation', 'Party', 'URL']
76
+ end
77
+
78
+ it 'Leadership column should be marked invisible' do
79
+ @spreadsheet.rows[0].column_for('URL').invisible?.should be_true
80
+ @spreadsheet.rows[0].column_for('State').invisible?.should be_false
81
+ end
82
+
83
+ it 'should give me back a Row object' do
84
+ @spreadsheet.rows.each do |r|
85
+ r.class.should eql TableFu::Row
86
+ end
87
+ end
88
+
89
+ it 'should give me back a 7 rows because it ignored row 476 and header' do
90
+ @spreadsheet.rows.size.should eql 7
91
+ end
92
+
93
+ it 'should give me back a column by it\'s header name' do
94
+ @spreadsheet.rows[3].column_for('State').to_s.should eql "Georgia"
95
+ @spreadsheet.rows[2].column_for('State').to_s.should eql "New Jersey"
96
+ @spreadsheet.rows[0].column_for('State').to_s.should eql "Wyoming"
97
+ end
98
+
99
+ it 'should total a column' do
100
+ @spreadsheet.total_for("Total Appropriation").value.should eql 16640189309
101
+ @spreadsheet.total_for("Total Appropriation").to_s.should eql "$16,640,189,309"
102
+ end
103
+
104
+ it 'should format a column' do
105
+ @spreadsheet.rows[0].column_for("Total Appropriation").to_s.should eql "$138,526,141"
106
+ @spreadsheet.rows[0].column_for("Total Appropriation").value.should eql 138526141
107
+ @spreadsheet.rows[4].column_for("Representative").to_s.should eql "Nunes, Devin"
108
+ end
109
+
110
+ it 'should format a header' do
111
+ @spreadsheet.headers[1].style.should eql 'text-align: left;'
112
+ @spreadsheet.headers[4].style.should eql 'text-align: right;'
113
+ end
114
+
115
+ it 'should not care what kind of keys the hash has' do
116
+ @spreadsheet = TableFu.new(File.open('spec/assets/test.csv'))
117
+ @spreadsheet.col_opts["style"] = {'Leadership' => "text-align: left;", 'URL' => 'text-align: right;'}
118
+
119
+ @spreadsheet.rows[0].column_for('Leadership').style.should ==
120
+ @spreadsheet.col_opts["style"]['Leadership']
121
+ end
122
+
123
+
124
+
125
+ end
126
+
127
+
128
+ describe TableFu, "with faceting" do
129
+
130
+ before :all do
131
+ csv = FasterCSV.parse(File.open('spec/assets/test.csv'))
132
+ @spreadsheet = TableFu.new(csv)
133
+ @spreadsheet.col_opts[:style] = {'Projects' => 'text-align:left;'}
134
+ @spreadsheet.col_opts[:formatting] = {'Total Appropriation' => :currency}
135
+ @spreadsheet.delete_rows! [8]
136
+ @spreadsheet.sorted_by = {'State' => {:order => 'ascending'}}
137
+ @faceted_spreadsheets = @spreadsheet.faceted_by("Party", :total => ['Projects', 'Total Appropriation'])
138
+ end
139
+
140
+ it "should have 2 facets" do
141
+ @faceted_spreadsheets.size.should == 2
142
+ end
143
+
144
+ it "should total up the projects and expenses" do
145
+ @faceted_spreadsheets[1].total_for("Projects").value.should eql 63
146
+ @faceted_spreadsheets[0].total_for("Projects").value.should eql 32
147
+ @faceted_spreadsheets[1].total_for("State").value.should eql 3
148
+ @faceted_spreadsheets[1].total_for("Total Appropriation").value.should eql 175142465
149
+ end
150
+
151
+ it "should keep formatting on totals" do
152
+ @faceted_spreadsheets[1].total_for("Total Appropriation").to_s.should eql "$175,142,465"
153
+ end
154
+
155
+ it "should remember what facet it belongs to" do
156
+ @faceted_spreadsheets[1].faceted?.should be_true
157
+ @faceted_spreadsheets[0].faceted_on.should == 'Democrat'
158
+ end
159
+
160
+ it "should keep the formatting" do
161
+
162
+ @faceted_spreadsheets[1].rows[1].column_for('Total Appropriation').to_s.should eql "$25,320,127"
163
+ @faceted_spreadsheets[1].rows[1].column_for('Projects').style.should eql "text-align:left;"
164
+ end
165
+
166
+
167
+ end
168
+
169
+ describe TableFu, 'with macro columns' do
170
+
171
+ class TableFu::Formatting
172
+
173
+ class<<self
174
+
175
+ def append(first, second)
176
+ "#{first}#{second}"
177
+ end
178
+
179
+ end
180
+
181
+ end
182
+
183
+
184
+ before :all do
185
+ csv = FasterCSV.parse(File.open('spec/assets/test_macro.csv').read)
186
+ @spreadsheet = TableFu.new(csv)
187
+ @spreadsheet.col_opts[:style] = {'Projects' => 'text-align:left;'}
188
+ @spreadsheet.col_opts[:formatting] = {'Total Appropriation' => :currency,
189
+ 'MacroColumn' => {'method' => 'append', 'arguments' => ['Projects','State']}}
190
+ @spreadsheet.sorted_by = {'State' => {:order => 'ascending'}}
191
+ @spreadsheet.col_opts[:columns] = ['State', 'Total Appropriation', 'MacroColumn']
192
+ end
193
+
194
+
195
+ it "should let us specify a macro for a column" do
196
+ @spreadsheet.rows[0].column_for('MacroColumn').to_s.should eql '10Alabama'
197
+ end
198
+
199
+ end
200
+
201
+ describe TableFu, 'with reordered columns' do
202
+
203
+ before :all do
204
+ csv = FasterCSV.parse(File.open('spec/assets/test.csv'))
205
+ @spreadsheet = TableFu.new(csv)
206
+ @spreadsheet.col_opts[:style] = {'Projects' => 'text-align:left;'}
207
+ @spreadsheet.col_opts[:formatting] = {'Total Appropriation' => :currency}
208
+ @spreadsheet.col_opts[:sorted_by] = {'State' => {:order => 'ascending'}}
209
+ end
210
+
211
+
212
+ it "should display columns in the correct order" do
213
+ @spreadsheet.col_opts[:columns] = ['State', 'Blah', 'Projects']
214
+ @spreadsheet.column_headers.size.should eql 7
215
+ @spreadsheet.columns.size.should eql 3
216
+ facets = @spreadsheet.faceted_by('State')
217
+ @spreadsheet.rows[0].column_for('Blah').to_s.should eql ''
218
+ @spreadsheet.rows[0].column_for('Projects').to_s.should eql '10'
219
+ end
220
+
221
+ end
data/table_fu.gemspec ADDED
@@ -0,0 +1,112 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{table_fu}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Mark Percival", "Jeff Larson"]
12
+ s.date = %q{2010-03-10}
13
+ s.description = %q{A library for manipulating tables as arrays}
14
+ s.email = %q{jeff.larson@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README",
23
+ "Rakefile",
24
+ "VERSION.yml",
25
+ "doc/TableFu.html",
26
+ "doc/TableFu/Datum.html",
27
+ "doc/TableFu/Formatting.html",
28
+ "doc/TableFu/Header.html",
29
+ "doc/TableFu/Row.html",
30
+ "doc/_index.html",
31
+ "doc/class_list.html",
32
+ "doc/css/common.css",
33
+ "doc/css/full_list.css",
34
+ "doc/css/style.css",
35
+ "doc/file.README.html",
36
+ "doc/file_list.html",
37
+ "doc/frames.html",
38
+ "doc/index.html",
39
+ "doc/js/app.js",
40
+ "doc/js/full_list.js",
41
+ "doc/js/jquery.js",
42
+ "doc/method_list.html",
43
+ "doc/top-level-namespace.html",
44
+ "documentation/css/dawn.css",
45
+ "documentation/css/styles.css",
46
+ "documentation/images/proplogo.png",
47
+ "documentation/index.html.erb",
48
+ "examples/columns.rb",
49
+ "examples/columns_hidden.rb",
50
+ "examples/faceting.rb",
51
+ "examples/formatting_options.rb",
52
+ "examples/last_name.rb",
53
+ "examples/link.rb",
54
+ "examples/only.rb",
55
+ "examples/rails_helpers.rb",
56
+ "examples/sort_by_column.rb",
57
+ "examples/sort_by_number.rb",
58
+ "examples/totals.rb",
59
+ "examples/zap_joyce.rb",
60
+ "index.html",
61
+ "lib/table_fu.rb",
62
+ "lib/table_fu/formatting.rb",
63
+ "spec/assets/sample.csv",
64
+ "spec/assets/test.csv",
65
+ "spec/assets/test_macro.csv",
66
+ "spec/rcov.opts",
67
+ "spec/readme_example_spec.rb",
68
+ "spec/spec.opts",
69
+ "spec/spec_helper.rb",
70
+ "spec/table_fu_spec.rb",
71
+ "table_fu.gemspec"
72
+ ]
73
+ s.homepage = %q{http://github.com/propublica/table_fu}
74
+ s.rdoc_options = ["--charset=UTF-8"]
75
+ s.require_paths = ["lib"]
76
+ s.rubygems_version = %q{1.3.5}
77
+ s.summary = %q{TableFu makes arrays act like spreadsheets}
78
+ s.test_files = [
79
+ "spec/readme_example_spec.rb",
80
+ "spec/spec_helper.rb",
81
+ "spec/table_fu_spec.rb",
82
+ "examples/columns.rb",
83
+ "examples/columns_hidden.rb",
84
+ "examples/faceting.rb",
85
+ "examples/formatting_options.rb",
86
+ "examples/last_name.rb",
87
+ "examples/link.rb",
88
+ "examples/only.rb",
89
+ "examples/rails_helpers.rb",
90
+ "examples/sort_by_column.rb",
91
+ "examples/sort_by_number.rb",
92
+ "examples/totals.rb",
93
+ "examples/zap_joyce.rb"
94
+ ]
95
+
96
+ if s.respond_to? :specification_version then
97
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
98
+ s.specification_version = 3
99
+
100
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
101
+ s.add_runtime_dependency(%q<fastercsv>, [">= 0"])
102
+ s.add_development_dependency(%q<spec>, [">= 0"])
103
+ else
104
+ s.add_dependency(%q<fastercsv>, [">= 0"])
105
+ s.add_dependency(%q<spec>, [">= 0"])
106
+ end
107
+ else
108
+ s.add_dependency(%q<fastercsv>, [">= 0"])
109
+ s.add_dependency(%q<spec>, [">= 0"])
110
+ end
111
+ end
112
+
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: table_fu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Mark Percival
8
+ - Jeff Larson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2010-03-10 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: fastercsv
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ version:
26
+ - !ruby/object:Gem::Dependency
27
+ name: spec
28
+ type: :development
29
+ version_requirement:
30
+ version_requirements: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ version:
36
+ description: A library for manipulating tables as arrays
37
+ email: jeff.larson@gmail.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - LICENSE
44
+ - README
45
+ files:
46
+ - .gitignore
47
+ - LICENSE
48
+ - README
49
+ - Rakefile
50
+ - VERSION.yml
51
+ - doc/TableFu.html
52
+ - doc/TableFu/Datum.html
53
+ - doc/TableFu/Formatting.html
54
+ - doc/TableFu/Header.html
55
+ - doc/TableFu/Row.html
56
+ - doc/_index.html
57
+ - doc/class_list.html
58
+ - doc/css/common.css
59
+ - doc/css/full_list.css
60
+ - doc/css/style.css
61
+ - doc/file.README.html
62
+ - doc/file_list.html
63
+ - doc/frames.html
64
+ - doc/index.html
65
+ - doc/js/app.js
66
+ - doc/js/full_list.js
67
+ - doc/js/jquery.js
68
+ - doc/method_list.html
69
+ - doc/top-level-namespace.html
70
+ - documentation/css/dawn.css
71
+ - documentation/css/styles.css
72
+ - documentation/images/proplogo.png
73
+ - documentation/index.html.erb
74
+ - examples/columns.rb
75
+ - examples/columns_hidden.rb
76
+ - examples/faceting.rb
77
+ - examples/formatting_options.rb
78
+ - examples/last_name.rb
79
+ - examples/link.rb
80
+ - examples/only.rb
81
+ - examples/rails_helpers.rb
82
+ - examples/sort_by_column.rb
83
+ - examples/sort_by_number.rb
84
+ - examples/totals.rb
85
+ - examples/zap_joyce.rb
86
+ - index.html
87
+ - lib/table_fu.rb
88
+ - lib/table_fu/formatting.rb
89
+ - spec/assets/sample.csv
90
+ - spec/assets/test.csv
91
+ - spec/assets/test_macro.csv
92
+ - spec/rcov.opts
93
+ - spec/readme_example_spec.rb
94
+ - spec/spec.opts
95
+ - spec/spec_helper.rb
96
+ - spec/table_fu_spec.rb
97
+ - table_fu.gemspec
98
+ has_rdoc: true
99
+ homepage: http://github.com/propublica/table_fu
100
+ licenses: []
101
+
102
+ post_install_message:
103
+ rdoc_options:
104
+ - --charset=UTF-8
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: "0"
112
+ version:
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: "0"
118
+ version:
119
+ requirements: []
120
+
121
+ rubyforge_project:
122
+ rubygems_version: 1.3.5
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: TableFu makes arrays act like spreadsheets
126
+ test_files:
127
+ - spec/readme_example_spec.rb
128
+ - spec/spec_helper.rb
129
+ - spec/table_fu_spec.rb
130
+ - examples/columns.rb
131
+ - examples/columns_hidden.rb
132
+ - examples/faceting.rb
133
+ - examples/formatting_options.rb
134
+ - examples/last_name.rb
135
+ - examples/link.rb
136
+ - examples/only.rb
137
+ - examples/rails_helpers.rb
138
+ - examples/sort_by_column.rb
139
+ - examples/sort_by_number.rb
140
+ - examples/totals.rb
141
+ - examples/zap_joyce.rb