table_fu 0.2.0 → 0.2.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.
- data/VERSION.yml +1 -1
- data/index.html +1 -1
- data/lib/table_fu.rb +13 -9
- data/spec/assets/test_macro.csv +1 -8
- data/spec/table_fu_spec.rb +9 -5
- data/table_fu.gemspec +1 -1
- metadata +2 -2
data/VERSION.yml
CHANGED
data/index.html
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
|
11
11
|
<body>
|
12
12
|
<a href="http://www.propublica.org" class="propublica"> </a>
|
13
|
-
<h1>TableFu <small>– Version: 0.
|
13
|
+
<h1>TableFu <small>– Version: 0.2.0</small></h1>
|
14
14
|
|
15
15
|
<p><a href="http://github.com/propublica/table-fu">TableFu</a> is a ruby gem for spreadsheet-style handling of arrays (e.g. filtering, formatting, and sorting by "column" or "row"). In addition, it has the ability to <a href="#facet">facet</a> — or group — rows according to cell value. It was developed as a backend for its companion project <a href="http://www.github.com/propublica/table-setter">TableSetter</a>.</p>
|
16
16
|
<p>For example, <strong>TableFu</strong> can consume a csv file and sort on a column:
|
data/lib/table_fu.rb
CHANGED
@@ -203,9 +203,9 @@ class TableFu
|
|
203
203
|
#
|
204
204
|
def datum_for(col_name)
|
205
205
|
if col_num = @spreadsheet.column_headers.index(col_name)
|
206
|
-
TableFu::Datum.new(self[col_num], col_name,
|
206
|
+
TableFu::Datum.new(self[col_num], col_name, self, @spreadsheet)
|
207
207
|
else # Return a nil Datum object for non existant column names
|
208
|
-
TableFu::Datum.new(nil, col_name,
|
208
|
+
TableFu::Datum.new(nil, col_name, self, @spreadsheet)
|
209
209
|
end
|
210
210
|
end
|
211
211
|
alias_method :column_for, :datum_for
|
@@ -215,8 +215,8 @@ class TableFu
|
|
215
215
|
def <=>(b)
|
216
216
|
if @spreadsheet.sorted_by
|
217
217
|
column = @spreadsheet.sorted_by.keys.first
|
218
|
-
order = @spreadsheet.sorted_by[
|
219
|
-
format = @spreadsheet.sorted_by[
|
218
|
+
order = @spreadsheet.sorted_by[column]["order"]
|
219
|
+
format = @spreadsheet.sorted_by[column]["format"]
|
220
220
|
a = column_for(column).value || ''
|
221
221
|
b = b.column_for(column).value || ''
|
222
222
|
if format
|
@@ -225,7 +225,7 @@ class TableFu
|
|
225
225
|
end
|
226
226
|
result = a <=> b
|
227
227
|
result = -1 if result.nil?
|
228
|
-
result = result * -1 if order == 'descending'
|
228
|
+
result = result * -1 if order == 'descending'
|
229
229
|
result
|
230
230
|
else
|
231
231
|
-1
|
@@ -241,10 +241,10 @@ class TableFu
|
|
241
241
|
# Each piece of datum should know where it is by column and row number, along
|
242
242
|
# with the spreadsheet it's apart of. There's probably a better way to go
|
243
243
|
# about doing this. Subclass?
|
244
|
-
def initialize(datum, col_name,
|
244
|
+
def initialize(datum, col_name, row, spreadsheet)
|
245
245
|
@datum = datum
|
246
246
|
@column_name = col_name
|
247
|
-
@
|
247
|
+
@row = row
|
248
248
|
@spreadsheet = spreadsheet
|
249
249
|
end
|
250
250
|
|
@@ -284,9 +284,13 @@ class TableFu
|
|
284
284
|
# 'AppendedColumn' => {'method' => 'append', 'arguments' => ['Projects','State']}}
|
285
285
|
#
|
286
286
|
# in the above case we handle the AppendedColumn in this method
|
287
|
-
|
287
|
+
|
288
|
+
if @spreadsheet.formatting && @spreadsheet.formatting[@column_name].is_a?(Hash)
|
288
289
|
method = @spreadsheet.formatting[@column_name]['method']
|
289
|
-
arguments =
|
290
|
+
arguments = @spreadsheet.formatting[@column_name]['arguments'].inject([]) do |arr,arg|
|
291
|
+
arr << @row.column_for(arg)
|
292
|
+
arr
|
293
|
+
end
|
290
294
|
TableFu::Formatting.send(method, *arguments)
|
291
295
|
end
|
292
296
|
end
|
data/spec/assets/test_macro.csv
CHANGED
@@ -1,8 +1 @@
|
|
1
|
-
|
2
|
-
,Alabama,1,Jo Bonner ,Republican,,10,11296197
|
3
|
-
,Arizona,8,Gabrielle Giffords ,Democrat,,20,42367198
|
4
|
-
,California,21,Devin Nunes ,Republican,,4,25320127
|
5
|
-
,Georgia,12,John Barrow ,Democrat,,12,28968552
|
6
|
-
,New Jersey,Multiple,,,,5,17922850
|
7
|
-
,Wyoming,,Cynthia Lummis ,Republican,,49,138526141
|
8
|
-
,TOTAL,,,,,,16375788244
|
1
|
+
State,Cong. District,Representative,Party,Leadership,Projects,Total Appropriation
|
data/spec/table_fu_spec.rb
CHANGED
@@ -187,13 +187,17 @@ describe TableFu, 'with macro columns' do
|
|
187
187
|
@spreadsheet.col_opts[:style] = {'Projects' => 'text-align:left;'}
|
188
188
|
@spreadsheet.col_opts[:formatting] = {'Total Appropriation' => :currency,
|
189
189
|
'MacroColumn' => {'method' => 'append', 'arguments' => ['Projects','State']}}
|
190
|
-
@spreadsheet.sorted_by = {'
|
191
|
-
@spreadsheet.col_opts[:columns] = ['State', 'Total Appropriation', 'MacroColumn']
|
190
|
+
@spreadsheet.sorted_by = {'Projects' => {'order' => 'descending'}}
|
191
|
+
@spreadsheet.col_opts[:columns] = ['State', 'Total Appropriation', 'Projects', 'MacroColumn']
|
192
192
|
end
|
193
|
-
|
194
|
-
|
193
|
+
|
195
194
|
it "should let us specify a macro for a column" do
|
196
|
-
@spreadsheet.rows[
|
195
|
+
@spreadsheet.rows[1].column_for('MacroColumn').to_s.should eql '20Arizona'
|
196
|
+
end
|
197
|
+
|
198
|
+
it "should keep the rows in order" do
|
199
|
+
@spreadsheet.rows[0].column_for('Projects').value.should eql 49
|
200
|
+
@spreadsheet.rows[1].column_for('Total Appropriation').to_s.should eql '$42,367,198'
|
197
201
|
end
|
198
202
|
|
199
203
|
end
|
data/table_fu.gemspec
CHANGED