Skip to content
This repository
Newer
Older
100644 1049 lines (945 sloc) 32.264 kb
41843932 » mikem836
2007-05-02 docs for table, fix for #270
1 # Ruport : Extensible Reporting System
7640084f » sandal
2006-08-13 fixed bug in tag duplication
2 #
41843932 » mikem836
2007-05-02 docs for table, fix for #270
3 # data/table.rb provides a table data structure for Ruport.
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
4 #
41843932 » mikem836
2007-05-02 docs for table, fix for #270
5 # Created by Gregory Brown / Dudley Flanders, 2006
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
6 # Copyright (C) 2006 Gregory Brown / Dudley Flanders, All Rights Reserved.
41843932 » mikem836
2007-05-02 docs for table, fix for #270
7 #
8 # This is free software distributed under the same terms as Ruby 1.8
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
9 # See LICENSE and COPYING for details.
41843932 » mikem836
2007-05-02 docs for table, fix for #270
10 #
11 module Ruport::Data
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
12
fa793713 » dudley
2006-11-11 Add headers to the class rdocs.
13 # === Overview
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
14 #
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
15 # This class is one of the core classes for building and working with data
16 # in Ruport. The idea is to get your data into a standard form, regardless
5480d2c8 » dudley
2006-08-14 A little English-Fu on jh++'s docs.
17 # of its source (a database, manual arrays, ActiveRecord, CSVs, etc.).
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
18 #
8864d46a » jh
2006-08-14 - added some api documentation to Ruport::Data::Table and Array
19 # Table is intended to be used as the data store for structured, tabular
28e261b1 » mikem836
2007-02-07 doc updates
20 # data.
8864d46a » jh
2006-08-14 - added some api documentation to Ruport::Data::Table and Array
21 #
41843932 » mikem836
2007-05-02 docs for table, fix for #270
22 # Once your data is in a Table object, it can be manipulated
8864d46a » jh
2006-08-14 - added some api documentation to Ruport::Data::Table and Array
23 # to suit your needs, then used to build a report.
24 #
ecd5ef55 » sandal
2007-08-28 Fighting with phantoms in the night
25 class Table
9706c785 » sandal
2007-04-20 Fix for #252
26
75b6e909 » sandal
2008-02-04 Ryan's pivot code. #380
27 class Pivot #:nodoc:
28
29 def initialize(table, group_col, pivot_col, summary_col, options = {})
30 @table = table
31 @group_column = group_col
32 @pivot_column = pivot_col
33 @summary_column = summary_col
34 @pivot_order = options[:pivot_order]
333a43e3 » Bob Nadler
2010-03-30 Operation support for pivot method.
35 @operation = options[:operation] || :first
e3bfad91 » wpiekutowski
2011-01-30 Move pivot grouping operations to Operation module
36 unless Operation.respond_to?(@operation, true)
11a80843 » wpiekutowski
2011-01-30 Move aggregations to separate methods
37 raise ArgumentError, "Unknown pivot operation '#{@operation}'"
38 end
75b6e909 » sandal
2008-02-04 Ryan's pivot code. #380
39 end
40
17df6aaa » wpiekutowski
2011-01-30 Rename #columns_from_pivot to #columns and cache the result. Extract …
41 def columns
42 return @columns if defined?(@columns)
43
3bc758e4 » wpiekutowski
2011-01-30 Shorten #convert_row_order_to_group_order to #row_order_to_group_orde…
44 ordering = self.class.row_order_to_group_order(@pivot_order)
75b6e909 » sandal
2008-02-04 Ryan's pivot code. #380
45 pivot_column_grouping = Grouping(@table, :by => @pivot_column)
17df6aaa » wpiekutowski
2011-01-30 Rename #columns_from_pivot to #columns and cache the result. Extract …
46 pivot_column_grouping.each { |n,g| g.add_column(n) { n } }
75b6e909 » sandal
2008-02-04 Ryan's pivot code. #380
47 pivot_column_grouping.sort_grouping_by!(ordering) if ordering
17df6aaa » wpiekutowski
2011-01-30 Rename #columns_from_pivot to #columns and cache the result. Extract …
48
49 @columns = pivot_column_grouping.inject([]) do |result, data|
50 name = data[0]
51 result << name
52 end
75b6e909 » sandal
2008-02-04 Ryan's pivot code. #380
53 end
54
55 def group_column_entries
56 @table.map {|row| row[@group_column]}.uniq
57 end
58
333a43e3 » Bob Nadler
2010-03-30 Operation support for pivot method.
59 def perform_operation(rows)
e3bfad91 » wpiekutowski
2011-01-30 Move pivot grouping operations to Operation module
60 Operation.send @operation, rows, @summary_column
11a80843 » wpiekutowski
2011-01-30 Move aggregations to separate methods
61 end
62
75b6e909 » sandal
2008-02-04 Ryan's pivot code. #380
63 def to_table
17df6aaa » wpiekutowski
2011-01-30 Rename #columns_from_pivot to #columns and cache the result. Extract …
64 table = Table.new
65 create_columns(table)
66
75b6e909 » sandal
2008-02-04 Ryan's pivot code. #380
67 outer_grouping = Grouping(@table, :by => @group_column)
68 group_column_entries.each {|outer_group_name|
69 outer_group = outer_grouping[outer_group_name]
17df6aaa » wpiekutowski
2011-01-30 Rename #columns_from_pivot to #columns and cache the result. Extract …
70 pivot_values = columns.inject({}) do |hsh, e|
75b6e909 » sandal
2008-02-04 Ryan's pivot code. #380
71 matching_rows = outer_group.rows_with(@pivot_column => e)
333a43e3 » Bob Nadler
2010-03-30 Operation support for pivot method.
72 hsh[e] = perform_operation(matching_rows)
75b6e909 » sandal
2008-02-04 Ryan's pivot code. #380
73 hsh
74 end
17df6aaa » wpiekutowski
2011-01-30 Rename #columns_from_pivot to #columns and cache the result. Extract …
75 table << [outer_group_name] + columns.map {|e|
75b6e909 » sandal
2008-02-04 Ryan's pivot code. #380
76 pivot_values[e]
77 }
78 }
17df6aaa » wpiekutowski
2011-01-30 Rename #columns_from_pivot to #columns and cache the result. Extract …
79
80 table
81 end
82
83 private
84
3bc758e4 » wpiekutowski
2011-01-30 Shorten #convert_row_order_to_group_order to #row_order_to_group_orde…
85 def self.row_order_to_group_order(row_order_spec)
86 case row_order_spec
87 when Array
88 proc {|group|
89 row_order_spec.map {|e| group[0][e].to_s }
90 }
91 when Proc
92 proc {|group|
93 if row_order_spec.arity == 2
94 row_order_spec.call(group[0], group.name)
95 else
96 row_order_spec.call(group[0])
97 end
98 }
99 when NilClass
100 nil
101 else
102 proc {|group| group[0][row_order_spec].to_s }
103 end
104 end
105
17df6aaa » wpiekutowski
2011-01-30 Rename #columns_from_pivot to #columns and cache the result. Extract …
106 def create_columns(table)
107 table.add_column(@group_column)
108 columns.each { |name| table.add_column(name) }
75b6e909 » sandal
2008-02-04 Ryan's pivot code. #380
109 end
110
e3bfad91 » wpiekutowski
2011-01-30 Move pivot grouping operations to Operation module
111 module Operation
112 extend self
113
114 def first(rows, summary_column)
115 rows.first && rows.first[summary_column]
116 end
117
118 def sum(rows, summary_column)
119 rows && rows.inject(0) { |sum,row| sum+row[summary_column] }
120 end
121
122 def count(rows, summary_column)
123 rows && rows.length
124 end
125
126 def mean(rows, summary_column)
127 sum = rows && rows.inject(0) { |sum,row| sum+row[summary_column] }
128 sum / rows.length
129 end
130
131 def min(rows, summary_column)
132 rows && (rows.collect { |r| r[summary_column] }).min
133 end
134
135 def max(rows, summary_column)
136 rows && (rows.collect { |r| r[summary_column] }).max
137 end
138 end
75b6e909 » sandal
2008-02-04 Ryan's pivot code. #380
139 end
140
141 # Creates a new table with values from the specified pivot column
142 # transformed into columns.
143 #
144 # Required options:
145 # <b><tt>:group_by</tt></b>:: The name of a column whose unique
146 # values should become rows in the new
147 # table.
148 #
149 # <b><tt>:values</tt></b>:: The name of a column that should supply
150 # the values for the pivoted columns.
151 #
152 # Optional:
153 # <b><tt>:pivot_order</tt></b>:: An ordering specification for the
154 # pivoted columns, in terms of the source
155 # rows. If this is a Proc there is an
156 # optional second argument that receives
157 # the name of the pivot column, which due
158 # to implementation oddity currently is
159 # removed from the row provided in the
160 # first argument. This wart will likely
161 # be fixed in a future version.
162 #
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
163 # <b><tt>:operation</tt></b>:: The operation to perform on
164 # <tt>:values</tt> column. Supported
165 # operations are <tt>:first</tt>,
166 # <tt>:sum</tt>, <tt>:count</tt>,
167 # <tt>:mean</tt>, <tt>:min</tt>, and
168 # <tt>:max</tt>. If not specified, the
0782081e » Bob Nadler
2010-03-31 Added :mean, :max and :min operations to Table#pivot.
169 # default is <tt>:first</tt>.
333a43e3 » Bob Nadler
2010-03-30 Operation support for pivot method.
170 #
75b6e909 » sandal
2008-02-04 Ryan's pivot code. #380
171 # Example:
172 #
173 # Given a table <em>my_table</em>:
174 # +-------------------------+
175 # | Group | Segment | Value |
176 # +-------------------------+
177 # | A | 1 | 0 |
178 # | A | 2 | 1 |
179 # | B | 1 | 2 |
180 # | B | 2 | 3 |
181 # +-------------------------+
182 #
183 # Pivoting the table on the Segment column:
184 #
185 # my_table.pivot('Segment', :group_by => 'Group', :values => 'Value',
186 # :pivot_order => proc {|row, name| name})
187 #
188 # Yields a new table like this:
189 # +---------------+
190 # | Group | 1 | 2 |
191 # +---------------+
192 # | A | 0 | 1 |
193 # | B | 2 | 3 |
194 # +---------------+
195 #
196 def pivot(pivot_column, options = {})
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
197 group_column = options[:group_by] ||
75b6e909 » sandal
2008-02-04 Ryan's pivot code. #380
198 raise(ArgumentError, ":group_by option required")
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
199 value_column = options[:values] ||
75b6e909 » sandal
2008-02-04 Ryan's pivot code. #380
200 raise(ArgumentError, ":values option required")
201 Pivot.new(
202 self, group_column, pivot_column, value_column, options
203 ).to_table
204 end
205
41843932 » mikem836
2007-05-02 docs for table, fix for #270
206 # === Overview
207 #
208 # This module provides facilities for creating tables from csv data.
209 #
9706c785 » sandal
2007-04-20 Fix for #252
210 module FromCSV
211 # Loads a CSV file directly into a Table using the FasterCSV library.
212 #
213 # Example:
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
214 #
9706c785 » sandal
2007-04-20 Fix for #252
215 # # treat first row as column_names
216 # table = Table.load('mydata.csv')
217 #
218 # # do not assume the data has column_names
219 # table = Table.load('mydata.csv',:has_names => false)
220 #
221 # # pass in FasterCSV options, such as column separators
8086f99a » sandal
2007-08-07 Docfix #348
222 # table = Table.load('mydata.csv',:csv_options => { :col_sep => "\t" })
9706c785 » sandal
2007-04-20 Fix for #252
223 #
224 def load(csv_file, options={},&block)
225 get_table_from_csv(:foreach, csv_file, options,&block)
226 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
227
9706c785 » sandal
2007-04-20 Fix for #252
228 # Creates a Table from a CSV string using FasterCSV. See Table.load for
229 # additional examples.
230 #
231 # table = Table.parse("a,b,c\n1,2,3\n4,5,6\n")
232 #
ecd5ef55 » sandal
2007-08-28 Fighting with phantoms in the night
233 def parse(string, options={},&block)
9706c785 » sandal
2007-04-20 Fix for #252
234 get_table_from_csv(:parse,string,options,&block)
235 end
236
237 private
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
238
9706c785 » sandal
2007-04-20 Fix for #252
239 def get_table_from_csv(msg,param,options={},&block) #:nodoc:
240 require "fastercsv"
241
242 options = {:has_names => true,
243 :csv_options => {} }.merge(options)
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
244
9706c785 » sandal
2007-04-20 Fix for #252
245 adjust_options_for_fcsv_headers(options)
246
2f130187 » sandal
2007-08-24 Merging in data feeders
247 table = self.new(options) do |feeder|
248 first_line = true
ecd5ef55 » sandal
2007-08-28 Fighting with phantoms in the night
249 FasterCSV.send(msg,param,options[:csv_options]) do |row|
2f130187 » sandal
2007-08-24 Merging in data feeders
250 if first_line
ecd5ef55 » sandal
2007-08-28 Fighting with phantoms in the night
251 adjust_for_headers(feeder.data,row,options)
2f130187 » sandal
2007-08-24 Merging in data feeders
252 first_line = false
ecd5ef55 » sandal
2007-08-28 Fighting with phantoms in the night
253 next if options[:has_names]
04603988 » sandal
2007-08-10 Basic CSV handling of :filters and :transforms, but we need to rethin…
254 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
255
2f130187 » sandal
2007-08-24 Merging in data feeders
256 if block
257 handle_csv_row_proc(feeder,row,options,block)
ecd5ef55 » sandal
2007-08-28 Fighting with phantoms in the night
258 else
259 feeder << row
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
260 end
2f130187 » sandal
2007-08-24 Merging in data feeders
261 end
9706c785 » sandal
2007-04-20 Fix for #252
262 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
263
2f130187 » sandal
2007-08-24 Merging in data feeders
264 return table
9706c785 » sandal
2007-04-20 Fix for #252
265 end
266
2f130187 » sandal
2007-08-24 Merging in data feeders
267 def handle_csv_row_proc(feeder,row,options,block)
9a18d059 » sandal
2007-04-20 small refactoring
268 if options[:records]
f2e27cc5 » sandal
2007-04-27 Fix for #285
269 rc = options[:record_class] || Record
2f130187 » sandal
2007-08-24 Merging in data feeders
270 row = rc.new(row, :attributes => feeder.data.column_names)
9a18d059 » sandal
2007-04-20 small refactoring
271 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
272
2f130187 » sandal
2007-08-24 Merging in data feeders
273 block[feeder,row]
9a18d059 » sandal
2007-04-20 small refactoring
274 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
275
9706c785 » sandal
2007-04-20 Fix for #252
276 def adjust_options_for_fcsv_headers(options)
277 options[:has_names] = false if options[:csv_options][:headers]
278 end
279
280 def adjust_for_headers(loaded,row,options)
281 if options[:has_names]
282 loaded.column_names = row
283 elsif options[:csv_options][:headers]
284 loaded.column_names = row.headers
285 end
286 end
287 end
288
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
289 include Enumerable
9706c785 » sandal
2007-04-20 Fix for #252
290 extend FromCSV
55a719f6 » sandal
2007-04-20 investigate tests, but this rocks
291
c128857f » mikem836
2008-03-17 change Renderer to Controller
292 include Ruport::Controller::Hooks
55a719f6 » sandal
2007-04-20 investigate tests, but this rocks
293 renders_as_table
b691e501 » sandal
2007-04-20 Fixed an inheritence bug for the new renderer hook stuff
294
41843932 » mikem836
2007-05-02 docs for table, fix for #270
295 def self.inherited(base) #:nodoc:
b691e501 » sandal
2007-04-20 Fixed an inheritence bug for the new renderer hook stuff
296 base.renders_as_table
297 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
298
5480d2c8 » dudley
2006-08-14 A little English-Fu on jh++'s docs.
299 # Creates a new table based on the supplied options.
41843932 » mikem836
2007-05-02 docs for table, fix for #270
300 #
301 # Valid options:
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
302 # <b><tt>:data</tt></b>:: An Array of Arrays representing the
41843932 » mikem836
2007-05-02 docs for table, fix for #270
303 # records in this Table.
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
304 # <b><tt>:column_names</tt></b>:: An Array containing the column names
41843932 » mikem836
2007-05-02 docs for table, fix for #270
305 # for this Table.
1479c53a » mikem836
2007-08-29 doc patches
306 # <b><tt>:filters</tt></b>:: A proc or array of procs that set up
307 # conditions to filter the data being
308 # added to the table.
309 # <b><tt>:transforms</tt></b>:: A proc or array of procs that perform
310 # transformations on the data being added
311 # to the table.
312 # <b><tt>:record_class</tt></b>:: Specify the class of the table's
313 # records.
314 #
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
315 # Example:
316 #
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
317 # table = Table.new :data => [[1,2,3], [3,4,5]],
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
318 # :column_names => %w[a b c]
8864d46a » jh
2006-08-14 - added some api documentation to Ruport::Data::Table and Array
319 #
349aca9c » sandal
2006-07-17 started work on Table
320 def initialize(options={})
2a139866 » dudley
2006-10-13 Made Table's @column_names and Record's @attributes default to [] ins…
321 @column_names = options[:column_names] ? options[:column_names].dup : []
0e563dff » sandal
2007-02-03 Thanks to Evan Weaver for helping me fix this marshalling bug
322 @record_class = options[:record_class] &&
323 options[:record_class].name || "Ruport::Data::Record"
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
324 @data = []
325
ecd5ef55 » sandal
2007-08-28 Fighting with phantoms in the night
326 feeder = Feeder.new(self)
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
327
8f8c4b20 » sandal
2007-08-28 Quieted some warnings, cleaned up some tests
328 Array(options[:filters]).each { |f| feeder.filter(&f) }
329 Array(options[:transforms]).each { |t| feeder.transform(&t) }
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
330
a3d912c4 » sandal
2006-10-10 fix for ticket:95. we should make practice of putting the trunk + sta…
331 if options[:data]
6251d6d9 » sandal
2007-08-10 optimization to table's constructor
332 options[:data].each do |e|
333 if e.kind_of?(Record)
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
334 e = if @column_names.empty? or
6251d6d9 » sandal
2007-08-10 optimization to table's constructor
335 e.attributes.all? { |a| a.kind_of?(Numeric) }
336 e.to_a
3d5285d6 » sandal
2007-04-17 Table.new optimization and benchmarks for manipulations
337 else
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
338 e.to_hash.values_at(*@column_names)
3d5285d6 » sandal
2007-04-17 Table.new optimization and benchmarks for manipulations
339 end
6251d6d9 » sandal
2007-08-10 optimization to table's constructor
340 end
ecd5ef55 » sandal
2007-08-28 Fighting with phantoms in the night
341 r = recordize(e)
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
342
2f130187 » sandal
2007-08-24 Merging in data feeders
343 feeder << r
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
344 end
345 end
346
347 yield(feeder) if block_given?
349aca9c » sandal
2006-07-17 started work on Table
348 end
349
41843932 » mikem836
2007-05-02 docs for table, fix for #270
350 # This Table's column names
349aca9c » sandal
2006-07-17 started work on Table
351 attr_reader :column_names
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
352
41843932 » mikem836
2007-05-02 docs for table, fix for #270
353 # This Table's data
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
354 attr_reader :data
355
903c6429 » sandal
2007-04-18 Minor move of requires
356 require "forwardable"
357 extend Forwardable
7b7f68d0 » sandal
2007-01-14 Collection now a module
358 def_delegators :@data, :each, :length, :size, :empty?, :[]
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
359
360 # Sets the column names for this table. <tt>new_column_names</tt> should
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
361 # be an array listing the names of the columns.
362 #
363 # Example:
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
364 #
365 # table = Table.new :data => [[1,2,3], [3,4,5]],
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
366 # :column_names => %w[a b c]
8864d46a » jh
2006-08-14 - added some api documentation to Ruport::Data::Table and Array
367 #
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
368 # table.column_names = %w[e f g]
369 #
b282e50b » sandal
2007-01-18 Thanks to Michael Milner for the patch
370 def column_names=(new_column_names)
371 columns = new_column_names.zip(@column_names)
372 @column_names.replace(new_column_names.dup)
373 unless @data.empty?
374 each { |r|
375 columns.each_with_index { |x,i|
376 if x[1].nil?
377 r.rename_attribute(i,x[0])
378 elsif x[1] != x[0]
379 r.rename_attribute(x[1],x[0],false)
380 end
381 }
382 r.send(:reindex, @column_names)
3d56ab3f » mikem836
2007-04-10 Documentation and whitespace for Table and Group
383 }
384 end
349aca9c » sandal
2006-07-17 started work on Table
385 end
386
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
387 # Compares this Table to another Table and returns <tt>true</tt> if
388 # both the <tt>data</tt> and <tt>column_names</tt> are equal.
389 #
390 # Example:
391 #
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
392 # one = Table.new :data => [[1,2], [3,4]],
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
393 # :column_names => %w[a b]
394 #
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
395 # two = Table.new :data => [[1,2], [3,4]],
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
396 # :column_names => %w[a b]
397 #
8864d46a » jh
2006-08-14 - added some api documentation to Ruport::Data::Table and Array
398 # one.eql?(two) #=> true
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
399 #
d2bc52ac » sandal
2006-08-05 added in Array#to_table and Collection#to_set
400 def eql?(other)
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
401 data.eql?(other.data) && column_names.eql?(other.column_names)
d2bc52ac » sandal
2006-08-05 added in Array#to_table and Collection#to_set
402 end
e2a1cf9a » sandal
2006-09-22 see CHANGELOG and examples
403
d2bc52ac » sandal
2006-08-05 added in Array#to_table and Collection#to_set
404 alias_method :==, :eql?
405
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
406 # Used to add extra data to the Table. <tt>row</tt> can be an Array,
ebf49db3 » sandal
2007-04-21 Table doc cleanup
407 # Hash or Record. It also can be anything that implements a meaningful
41843932 » mikem836
2007-05-02 docs for table, fix for #270
408 # to_hash or to_ary.
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
409 #
410 # Example:
411 #
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
412 # data = Table.new :data => [[1,2], [3,4]],
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
413 # :column_names => %w[a b]
8864d46a » jh
2006-08-14 - added some api documentation to Ruport::Data::Table and Array
414 # data << [8,9]
415 # data << { :a => 4, :b => 5}
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
416 # data << Record.new [5,6], :attributes => %w[a b]
417 #
6892ffe8 » sandal
2007-04-18 Table#<< refactoring
418 def <<(row)
6251d6d9 » sandal
2007-08-10 optimization to table's constructor
419 @data << recordize(row)
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
420 return self
421 end
422
d6396031 » Odaeus
2010-05-14 Tidy up add_row method and add tests.
423 # Add a row to a certain location within the existing table.
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
424 #
2e61f13b » Michael Pope
2010-04-29 Add example to add_row comments
425 # data.add_row([8,9], :position => 0)
426 #
427 #
d6396031 » Odaeus
2010-05-14 Tidy up add_row method and add tests.
428 def add_row(row_data, options={})
429 @data.insert(options[:position] || @data.length, recordize(row_data))
430 return self
41134cc6 » map7
2010-04-28 Added an add_row function to allow you to add a row to an existing ta…
431 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
432
41843932 » mikem836
2007-05-02 docs for table, fix for #270
433 # Returns the record class constant being used by the table.
0e563dff » sandal
2007-02-03 Thanks to Evan Weaver for helping me fix this marshalling bug
434 def record_class
1b42f3fb » sandal
2007-02-07 Let's not live on the edge: http://pastie.textmate.org/38576
435 @record_class.split("::").inject(Class) { |c,el| c.send(:const_get,el) }
0e563dff » sandal
2007-02-03 Thanks to Evan Weaver for helping me fix this marshalling bug
436 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
437
148c62ca » sandal
2007-01-26 minor doc patches for ruport and config
438 # Used to merge two Tables by rows.
41843932 » mikem836
2007-05-02 docs for table, fix for #270
439 # Raises an ArgumentError if the Tables don't have identical columns.
600962b2 » dudley
2006-09-25 Implemented Table#+
440 #
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
441 # Example:
442 #
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
443 # inky = Table.new :data => [[1,2], [3,4]],
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
444 # :column_names => %w[a b]
445 #
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
446 # blinky = Table.new :data => [[5,6]],
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
447 # :column_names => %w[a b]
448 #
600962b2 » dudley
2006-09-25 Implemented Table#+
449 # sue = inky + blinky
450 # sue.data #=> [[1,2],[3,4],[5,6]]
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
451 #
600962b2 » dudley
2006-09-25 Implemented Table#+
452 def +(other)
453 raise ArgumentError unless other.column_names == @column_names
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
454 self.class.new( :column_names => @column_names,
216bc3bd » sandal
2007-04-02 fixed several bugs... ruport-util broken though
455 :data => @data + other.data,
456 :record_class => record_class )
600962b2 » dudley
2006-09-25 Implemented Table#+
457 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
458
41843932 » mikem836
2007-05-02 docs for table, fix for #270
459 # Allows you to change the order of, or reduce the number of columns in a
460 # Table.
461 #
462 # Example:
463 #
464 # a = Table.new :data => [[1,2,3],[4,5,6]], :column_names => %w[a b c]
465 # a.reorder("b","c","a")
466 # a.column_names #=> ["b","c","a"]
467 #
468 # a = Table.new :data => [[1,2,3],[4,5,6]], :column_names => %w[a b c]
469 # a.reorder(1,2,0)
470 # a.column_names #=> ["b","c","a"]
471 #
472 # a = Table.new :data => [[1,2,3],[4,5,6]], :column_names => %w[a b c]
473 # a.reorder(0,2)
474 # a.column_names #=> ["a","c"]
475 #
25bcd052 » sandal
2007-01-15 This removes Table#reorder\! and makes Table#reorder destructive. It …
476 def reorder(*indices)
1760febd » mikem836
2007-04-18 fix for Table#reorder
477 raise(ArgumentError,"Can't reorder without column names set!") if
478 @column_names.empty?
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
479
d605f41c » sandal
2006-08-13 reorder now can take an array
480 indices = indices[0] if indices[0].kind_of? Array
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
481
482 if indices.all? { |i| i.kind_of? Integer }
483 indices.map! { |i| @column_names[i] }
1760febd » mikem836
2007-04-18 fix for Table#reorder
484 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
485
1760febd » mikem836
2007-04-18 fix for Table#reorder
486 reduce(indices)
41843932 » mikem836
2007-05-02 docs for table, fix for #270
487 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
488
41843932 » mikem836
2007-05-02 docs for table, fix for #270
489 # Adds an extra column to the Table.
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
490 #
41843932 » mikem836
2007-05-02 docs for table, fix for #270
491 # Available Options:
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
492 # <b><tt>:default</tt></b>:: The default value to use for the column in
06171f67 » mikem836
2007-04-18 doc patch
493 # existing rows. Set to nil if not specified.
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
494 #
0b01c4b9 » sandal
2007-01-14 some improvements to add_column
495 # <b><tt>:position</tt></b>:: Inserts the column at the indicated position
496 # number.
497 #
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
498 # <b><tt>:before</tt></b>:: Inserts the new column before the column
41843932 » mikem836
2007-05-02 docs for table, fix for #270
499 # indicated (by name).
0b01c4b9 » sandal
2007-01-14 some improvements to add_column
500 #
501 # <b><tt>:after</tt></b>:: Inserts the new column after the column
41843932 » mikem836
2007-05-02 docs for table, fix for #270
502 # indicated (by name).
0b01c4b9 » sandal
2007-01-14 some improvements to add_column
503 #
504 # If a block is provided, it will be used to build up the column.
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
505 #
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
506 # Example:
507 #
973393e8 » Odaeus
2010-11-20 Convert Table() calls Table.new in code and comments.
508 # data = Table.new("a","b") { |t| t << [1,2] << [3,4] }
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
509 #
28e261b1 » mikem836
2007-02-07 doc updates
510 # # basic usage, column full of 1's
25bcd052 » sandal
2007-01-15 This removes Table#reorder\! and makes Table#reorder destructive. It …
511 # data.add_column 'new_column', :default => 1
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
512 #
28e261b1 » mikem836
2007-02-07 doc updates
513 # # new empty column before new_column
25bcd052 » sandal
2007-01-15 This removes Table#reorder\! and makes Table#reorder destructive. It …
514 # data.add_column 'new_col2', :before => 'new_column'
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
515 #
25bcd052 » sandal
2007-01-15 This removes Table#reorder\! and makes Table#reorder destructive. It …
516 # # new column placed just after column a
28e261b1 » mikem836
2007-02-07 doc updates
517 # data.add_column 'new_col3', :position => 1
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
518 #
25bcd052 » sandal
2007-01-15 This removes Table#reorder\! and makes Table#reorder destructive. It …
519 # # new column built via a block, added at the end of the table
520 # data.add_column("new_col4") { |r| r.a + r.b }
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
521 #
ecd5ef55 » sandal
2007-08-28 Fighting with phantoms in the night
522 def add_column(name,options={})
0b01c4b9 » sandal
2007-01-14 some improvements to add_column
523 if pos = options[:position]
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
524 column_names.insert(pos,name)
0b01c4b9 » sandal
2007-01-14 some improvements to add_column
525 elsif pos = options[:after]
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
526 column_names.insert(column_names.index(pos)+1,name)
0b01c4b9 » sandal
2007-01-14 some improvements to add_column
527 elsif pos = options[:before]
528 column_names.insert(column_names.index(pos),name)
529 else
530 column_names << name
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
531 end
0b01c4b9 » sandal
2007-01-14 some improvements to add_column
532
b361a043 » sandal
2006-08-08 some work on Table
533 if block_given?
830b0972 » sandal
2007-01-14 :fill becomes :default in Table#add_column
534 each { |r| r[name] = yield(r) || options[:default] }
b361a043 » sandal
2006-08-08 some work on Table
535 else
830b0972 » sandal
2007-01-14 :fill becomes :default in Table#add_column
536 each { |r| r[name] = options[:default] }
98f93727 » dudley
2006-09-25 Made Table#append_column return self.
537 end; self
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
538 end
539
41843932 » mikem836
2007-05-02 docs for table, fix for #270
540 # Add multiple extra columns to the Table. See <tt>add_column</tt> for
541 # a list of available options.
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
542 #
41843932 » mikem836
2007-05-02 docs for table, fix for #270
543 # Example:
544 #
973393e8 » Odaeus
2010-11-20 Convert Table() calls Table.new in code and comments.
545 # data = Table.new("a","b") { |t| t << [1,2] << [3,4] }
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
546 #
41843932 » mikem836
2007-05-02 docs for table, fix for #270
547 # data.add_columns ['new_column_1','new_column_2'], :default => 1
548 #
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
549 def add_columns(names,options={})
fa22c96e » sandal
2007-03-03 Fix for #157
550 raise "Greg isn't smart enough to figure this out.\n"+
ecd5ef55 » sandal
2007-08-28 Fighting with phantoms in the night
551 "Send ideas in at http://list.rubyreports.org" if block_given?
fa22c96e » sandal
2007-03-03 Fix for #157
552 need_reverse = !!(options[:after] || options[:position])
553 names = names.reverse if need_reverse
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
554 names.each { |n| add_column(n,options) }
fa22c96e » sandal
2007-03-03 Fix for #157
555 self
ca525dbc » sandal
2007-01-15 Use Record#to_a instead of Record#data if you want arrays
556 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
557
25bcd052 » sandal
2007-01-15 This removes Table#reorder\! and makes Table#reorder destructive. It …
558 # Removes the given column from the table. May use name or position.
559 #
560 # Example:
561 #
562 # table.remove_column(0) #=> removes the first column
563 # table.remove_column("apple") #=> removes column named apple
3d56ab3f » mikem836
2007-04-10 Documentation and whitespace for Table and Group
564 #
ecd5ef55 » sandal
2007-08-28 Fighting with phantoms in the night
565 def remove_column(col)
566 col = column_names[col] if col.kind_of? Fixnum
ca525dbc » sandal
2007-01-15 Use Record#to_a instead of Record#data if you want arrays
567 column_names.delete(col)
568 each { |r| r.send(:delete,col) }
d3879475 » sandal
2007-01-21 Chris Carter's table#remove_columns patch with minor modification to …
569 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
570
d3879475 » sandal
2007-01-21 Chris Carter's table#remove_columns patch with minor modification to …
571 # Removes multiple columns from the table. May use name or position
572 # Will autosplat arrays.
573 #
574 # Example:
575 # table.remove_columns('a','b','c')
576 # table.remove_columns([0,1])
3d56ab3f » mikem836
2007-04-10 Documentation and whitespace for Table and Group
577 #
d3879475 » sandal
2007-01-21 Chris Carter's table#remove_columns patch with minor modification to …
578 def remove_columns(*cols)
579 cols = cols[0] if cols[0].kind_of? Array
580 cols.each { |col| remove_column(col) }
581 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
582
41843932 » mikem836
2007-05-02 docs for table, fix for #270
583 # Renames a column. Will update Record attributes as well.
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
584 #
25bcd052 » sandal
2007-01-15 This removes Table#reorder\! and makes Table#reorder destructive. It …
585 # Example:
586 #
587 # old_values = table.map { |r| r.a }
588 # table.rename_column("a","zanzibar")
589 # new_values = table.map { |r| r.zanzibar }
590 # old_values == new_values #=> true
591 # table.column_names.include?("a") #=> false
3d56ab3f » mikem836
2007-04-10 Documentation and whitespace for Table and Group
592 #
ce6f8391 » sandal
2007-01-15 improvements to table
593 def rename_column(old_name,new_name)
853db0b1 » sandal
2008-03-31 Making Table#rename_column fail silently
594 index = column_names.index(old_name) or return
595 self.column_names[index] = new_name
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
596 each { |r| r.rename_attribute(old_name,new_name,false)}
ce6f8391 » sandal
2007-01-15 improvements to table
597 end
ed4fdead » mikem836
2007-03-09 Fix for #176, Table#rename_columns
598
599 # Renames multiple columns. Takes either a hash of "old" => "new"
600 # names or two arrays of names %w[old names],%w[new names].
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
601 #
ed4fdead » mikem836
2007-03-09 Fix for #176, Table#rename_columns
602 # Example:
603 #
604 # table.column_names #=> ["a", "b"]
605 # table.rename_columns ["a", "b"], ["c", "d"]
606 # table.column_names #=> ["c", "d"]
607 #
608 # table.column_names #=> ["a", "b"]
609 # table.rename_columns {"a" => "c", "b" => "d"}
610 # table.column_names #=> ["c", "d"]
611 #
97711e96 » sandal
2007-04-11 fix for #249
612 def rename_columns(old_cols=nil,new_cols=nil)
613 if block_given?
614 if old_cols
615 old_cols.each { |c| rename_column(c,yield(c)) }
616 else
617 column_names.each { |c| rename_column(c,yield(c)) }
618 end
619 return
620 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
621
97711e96 » sandal
2007-04-11 fix for #249
622 raise ArgumentError unless old_cols
623
ed4fdead » mikem836
2007-03-09 Fix for #176, Table#rename_columns
624 if new_cols
625 raise ArgumentError,
626 "odd number of arguments" unless old_cols.size == new_cols.size
627 h = Hash[*old_cols.zip(new_cols).flatten]
628 else
629 h = old_cols
630 end
631 h.each {|old,new| rename_column(old,new) }
632 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
633
25bcd052 » sandal
2007-01-15 This removes Table#reorder\! and makes Table#reorder destructive. It …
634 # Exchanges one column with another.
635 #
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
636 # Example:
25bcd052 » sandal
2007-01-15 This removes Table#reorder\! and makes Table#reorder destructive. It …
637 #
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
638 # >> a = Table.new(%w[a b c]) { |t| t << [1,2,3] << [4,5,6] }
25bcd052 » sandal
2007-01-15 This removes Table#reorder\! and makes Table#reorder destructive. It …
639 # >> puts a
640 # +-----------+
641 # | a | b | c |
642 # +-----------+
643 # | 1 | 2 | 3 |
644 # | 4 | 5 | 6 |
645 # +-----------+
646 # >> a.swap_column("a","c")
647 # >> puts a
648 # +-----------+
649 # | c | b | a |
650 # +-----------+
651 # | 3 | 2 | 1 |
652 # | 6 | 5 | 4 |
653 # +-----------+
3d56ab3f » mikem836
2007-04-10 Documentation and whitespace for Table and Group
654 #
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
655 def swap_column(a,b)
ce6f8391 » sandal
2007-01-15 improvements to table
656 if [a,b].all? { |r| r.kind_of? Fixnum }
657 col_a,col_b = column_names[a],column_names[b]
658 column_names[a] = col_b
659 column_names[b] = col_a
660 else
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
661 a_ind, b_ind = [column_names.index(a), column_names.index(b)]
ce6f8391 » sandal
2007-01-15 improvements to table
662 column_names[b_ind] = a
663 column_names[a_ind] = b
664 end
665 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
666
25bcd052 » sandal
2007-01-15 This removes Table#reorder\! and makes Table#reorder destructive. It …
667 # Allows you to specify a new column to replace an existing column
668 # in your table via a block.
669 #
670 # Example:
671 #
973393e8 » Odaeus
2010-11-20 Convert Table() calls Table.new in code and comments.
672 # >> a = Table.new(%w[a b c]) { |t| t << [1,2,3] << [4,5,6] }
25bcd052 » sandal
2007-01-15 This removes Table#reorder\! and makes Table#reorder destructive. It …
673 # >> a.replace_column("c","c2") { |r| r.c * 2 + r.a }
674 #
675 # >> puts a
676 # +------------+
677 # | a | b | c2 |
678 # +------------+
679 # | 1 | 2 | 7 |
680 # | 4 | 5 | 16 |
681 # +------------+
3d56ab3f » mikem836
2007-04-10 Documentation and whitespace for Table and Group
682 #
f7d07e57 » sandal
2007-02-28 Fix for #169 and also basic tests for Table() iterators
683 def replace_column(old_col,new_col=nil,&block)
684 if new_col
685 add_column(new_col,:after => old_col,&block)
686 remove_column(old_col)
687 else
688 each { |r| r[old_col] = yield(r) }
689 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
690 end
691
25bcd052 » sandal
2007-01-15 This removes Table#reorder\! and makes Table#reorder destructive. It …
692 # Generates a sub table
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
693 #
25bcd052 » sandal
2007-01-15 This removes Table#reorder\! and makes Table#reorder destructive. It …
694 # Examples:
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
695 #
25bcd052 » sandal
2007-01-15 This removes Table#reorder\! and makes Table#reorder destructive. It …
696 # table = [[1,2,3,4],[5,6,7,8],[9,10,11,12]].to_table(%w[a b c d])
697 #
698 # Using column_names and a range:
699 #
12ea24db » mikem836
2007-03-07 Doc patches for Table
700 # sub_table = table.sub_table(%w[a b],1..-1)
701 # sub_table == [[5,6],[9,10]].to_table(%w[a b]) #=> true
25bcd052 » sandal
2007-01-15 This removes Table#reorder\! and makes Table#reorder destructive. It …
702 #
703 # Using just column_names:
704 #
705 # sub_table = table.sub_table(%w[a d])
706 # sub_table == [[1,4],[5,8],[9,12]].to_table(%w[a d]) #=> true
707 #
708 # Using column_names and a block:
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
709 #
710 # sub_table = table.sub_table(%w[d b]) { |r| r.a < 6 }
711 # sub_table == [[4,2],[8,6]].to_table(%w[d b]) #=> true
20f0a0d8 » sandal
2007-04-17 Fix for #189
712 #
713 # Using a range for row reduction:
714 # sub_table = table.sub_table(1..-1)
715 # sub_table == [[5,6,7,8],[9,10,11,12]].to_table(%w[a b c d]) #=> true
25bcd052 » sandal
2007-01-15 This removes Table#reorder\! and makes Table#reorder destructive. It …
716 #
717 # Using just a block:
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
718 #
25bcd052 » sandal
2007-01-15 This removes Table#reorder\! and makes Table#reorder destructive. It …
719 # sub_table = table.sub_table { |r| r.c > 10 }
720 # sub_table == [[9,10,11,12]].to_table(%w[a b c d]) #=> true
721 #
ecd5ef55 » sandal
2007-08-28 Fighting with phantoms in the night
722 def sub_table(cor=column_names,range=nil,&block)
723 if range
20f0a0d8 » sandal
2007-04-17 Fix for #189
724 self.class.new(:column_names => cor,:data => data[range])
ecd5ef55 » sandal
2007-08-28 Fighting with phantoms in the night
725 elsif cor.kind_of?(Range)
726 self.class.new(:column_names => column_names,:data => data[cor])
727 elsif block
20f0a0d8 » sandal
2007-04-17 Fix for #189
728 self.class.new( :column_names => cor, :data => data.select(&block))
3d5285d6 » sandal
2007-04-17 Table.new optimization and benchmarks for manipulations
729 else
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
730 self.class.new( :column_names => cor, :data => data)
731 end
8faf9038 » sandal
2007-01-15 Table#sub_table, where have you been all my life?
732 end
da384d01 » sandal
2007-03-01 fix for #171
733
3d56ab3f » mikem836
2007-04-10 Documentation and whitespace for Table and Group
734 # Generates a sub table in place, modifying the receiver. See documentation
735 # for <tt>sub_table</tt>.
736 #
da384d01 » sandal
2007-03-01 fix for #171
737 def reduce(columns=column_names,range=nil,&block)
738 t = sub_table(columns,range,&block)
739 @data = t.data
740 @column_names = t.column_names
741 self
742 end
743
744 alias_method :sub_table!, :reduce
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
745
3d56ab3f » mikem836
2007-04-10 Documentation and whitespace for Table and Group
746 # Returns an array of values for the given column name.
747 #
41843932 » mikem836
2007-05-02 docs for table, fix for #270
748 # Example:
749 #
750 # table = [[1,2],[3,4],[5,6]].to_table(%w[col1 col2])
751 # table.column("col1") #=> [1,3,5]
752 #
61569587 » sandal
2007-01-16 Table#column
753 def column(name)
267bd5ff » sandal
2007-01-22 fix for #141
754 case(name)
755 when Integer
3d56ab3f » mikem836
2007-04-10 Documentation and whitespace for Table and Group
756 unless column_names.empty?
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
757 raise ArgumentError if name > column_names.length
267bd5ff » sandal
2007-01-22 fix for #141
758 end
759 else
760 raise ArgumentError unless column_names.include?(name)
761 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
762
3d56ab3f » mikem836
2007-04-10 Documentation and whitespace for Table and Group
763 map { |r| r[name] }
61569587 » sandal
2007-01-16 Table#column
764 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
765
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
766 # Calculates sums. If a column name or index is given, it will try to
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
767 # convert each element of that column to an integer or float
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
768 # and add them together.
78c243b1 » sandal
2006-09-16 reinstated sigma/sum (ticket:72)
769 #
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
770 # If a block is given, it yields each Record so that you can do your own
1dcd3f67 » dudley
2006-11-11 Update api docs for Table.
771 # calculation.
19d55244 » sandal
2006-10-10 fix for ticket:85 . Table#sum() now autocoerces numbers
772 #
78c243b1 » sandal
2006-09-16 reinstated sigma/sum (ticket:72)
773 # Example:
774 #
84c33ee3 » sandal
2006-11-11 blah
775 # table = [[1,2],[3,4],[5,6]].to_table(%w[col1 col2])
776 # table.sigma("col1") #=> 9
777 # table.sigma(0) #=> 9
778 # table.sigma { |r| r.col1 + r.col2 } #=> 21
779 # table.sigma { |r| r.col2 + 1 } #=> 15
78c243b1 » sandal
2006-09-16 reinstated sigma/sum (ticket:72)
780 #
781 def sigma(column=nil)
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
782 inject(0) { |s,r|
19d55244 » sandal
2006-10-10 fix for ticket:85 . Table#sum() now autocoerces numbers
783 if column
27c1dde5 » sandal
2007-04-01 ooh... found a baby bug
784 s + if r.get(column).kind_of? Numeric
785 r.get(column)
19d55244 » sandal
2006-10-10 fix for ticket:85 . Table#sum() now autocoerces numbers
786 else
27c1dde5 » sandal
2007-04-01 ooh... found a baby bug
787 r.get(column) =~ /\./ ? r.get(column).to_f : r.get(column).to_i
19d55244 » sandal
2006-10-10 fix for ticket:85 . Table#sum() now autocoerces numbers
788 end
789 else
ecd5ef55 » sandal
2007-08-28 Fighting with phantoms in the night
790 s + yield(r)
19d55244 » sandal
2006-10-10 fix for ticket:85 . Table#sum() now autocoerces numbers
791 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
792 }
78c243b1 » sandal
2006-09-16 reinstated sigma/sum (ticket:72)
793 end
794
795 alias_method :sum, :sigma
4e771586 » sandal
2007-01-14 small cleanup
796
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
797 # Returns a sorted table. If col_names is specified,
23fc4ad7 » sandal
2007-08-28 sort_rows_by doc patch from Stefan Mahlitz.
798 # the block is ignored and the table is sorted by the named columns.
799 #
800 # The second argument specifies sorting options. Currently only
801 # :order is supported. Default order is ascending, to sort decending
802 # use :order => :descending
2818e5e6 » dudley
2006-11-15 Add Table#sort_rows_by (Thanks to Stefan Mahlitz for the patch!)
803 #
804 # Example:
805 #
806 # table = [[4, 3], [2, 5], [7, 1]].to_table(%w[col1 col2 ])
807 #
808 # # returns a new table sorted by col1
809 # table.sort_rows_by {|r| r["col1"]}
810 #
23fc4ad7 » sandal
2007-08-28 sort_rows_by doc patch from Stefan Mahlitz.
811 # # returns a new table sorted by col1, in descending order
812 # table.sort_rows_by(nil, :order => :descending) {|r| r["col1"]}
813 #
2818e5e6 » dudley
2006-11-15 Add Table#sort_rows_by (Thanks to Stefan Mahlitz for the patch!)
814 # # returns a new table sorted by col2
23fc4ad7 » sandal
2007-08-28 sort_rows_by doc patch from Stefan Mahlitz.
815 # table.sort_rows_by(["col2"])
816 #
817 # # returns a new table sorted by col2, descending order
818 # table.sort_rows_by("col2", :order => :descending)
2818e5e6 » dudley
2006-11-15 Add Table#sort_rows_by (Thanks to Stefan Mahlitz for the patch!)
819 #
820 # # returns a new table sorted by col1, then col2
23fc4ad7 » sandal
2007-08-28 sort_rows_by doc patch from Stefan Mahlitz.
821 # table.sort_rows_by(["col1", "col2"])
822 #
823 # # returns a new table sorted by col1, then col2, in descending order
824 # table.sort_rows_by(["col1", "col2"], :order => descending)
2818e5e6 » dudley
2006-11-15 Add Table#sort_rows_by (Thanks to Stefan Mahlitz for the patch!)
825 #
ecd5ef55 » sandal
2007-08-28 Fighting with phantoms in the night
826 def sort_rows_by(col_names=nil, options={}, &block)
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
827 # stabilizer is needed because of
2818e5e6 » dudley
2006-11-15 Add Table#sort_rows_by (Thanks to Stefan Mahlitz for the patch!)
828 # http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/170565
ecd5ef55 » sandal
2007-08-28 Fighting with phantoms in the night
829 stabilizer = 0
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
830
831 nil_rows, sortable = partition do |r|
832 Array(col_names).any? { |c| r[c].nil? }
83d06bae » sandal
2007-06-23 Enhanced sorting support
833 end
2818e5e6 » dudley
2006-11-15 Add Table#sort_rows_by (Thanks to Stefan Mahlitz for the patch!)
834
835 data_array =
2f633a22 » sandal
2006-12-22 fix for #121
836 if col_names
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
837 sortable.sort_by do |r|
2818e5e6 » dudley
2006-11-15 Add Table#sort_rows_by (Thanks to Stefan Mahlitz for the patch!)
838 stabilizer += 1
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
839 [Array(col_names).map {|col| r[col]}, stabilizer]
2818e5e6 » dudley
2006-11-15 Add Table#sort_rows_by (Thanks to Stefan Mahlitz for the patch!)
840 end
841 else
ffa2ca48 » sandal
2007-06-27 Thanks to gregwebs for the refactoring suggestion
842 sortable.sort_by(&block)
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
843 end
844
83d06bae » sandal
2007-06-23 Enhanced sorting support
845 data_array += nil_rows
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
846 data_array.reverse! if options[:order] == :descending
2818e5e6 » dudley
2006-11-15 Add Table#sort_rows_by (Thanks to Stefan Mahlitz for the patch!)
847
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
848 table = self.class.new( :data => data_array,
216bc3bd » sandal
2007-04-02 fixed several bugs... ruport-util broken though
849 :column_names => @column_names,
850 :record_class => record_class )
2818e5e6 » dudley
2006-11-15 Add Table#sort_rows_by (Thanks to Stefan Mahlitz for the patch!)
851
852 return table
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
853 end
854
41843932 » mikem836
2007-05-02 docs for table, fix for #270
855 # Same as Table#sort_rows_by, but self modifying.
856 # See <tt>sort_rows_by</tt> for documentation.
857 #
ee3561e0 » sandal
2007-06-23 Fixed a bug in 1.1.1107
858 def sort_rows_by!(col_names=nil,options={},&block)
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
859 table = sort_rows_by(col_names,options,&block)
68c250f6 » sandal
2007-04-17 Fix for #238
860 @data = table.data
2818e5e6 » dudley
2006-11-15 Add Table#sort_rows_by (Thanks to Stefan Mahlitz for the patch!)
861 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
862
41843932 » mikem836
2007-05-02 docs for table, fix for #270
863 # Get an array of records from the Table limited by the criteria specified.
864 #
865 # Example:
866 #
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
867 # table = Table.new :data => [[1,2,3], [1,4,6], [4,5,6]],
41843932 » mikem836
2007-05-02 docs for table, fix for #270
868 # :column_names => %w[a b c]
869 # table.rows_with(:a => 1) #=> [[1,2,3], [1,4,6]]
870 # table.rows_with(:a => 1, :b => 4) #=> [[1,4,6]]
871 # table.rows_with_a(1) #=> [[1,2,3], [1,4,6]]
872 # table.rows_with(%w[a b]) {|a,b| [a,b] == [1,4] } #=> [[1,4,6]]
873 #
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
874 def rows_with(columns,&block)
db44e65f » sandal
2007-01-18 Train modifications
875 select { |r|
876 if block
877 block[*(columns.map { |c| r.get(c) })]
878 else
879 columns.all? { |k,v| r.get(k) == v }
880 end
881 }
882 end
ecd5ef55 » sandal
2007-08-28 Fighting with phantoms in the night
883
4d5fc9d6 » map7
2010-04-28 Added row search ability which returns position
884 # Search row for a string and return the position
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
885 #
d6396031 » Odaeus
2010-05-14 Tidy up add_row method and add tests.
886 # Example:
887 #
888 # table = Table.new :data => [["Mow Lawn","50"], ["Sew","40"], ["Clean dishes","5"]],
4d5fc9d6 » map7
2010-04-28 Added row search ability which returns position
889 # :column_names => %w[task cost]
890 # table.row_search("Sew", :column => 0) #=> [[1,2,3], [1,4,6]]
891 #
93291fd9 » Michael Pope
2010-04-29 Added greater than and less than search abilities for row_search method
892 # Search for a number in column 0 greater than 999.
893 # result = table.row_search(999, :column => 0, :greater_than => true)
894 #
d6396031 » Odaeus
2010-05-14 Tidy up add_row method and add tests.
895 #
4d5fc9d6 » map7
2010-04-28 Added row search ability which returns position
896 def row_search(search, options={})
897 position = 0
d6396031 » Odaeus
2010-05-14 Tidy up add_row method and add tests.
898
4d5fc9d6 » map7
2010-04-28 Added row search ability which returns position
899 if column = options[:column]
900 self.each do |row|
d6396031 » Odaeus
2010-05-14 Tidy up add_row method and add tests.
901
93291fd9 » Michael Pope
2010-04-29 Added greater than and less than search abilities for row_search method
902 if gt=options[:greater_than]
903 return position if row[column] > search
904 end
905
906 if lt=options[:less_than]
907 return position if row[column] < search
908 end
d6396031 » Odaeus
2010-05-14 Tidy up add_row method and add tests.
909
93291fd9 » Michael Pope
2010-04-29 Added greater than and less than search abilities for row_search method
910 unless gt or lt
911 if row[column] =~ /#{search}/ # Search for part of or whole search text.
912 return position
913 end
4d5fc9d6 » map7
2010-04-28 Added row search ability which returns position
914 end
d6396031 » Odaeus
2010-05-14 Tidy up add_row method and add tests.
915
4d5fc9d6 » map7
2010-04-28 Added row search ability which returns position
916 position += 1
917 end
918 end
919 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
920
41843932 » mikem836
2007-05-02 docs for table, fix for #270
921 # Create a copy of the Table. Records will be copied as well.
4e771586 » sandal
2007-01-14 small cleanup
922 #
923 # Example:
924 #
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
925 # one = Table.new :data => [[1,2], [3,4]],
4e771586 » sandal
2007-01-14 small cleanup
926 # :column_names => %w[a b]
927 # two = one.dup
928 #
ccaf683a » mikem836
2007-04-10 initialize_copy for Table and Group
929 def initialize_copy(from)
930 @record_class = from.record_class.name
931 @column_names = from.column_names.dup
932 @data = []
933 from.data.each { |r| self << r.dup }
934 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
935
41843932 » mikem836
2007-05-02 docs for table, fix for #270
936 # Uses Ruport's built-in text formatter to render this Table into a String.
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
937 #
4e771586 » sandal
2007-01-14 small cleanup
938 # Example:
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
939 #
940 # data = Table.new :data => [[1,2], [3,4]],
4e771586 » sandal
2007-01-14 small cleanup
941 # :column_names => %w[a b]
942 # puts data.to_s
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
943 #
4e771586 » sandal
2007-01-14 small cleanup
944 def to_s
945 as(:text)
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
946 end
ddd4ce23 » sandal
2007-03-08 Free table headers for you lazies out there, and general refactoring
947
3d56ab3f » mikem836
2007-04-10 Documentation and whitespace for Table and Group
948 # Convert the Table into a Group using the supplied group name.
949 #
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
950 # data = Table.new :data => [[1,2], [3,4]],
3d56ab3f » mikem836
2007-04-10 Documentation and whitespace for Table and Group
951 # :column_names => %w[a b]
952 # group = data.to_group("my_group")
953 #
95f3d656 » mikem836
2007-04-19 move grouped_data to Group - fix for #251
954 def to_group(name=nil)
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
955 Group.new( :data => data,
95f3d656 » mikem836
2007-04-19 move grouped_data to Group - fix for #251
956 :column_names => column_names,
957 :name => name,
958 :record_class => record_class )
ddd4ce23 » sandal
2007-03-08 Free table headers for you lazies out there, and general refactoring
959 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
960
4e771586 » sandal
2007-01-14 small cleanup
961 # NOTE: does not respect tainted status
962 alias_method :clone, :dup
01005320 » sandal
2006-10-21 added in create_rag_group
963
db44e65f » sandal
2007-01-18 Train modifications
964 # Provides a shortcut for the <tt>as()</tt> method by converting a call to
965 # <tt>as(:format_name)</tt> into a call to <tt>to_format_name</tt>
41843932 » mikem836
2007-05-02 docs for table, fix for #270
966 #
967 # Also converts a call to <tt>rows_with_columnname</tt> to a call to
968 # <tt>rows_with(:columnname => args[0])</tt>.
969 #
c6d1475f » sandal
2007-03-10 Fix for #177
970 def method_missing(id,*args,&block)
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
971 return as($1.to_sym,*args,&block) if id.to_s =~ /^to_(.*)/
db44e65f » sandal
2007-01-18 Train modifications
972 return rows_with($1.to_sym => args[0]) if id.to_s =~ /^rows_with_(.*)/
973 super
974 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
975
2f130187 » sandal
2007-08-24 Merging in data feeders
976 def feed_element(row)
977 recordize(row)
978 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
979
980 private
981
6251d6d9 » sandal
2007-08-10 optimization to table's constructor
982 def recordize(row)
983 case row
984 when Array
985 normalize_array(row)
986 when Hash
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
987 normalize_hash(row)
988 when record_class
6251d6d9 » sandal
2007-08-10 optimization to table's constructor
989 recordize(normalize_record(row))
990 else
991 normalize_hash(row) rescue normalize_array(row)
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
992 end
993 end
994
6251d6d9 » sandal
2007-08-10 optimization to table's constructor
995 def normalize_hash(hash_obj)
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
996 hash_obj = hash_obj.to_hash
6251d6d9 » sandal
2007-08-10 optimization to table's constructor
997 raise ArgumentError unless @column_names
998 record_class.new(hash_obj, :attributes => @column_names)
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
999 end
1000
6251d6d9 » sandal
2007-08-10 optimization to table's constructor
1001 def normalize_record(record)
1002 record.send(column_names.empty? ? :to_a : :to_hash)
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
1003 end
1004
6251d6d9 » sandal
2007-08-10 optimization to table's constructor
1005 def normalize_array(array)
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
1006 attributes = @column_names.empty? ? nil : @column_names
1007 record_class.new(array.to_ary, :attributes => attributes)
1008 end
4e771586 » sandal
2007-01-14 small cleanup
1009 end
01005320 » sandal
2006-10-21 added in create_rag_group
1010 end
1011
2f633a22 » sandal
2006-12-22 fix for #121
1012
86f439fc » Odaeus
2010-11-20 Move Table() from Kernel to Ruport module.
1013 module Ruport
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
1014
2f633a22 » sandal
2006-12-22 fix for #121
1015 # Shortcut interface for creating Data::Tables
1016 #
1017 # Examples:
1018 #
1019 # t = Table(%w[a b c]) #=> creates a new empty table w. cols a,b,c
1020 # t = Table("a","b","c") #=> creates a new empty table w. cols a,b,c
1021 #
1022 # # allows building table inside of block, returns table object
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
1023 # t = Table(%w[a b c]) { |t| t << [1,2,3] }
2f633a22 » sandal
2006-12-22 fix for #121
1024 #
1025 # # allows loading table from CSV
4db6666b » sandal
2007-02-27 Smoking rock
1026 # # accepts all Data::Table.load options, including block (yields table,row)
2f633a22 » sandal
2006-12-22 fix for #121
1027 #
1028 # t = Table("foo.csv")
1029 # t = Table("bar.csv", :has_names => false)
1030 def Table(*args,&block)
0290d2fe » dinko
2006-12-24 git-svn-id: http://stonecode.svnrepository.com/svn/ruport/trunk@400 b…
1031 case(args[0])
51c5c307 » sandal
2006-12-24 minor cleanup
1032 when Array
b9afbdf5 » sandal
2007-01-27 Fix for #133. Need extra work on this, maybe
1033 opts = args[1] || {}
2f130187 » sandal
2007-08-24 Merging in data feeders
1034 Ruport::Data::Table.new(f={:column_names => args[0]}.merge(opts),&block)
51c5c307 » sandal
2006-12-24 minor cleanup
1035 when /\.csv/
86f439fc » Odaeus
2010-11-20 Move Table() from Kernel to Ruport module.
1036 Ruport::Data::Table.load(*args,&block)
d0d41596 » sandal
2007-02-25 Table() just got a little sexier
1037 when Hash
1038 if file = args[0].delete(:file)
86f439fc » Odaeus
2010-11-20 Move Table() from Kernel to Ruport module.
1039 Ruport::Data::Table.load(file,args[0],&block)
d0d41596 » sandal
2007-02-25 Table() just got a little sexier
1040 elsif string = args[0].delete(:string)
86f439fc » Odaeus
2010-11-20 Move Table() from Kernel to Ruport module.
1041 Ruport::Data::Table.parse(string,args[0],&block)
216bc3bd » sandal
2007-04-02 fixed several bugs... ruport-util broken though
1042 else
86f439fc » Odaeus
2010-11-20 Move Table() from Kernel to Ruport module.
1043 Ruport::Data::Table.new(args[0],&block)
d0d41596 » sandal
2007-02-25 Table() just got a little sexier
1044 end
51c5c307 » sandal
2006-12-24 minor cleanup
1045 else
2f130187 » sandal
2007-08-24 Merging in data feeders
1046 Ruport::Data::Table.new(:data => [], :column_names => args,&block)
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
1047 end
01005320 » sandal
2006-10-21 added in create_rag_group
1048 end
4e651641 » wpiekutowski
2011-01-30 Clean up whitespace in Data::Table and related tests
1049 end
Something went wrong with that request. Please try again.