File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -238,6 +238,34 @@ def drop_left
238
238
return tbl
239
239
end
240
240
241
+ #
242
+ # Build table from CSV dump
243
+ #
244
+ def self . new_from_csv ( csv )
245
+ # Read in or keep data, get CSV or die
246
+ if csv . is_a? ( String )
247
+ csv = File . file? ( csv ) ? CSV . read ( csv ) : CSV . parse ( csv )
248
+ end
249
+ # Adjust for skew
250
+ if csv . first == [ "Keys" , "Values" ]
251
+ csv . shift # drop marker
252
+ cols = [ ]
253
+ rows = [ ]
254
+ csv . each do |row |
255
+ cols << row . shift
256
+ rows << row
257
+ end
258
+ tbl = self . new ( 'Columns' => cols )
259
+ rows . in_groups_of ( cols . count ) { |r | tbl << r . flatten }
260
+ else
261
+ tbl = self . new ( 'Columns' => csv . shift )
262
+ while !csv . empty? do
263
+ tbl << csv . shift
264
+ end
265
+ end
266
+ return tbl
267
+ end
268
+
241
269
def []( *col_names )
242
270
tbl = self . class . new ( 'Indent' => self . indent ,
243
271
'Header' => self . header ,
You can’t perform that action at this time.
0 commit comments