table_fu 0.3.3 → 0.3.4
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 +17 -11
- data/spec/table_fu_spec.rb +10 -2
- data/table_fu.gemspec +1 -1
- metadata +3 -3
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.3.
|
13
|
+
<h1>TableFu <small>– Version: 0.3.4</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
@@ -276,8 +276,8 @@ class TableFu
|
|
276
276
|
# And finally we return a empty string object or the value.
|
277
277
|
#
|
278
278
|
def to_s
|
279
|
-
ret = if
|
280
|
-
macro_value
|
279
|
+
ret = if macro?
|
280
|
+
macro_value.to_s
|
281
281
|
elsif @spreadsheet.formatting && format_method = @spreadsheet.formatting[column_name]
|
282
282
|
TableFu::Formatting.send(format_method, @datum) || ''
|
283
283
|
else
|
@@ -287,10 +287,18 @@ class TableFu
|
|
287
287
|
ret
|
288
288
|
end
|
289
289
|
|
290
|
-
# Returns
|
290
|
+
# Returns whether there is a macro format for this datum
|
291
291
|
#
|
292
292
|
# Returns:
|
293
|
-
#
|
293
|
+
# true if there is a macro format, false or nil otherwise
|
294
|
+
def macro?
|
295
|
+
@spreadsheet.formatting && @spreadsheet.formatting[@column_name].is_a?(Hash)
|
296
|
+
end
|
297
|
+
|
298
|
+
# Returns the macro'd format
|
299
|
+
#
|
300
|
+
# Returns:
|
301
|
+
# The macro value
|
294
302
|
def macro_value
|
295
303
|
# Grab the macro method first
|
296
304
|
# Then get a array of the values in the columns listed as arguments
|
@@ -302,14 +310,12 @@ class TableFu
|
|
302
310
|
#
|
303
311
|
# in the above case we handle the AppendedColumn in this method
|
304
312
|
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
arr
|
310
|
-
end
|
311
|
-
TableFu::Formatting.send(method, *arguments)
|
313
|
+
method = @spreadsheet.formatting[@column_name]['method']
|
314
|
+
arguments = @spreadsheet.formatting[@column_name]['arguments'].inject([]) do |arr,arg|
|
315
|
+
arr << @row.column_for(arg)
|
316
|
+
arr
|
312
317
|
end
|
318
|
+
TableFu::Formatting.send(method, *arguments)
|
313
319
|
end
|
314
320
|
|
315
321
|
# Returns the raw value of a datum
|
data/spec/table_fu_spec.rb
CHANGED
@@ -184,6 +184,10 @@ describe TableFu, 'with macro columns' do
|
|
184
184
|
"#{first}#{second}"
|
185
185
|
end
|
186
186
|
|
187
|
+
def null(first)
|
188
|
+
nil
|
189
|
+
end
|
190
|
+
|
187
191
|
end
|
188
192
|
|
189
193
|
end
|
@@ -194,7 +198,8 @@ describe TableFu, 'with macro columns' do
|
|
194
198
|
@spreadsheet = TableFu.new(csv)
|
195
199
|
@spreadsheet.col_opts[:style] = {'Projects' => 'text-align:left;'}
|
196
200
|
@spreadsheet.col_opts[:formatting] = {'Total Appropriation' => :currency,
|
197
|
-
'MacroColumn' => {'method' => 'append', 'arguments' => ['Projects','State']}
|
201
|
+
'MacroColumn' => {'method' => 'append', 'arguments' => ['Projects','State']},
|
202
|
+
'NullColumn' => {'method' => 'null', 'arguments' => ['Projects']}}
|
198
203
|
@spreadsheet.sorted_by = {'Projects' => {'order' => 'descending'}}
|
199
204
|
@spreadsheet.col_opts[:columns] = ['State', 'Total Appropriation', 'Projects', 'MacroColumn']
|
200
205
|
end
|
@@ -208,11 +213,14 @@ describe TableFu, 'with macro columns' do
|
|
208
213
|
@spreadsheet.rows[1].column_for('Total Appropriation').to_s.should eql '$42,367,198'
|
209
214
|
end
|
210
215
|
|
216
|
+
it "should allow macros that take arguments to return nil" do
|
217
|
+
@spreadsheet.rows[0].column_for('NullColumn').to_s.should eql ''
|
218
|
+
end
|
219
|
+
|
211
220
|
it "should not barf if we try a bad formatter" do
|
212
221
|
@spreadsheet.col_opts[:formatting] = {'Total Appropriation' => :bad_formatter }
|
213
222
|
@spreadsheet.rows[1].column_for('Total Appropriation').to_s.should eql "bad_formatter not a valid formatter!"
|
214
223
|
end
|
215
|
-
|
216
224
|
end
|
217
225
|
|
218
226
|
describe TableFu, 'with reordered columns' do
|
data/table_fu.gemspec
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: table_fu
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
9
|
+
- 4
|
10
|
+
version: 0.3.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Mark Percival
|