table_fu 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :minor: 2
3
- :patch: 0
3
+ :patch: 1
4
4
  :build:
5
5
  :major: 0
data/index.html CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  <body>
12
12
  <a href="http://www.propublica.org" class="propublica">&nbsp;</a>
13
- <h1>TableFu <small>&ndash; Version: 0.1.1</small></h1>
13
+ <h1>TableFu <small>&ndash; 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> &mdash; or group &mdash; 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, @row_num, @spreadsheet)
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, @row_num, @spreadsheet)
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[@spreadsheet.sorted_by.keys.first]["order"]
219
- format = @spreadsheet.sorted_by[@spreadsheet.sorted_by.keys.first]["format"]
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, row_num, spreadsheet)
244
+ def initialize(datum, col_name, row, spreadsheet)
245
245
  @datum = datum
246
246
  @column_name = col_name
247
- @row_num = row_num
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
- if @row_num && @spreadsheet.formatting && @spreadsheet.formatting[@column_name].is_a?(Hash)
287
+
288
+ if @spreadsheet.formatting && @spreadsheet.formatting[@column_name].is_a?(Hash)
288
289
  method = @spreadsheet.formatting[@column_name]['method']
289
- arguments = @spreadsheet.formatting[@column_name]['arguments'].inject([]){|arr,arg| arr << @spreadsheet.rows[@row_num].column_for(arg); arr}
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
@@ -1,8 +1 @@
1
- MacroColumn,State,Cong. District,Representative,Party,Leadership,Projects,Total Appropriation
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
@@ -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 = {'State' => {:order => 'ascending'}}
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[0].column_for('MacroColumn').to_s.should eql '10Alabama'
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
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{table_fu}
8
- s.version = "0.2.0"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Mark Percival", "Jeff Larson"]
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 1
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Mark Percival