csv 1.0.1 → 1.0.2
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.
- checksums.yaml +5 -5
- data/LICENSE.txt +12 -1
- data/README.md +6 -0
- data/lib/csv.rb +210 -820
- data/lib/{core_ext → csv/core_ext}/array.rb +0 -0
- data/lib/{core_ext → csv/core_ext}/string.rb +0 -0
- data/lib/csv/row.rb +388 -0
- data/lib/csv/table.rb +378 -0
- data/lib/csv/version.rb +6 -0
- data/news.md +63 -0
- metadata +23 -6
File without changes
|
File without changes
|
data/lib/csv/row.rb
ADDED
@@ -0,0 +1,388 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "forwardable"
|
4
|
+
|
5
|
+
class CSV
|
6
|
+
#
|
7
|
+
# A CSV::Row is part Array and part Hash. It retains an order for the fields
|
8
|
+
# and allows duplicates just as an Array would, but also allows you to access
|
9
|
+
# fields by name just as you could if they were in a Hash.
|
10
|
+
#
|
11
|
+
# All rows returned by CSV will be constructed from this class, if header row
|
12
|
+
# processing is activated.
|
13
|
+
#
|
14
|
+
class Row
|
15
|
+
#
|
16
|
+
# Construct a new CSV::Row from +headers+ and +fields+, which are expected
|
17
|
+
# to be Arrays. If one Array is shorter than the other, it will be padded
|
18
|
+
# with +nil+ objects.
|
19
|
+
#
|
20
|
+
# The optional +header_row+ parameter can be set to +true+ to indicate, via
|
21
|
+
# CSV::Row.header_row?() and CSV::Row.field_row?(), that this is a header
|
22
|
+
# row. Otherwise, the row is assumes to be a field row.
|
23
|
+
#
|
24
|
+
# A CSV::Row object supports the following Array methods through delegation:
|
25
|
+
#
|
26
|
+
# * empty?()
|
27
|
+
# * length()
|
28
|
+
# * size()
|
29
|
+
#
|
30
|
+
def initialize(headers, fields, header_row = false)
|
31
|
+
@header_row = header_row
|
32
|
+
headers.each { |h| h.freeze if h.is_a? String }
|
33
|
+
|
34
|
+
# handle extra headers or fields
|
35
|
+
@row = if headers.size >= fields.size
|
36
|
+
headers.zip(fields)
|
37
|
+
else
|
38
|
+
fields.zip(headers).each(&:reverse!)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Internal data format used to compare equality.
|
43
|
+
attr_reader :row
|
44
|
+
protected :row
|
45
|
+
|
46
|
+
### Array Delegation ###
|
47
|
+
|
48
|
+
extend Forwardable
|
49
|
+
def_delegators :@row, :empty?, :length, :size
|
50
|
+
|
51
|
+
# Returns +true+ if this is a header row.
|
52
|
+
def header_row?
|
53
|
+
@header_row
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns +true+ if this is a field row.
|
57
|
+
def field_row?
|
58
|
+
not header_row?
|
59
|
+
end
|
60
|
+
|
61
|
+
# Returns the headers of this row.
|
62
|
+
def headers
|
63
|
+
@row.map(&:first)
|
64
|
+
end
|
65
|
+
|
66
|
+
#
|
67
|
+
# :call-seq:
|
68
|
+
# field( header )
|
69
|
+
# field( header, offset )
|
70
|
+
# field( index )
|
71
|
+
#
|
72
|
+
# This method will return the field value by +header+ or +index+. If a field
|
73
|
+
# is not found, +nil+ is returned.
|
74
|
+
#
|
75
|
+
# When provided, +offset+ ensures that a header match occurs on or later
|
76
|
+
# than the +offset+ index. You can use this to find duplicate headers,
|
77
|
+
# without resorting to hard-coding exact indices.
|
78
|
+
#
|
79
|
+
def field(header_or_index, minimum_index = 0)
|
80
|
+
# locate the pair
|
81
|
+
finder = (header_or_index.is_a?(Integer) || header_or_index.is_a?(Range)) ? :[] : :assoc
|
82
|
+
pair = @row[minimum_index..-1].send(finder, header_or_index)
|
83
|
+
|
84
|
+
# return the field if we have a pair
|
85
|
+
if pair.nil?
|
86
|
+
nil
|
87
|
+
else
|
88
|
+
header_or_index.is_a?(Range) ? pair.map(&:last) : pair.last
|
89
|
+
end
|
90
|
+
end
|
91
|
+
alias_method :[], :field
|
92
|
+
|
93
|
+
#
|
94
|
+
# :call-seq:
|
95
|
+
# fetch( header )
|
96
|
+
# fetch( header ) { |row| ... }
|
97
|
+
# fetch( header, default )
|
98
|
+
#
|
99
|
+
# This method will fetch the field value by +header+. It has the same
|
100
|
+
# behavior as Hash#fetch: if there is a field with the given +header+, its
|
101
|
+
# value is returned. Otherwise, if a block is given, it is yielded the
|
102
|
+
# +header+ and its result is returned; if a +default+ is given as the
|
103
|
+
# second argument, it is returned; otherwise a KeyError is raised.
|
104
|
+
#
|
105
|
+
def fetch(header, *varargs)
|
106
|
+
raise ArgumentError, "Too many arguments" if varargs.length > 1
|
107
|
+
pair = @row.assoc(header)
|
108
|
+
if pair
|
109
|
+
pair.last
|
110
|
+
else
|
111
|
+
if block_given?
|
112
|
+
yield header
|
113
|
+
elsif varargs.empty?
|
114
|
+
raise KeyError, "key not found: #{header}"
|
115
|
+
else
|
116
|
+
varargs.first
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
# Returns +true+ if there is a field with the given +header+.
|
122
|
+
def has_key?(header)
|
123
|
+
!!@row.assoc(header)
|
124
|
+
end
|
125
|
+
alias_method :include?, :has_key?
|
126
|
+
alias_method :key?, :has_key?
|
127
|
+
alias_method :member?, :has_key?
|
128
|
+
|
129
|
+
#
|
130
|
+
# :call-seq:
|
131
|
+
# []=( header, value )
|
132
|
+
# []=( header, offset, value )
|
133
|
+
# []=( index, value )
|
134
|
+
#
|
135
|
+
# Looks up the field by the semantics described in CSV::Row.field() and
|
136
|
+
# assigns the +value+.
|
137
|
+
#
|
138
|
+
# Assigning past the end of the row with an index will set all pairs between
|
139
|
+
# to <tt>[nil, nil]</tt>. Assigning to an unused header appends the new
|
140
|
+
# pair.
|
141
|
+
#
|
142
|
+
def []=(*args)
|
143
|
+
value = args.pop
|
144
|
+
|
145
|
+
if args.first.is_a? Integer
|
146
|
+
if @row[args.first].nil? # extending past the end with index
|
147
|
+
@row[args.first] = [nil, value]
|
148
|
+
@row.map! { |pair| pair.nil? ? [nil, nil] : pair }
|
149
|
+
else # normal index assignment
|
150
|
+
@row[args.first][1] = value
|
151
|
+
end
|
152
|
+
else
|
153
|
+
index = index(*args)
|
154
|
+
if index.nil? # appending a field
|
155
|
+
self << [args.first, value]
|
156
|
+
else # normal header assignment
|
157
|
+
@row[index][1] = value
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
#
|
163
|
+
# :call-seq:
|
164
|
+
# <<( field )
|
165
|
+
# <<( header_and_field_array )
|
166
|
+
# <<( header_and_field_hash )
|
167
|
+
#
|
168
|
+
# If a two-element Array is provided, it is assumed to be a header and field
|
169
|
+
# and the pair is appended. A Hash works the same way with the key being
|
170
|
+
# the header and the value being the field. Anything else is assumed to be
|
171
|
+
# a lone field which is appended with a +nil+ header.
|
172
|
+
#
|
173
|
+
# This method returns the row for chaining.
|
174
|
+
#
|
175
|
+
def <<(arg)
|
176
|
+
if arg.is_a?(Array) and arg.size == 2 # appending a header and name
|
177
|
+
@row << arg
|
178
|
+
elsif arg.is_a?(Hash) # append header and name pairs
|
179
|
+
arg.each { |pair| @row << pair }
|
180
|
+
else # append field value
|
181
|
+
@row << [nil, arg]
|
182
|
+
end
|
183
|
+
|
184
|
+
self # for chaining
|
185
|
+
end
|
186
|
+
|
187
|
+
#
|
188
|
+
# A shortcut for appending multiple fields. Equivalent to:
|
189
|
+
#
|
190
|
+
# args.each { |arg| csv_row << arg }
|
191
|
+
#
|
192
|
+
# This method returns the row for chaining.
|
193
|
+
#
|
194
|
+
def push(*args)
|
195
|
+
args.each { |arg| self << arg }
|
196
|
+
|
197
|
+
self # for chaining
|
198
|
+
end
|
199
|
+
|
200
|
+
#
|
201
|
+
# :call-seq:
|
202
|
+
# delete( header )
|
203
|
+
# delete( header, offset )
|
204
|
+
# delete( index )
|
205
|
+
#
|
206
|
+
# Used to remove a pair from the row by +header+ or +index+. The pair is
|
207
|
+
# located as described in CSV::Row.field(). The deleted pair is returned,
|
208
|
+
# or +nil+ if a pair could not be found.
|
209
|
+
#
|
210
|
+
def delete(header_or_index, minimum_index = 0)
|
211
|
+
if header_or_index.is_a? Integer # by index
|
212
|
+
@row.delete_at(header_or_index)
|
213
|
+
elsif i = index(header_or_index, minimum_index) # by header
|
214
|
+
@row.delete_at(i)
|
215
|
+
else
|
216
|
+
[ ]
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
#
|
221
|
+
# The provided +block+ is passed a header and field for each pair in the row
|
222
|
+
# and expected to return +true+ or +false+, depending on whether the pair
|
223
|
+
# should be deleted.
|
224
|
+
#
|
225
|
+
# This method returns the row for chaining.
|
226
|
+
#
|
227
|
+
# If no block is given, an Enumerator is returned.
|
228
|
+
#
|
229
|
+
def delete_if(&block)
|
230
|
+
return enum_for(__method__) { size } unless block_given?
|
231
|
+
|
232
|
+
@row.delete_if(&block)
|
233
|
+
|
234
|
+
self # for chaining
|
235
|
+
end
|
236
|
+
|
237
|
+
#
|
238
|
+
# This method accepts any number of arguments which can be headers, indices,
|
239
|
+
# Ranges of either, or two-element Arrays containing a header and offset.
|
240
|
+
# Each argument will be replaced with a field lookup as described in
|
241
|
+
# CSV::Row.field().
|
242
|
+
#
|
243
|
+
# If called with no arguments, all fields are returned.
|
244
|
+
#
|
245
|
+
def fields(*headers_and_or_indices)
|
246
|
+
if headers_and_or_indices.empty? # return all fields--no arguments
|
247
|
+
@row.map(&:last)
|
248
|
+
else # or work like values_at()
|
249
|
+
all = []
|
250
|
+
headers_and_or_indices.each do |h_or_i|
|
251
|
+
if h_or_i.is_a? Range
|
252
|
+
index_begin = h_or_i.begin.is_a?(Integer) ? h_or_i.begin :
|
253
|
+
index(h_or_i.begin)
|
254
|
+
index_end = h_or_i.end.is_a?(Integer) ? h_or_i.end :
|
255
|
+
index(h_or_i.end)
|
256
|
+
new_range = h_or_i.exclude_end? ? (index_begin...index_end) :
|
257
|
+
(index_begin..index_end)
|
258
|
+
all.concat(fields.values_at(new_range))
|
259
|
+
else
|
260
|
+
all << field(*Array(h_or_i))
|
261
|
+
end
|
262
|
+
end
|
263
|
+
return all
|
264
|
+
end
|
265
|
+
end
|
266
|
+
alias_method :values_at, :fields
|
267
|
+
|
268
|
+
#
|
269
|
+
# :call-seq:
|
270
|
+
# index( header )
|
271
|
+
# index( header, offset )
|
272
|
+
#
|
273
|
+
# This method will return the index of a field with the provided +header+.
|
274
|
+
# The +offset+ can be used to locate duplicate header names, as described in
|
275
|
+
# CSV::Row.field().
|
276
|
+
#
|
277
|
+
def index(header, minimum_index = 0)
|
278
|
+
# find the pair
|
279
|
+
index = headers[minimum_index..-1].index(header)
|
280
|
+
# return the index at the right offset, if we found one
|
281
|
+
index.nil? ? nil : index + minimum_index
|
282
|
+
end
|
283
|
+
|
284
|
+
# Returns +true+ if +name+ is a header for this row, and +false+ otherwise.
|
285
|
+
def header?(name)
|
286
|
+
headers.include? name
|
287
|
+
end
|
288
|
+
alias_method :include?, :header?
|
289
|
+
|
290
|
+
#
|
291
|
+
# Returns +true+ if +data+ matches a field in this row, and +false+
|
292
|
+
# otherwise.
|
293
|
+
#
|
294
|
+
def field?(data)
|
295
|
+
fields.include? data
|
296
|
+
end
|
297
|
+
|
298
|
+
include Enumerable
|
299
|
+
|
300
|
+
#
|
301
|
+
# Yields each pair of the row as header and field tuples (much like
|
302
|
+
# iterating over a Hash). This method returns the row for chaining.
|
303
|
+
#
|
304
|
+
# If no block is given, an Enumerator is returned.
|
305
|
+
#
|
306
|
+
# Support for Enumerable.
|
307
|
+
#
|
308
|
+
def each(&block)
|
309
|
+
return enum_for(__method__) { size } unless block_given?
|
310
|
+
|
311
|
+
@row.each(&block)
|
312
|
+
|
313
|
+
self # for chaining
|
314
|
+
end
|
315
|
+
|
316
|
+
alias_method :each_pair, :each
|
317
|
+
|
318
|
+
#
|
319
|
+
# Returns +true+ if this row contains the same headers and fields in the
|
320
|
+
# same order as +other+.
|
321
|
+
#
|
322
|
+
def ==(other)
|
323
|
+
return @row == other.row if other.is_a? CSV::Row
|
324
|
+
@row == other
|
325
|
+
end
|
326
|
+
|
327
|
+
#
|
328
|
+
# Collapses the row into a simple Hash. Be warned that this discards field
|
329
|
+
# order and clobbers duplicate fields.
|
330
|
+
#
|
331
|
+
def to_h
|
332
|
+
hash = {}
|
333
|
+
each do |key, _value|
|
334
|
+
hash[key] = self[key] unless hash.key?(key)
|
335
|
+
end
|
336
|
+
hash
|
337
|
+
end
|
338
|
+
alias_method :to_hash, :to_h
|
339
|
+
|
340
|
+
alias_method :to_ary, :to_a
|
341
|
+
|
342
|
+
#
|
343
|
+
# Returns the row as a CSV String. Headers are not used. Equivalent to:
|
344
|
+
#
|
345
|
+
# csv_row.fields.to_csv( options )
|
346
|
+
#
|
347
|
+
def to_csv(**options)
|
348
|
+
fields.to_csv(options)
|
349
|
+
end
|
350
|
+
alias_method :to_s, :to_csv
|
351
|
+
|
352
|
+
#
|
353
|
+
# Extracts the nested value specified by the sequence of +index+ or +header+ objects by calling dig at each step,
|
354
|
+
# returning nil if any intermediate step is nil.
|
355
|
+
#
|
356
|
+
def dig(index_or_header, *indexes)
|
357
|
+
value = field(index_or_header)
|
358
|
+
if value.nil?
|
359
|
+
nil
|
360
|
+
elsif indexes.empty?
|
361
|
+
value
|
362
|
+
else
|
363
|
+
unless value.respond_to?(:dig)
|
364
|
+
raise TypeError, "#{value.class} does not have \#dig method"
|
365
|
+
end
|
366
|
+
value.dig(*indexes)
|
367
|
+
end
|
368
|
+
end
|
369
|
+
|
370
|
+
# A summary of fields, by header, in an ASCII compatible String.
|
371
|
+
def inspect
|
372
|
+
str = ["#<", self.class.to_s]
|
373
|
+
each do |header, field|
|
374
|
+
str << " " << (header.is_a?(Symbol) ? header.to_s : header.inspect) <<
|
375
|
+
":" << field.inspect
|
376
|
+
end
|
377
|
+
str << ">"
|
378
|
+
begin
|
379
|
+
str.join('')
|
380
|
+
rescue # any encoding error
|
381
|
+
str.map do |s|
|
382
|
+
e = Encoding::Converter.asciicompat_encoding(s.encoding)
|
383
|
+
e ? s.encode(e) : s.force_encoding("ASCII-8BIT")
|
384
|
+
end.join('')
|
385
|
+
end
|
386
|
+
end
|
387
|
+
end
|
388
|
+
end
|
data/lib/csv/table.rb
ADDED
@@ -0,0 +1,378 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "forwardable"
|
4
|
+
|
5
|
+
class CSV
|
6
|
+
#
|
7
|
+
# A CSV::Table is a two-dimensional data structure for representing CSV
|
8
|
+
# documents. Tables allow you to work with the data by row or column,
|
9
|
+
# manipulate the data, and even convert the results back to CSV, if needed.
|
10
|
+
#
|
11
|
+
# All tables returned by CSV will be constructed from this class, if header
|
12
|
+
# row processing is activated.
|
13
|
+
#
|
14
|
+
class Table
|
15
|
+
#
|
16
|
+
# Construct a new CSV::Table from +array_of_rows+, which are expected
|
17
|
+
# to be CSV::Row objects. All rows are assumed to have the same headers.
|
18
|
+
#
|
19
|
+
# A CSV::Table object supports the following Array methods through
|
20
|
+
# delegation:
|
21
|
+
#
|
22
|
+
# * empty?()
|
23
|
+
# * length()
|
24
|
+
# * size()
|
25
|
+
#
|
26
|
+
def initialize(array_of_rows)
|
27
|
+
@table = array_of_rows
|
28
|
+
@mode = :col_or_row
|
29
|
+
end
|
30
|
+
|
31
|
+
# The current access mode for indexing and iteration.
|
32
|
+
attr_reader :mode
|
33
|
+
|
34
|
+
# Internal data format used to compare equality.
|
35
|
+
attr_reader :table
|
36
|
+
protected :table
|
37
|
+
|
38
|
+
### Array Delegation ###
|
39
|
+
|
40
|
+
extend Forwardable
|
41
|
+
def_delegators :@table, :empty?, :length, :size
|
42
|
+
|
43
|
+
#
|
44
|
+
# Returns a duplicate table object, in column mode. This is handy for
|
45
|
+
# chaining in a single call without changing the table mode, but be aware
|
46
|
+
# that this method can consume a fair amount of memory for bigger data sets.
|
47
|
+
#
|
48
|
+
# This method returns the duplicate table for chaining. Don't chain
|
49
|
+
# destructive methods (like []=()) this way though, since you are working
|
50
|
+
# with a duplicate.
|
51
|
+
#
|
52
|
+
def by_col
|
53
|
+
self.class.new(@table.dup).by_col!
|
54
|
+
end
|
55
|
+
|
56
|
+
#
|
57
|
+
# Switches the mode of this table to column mode. All calls to indexing and
|
58
|
+
# iteration methods will work with columns until the mode is changed again.
|
59
|
+
#
|
60
|
+
# This method returns the table and is safe to chain.
|
61
|
+
#
|
62
|
+
def by_col!
|
63
|
+
@mode = :col
|
64
|
+
|
65
|
+
self
|
66
|
+
end
|
67
|
+
|
68
|
+
#
|
69
|
+
# Returns a duplicate table object, in mixed mode. This is handy for
|
70
|
+
# chaining in a single call without changing the table mode, but be aware
|
71
|
+
# that this method can consume a fair amount of memory for bigger data sets.
|
72
|
+
#
|
73
|
+
# This method returns the duplicate table for chaining. Don't chain
|
74
|
+
# destructive methods (like []=()) this way though, since you are working
|
75
|
+
# with a duplicate.
|
76
|
+
#
|
77
|
+
def by_col_or_row
|
78
|
+
self.class.new(@table.dup).by_col_or_row!
|
79
|
+
end
|
80
|
+
|
81
|
+
#
|
82
|
+
# Switches the mode of this table to mixed mode. All calls to indexing and
|
83
|
+
# iteration methods will use the default intelligent indexing system until
|
84
|
+
# the mode is changed again. In mixed mode an index is assumed to be a row
|
85
|
+
# reference while anything else is assumed to be column access by headers.
|
86
|
+
#
|
87
|
+
# This method returns the table and is safe to chain.
|
88
|
+
#
|
89
|
+
def by_col_or_row!
|
90
|
+
@mode = :col_or_row
|
91
|
+
|
92
|
+
self
|
93
|
+
end
|
94
|
+
|
95
|
+
#
|
96
|
+
# Returns a duplicate table object, in row mode. This is handy for chaining
|
97
|
+
# in a single call without changing the table mode, but be aware that this
|
98
|
+
# method can consume a fair amount of memory for bigger data sets.
|
99
|
+
#
|
100
|
+
# This method returns the duplicate table for chaining. Don't chain
|
101
|
+
# destructive methods (like []=()) this way though, since you are working
|
102
|
+
# with a duplicate.
|
103
|
+
#
|
104
|
+
def by_row
|
105
|
+
self.class.new(@table.dup).by_row!
|
106
|
+
end
|
107
|
+
|
108
|
+
#
|
109
|
+
# Switches the mode of this table to row mode. All calls to indexing and
|
110
|
+
# iteration methods will work with rows until the mode is changed again.
|
111
|
+
#
|
112
|
+
# This method returns the table and is safe to chain.
|
113
|
+
#
|
114
|
+
def by_row!
|
115
|
+
@mode = :row
|
116
|
+
|
117
|
+
self
|
118
|
+
end
|
119
|
+
|
120
|
+
#
|
121
|
+
# Returns the headers for the first row of this table (assumed to match all
|
122
|
+
# other rows). An empty Array is returned for empty tables.
|
123
|
+
#
|
124
|
+
def headers
|
125
|
+
if @table.empty?
|
126
|
+
Array.new
|
127
|
+
else
|
128
|
+
@table.first.headers
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
#
|
133
|
+
# In the default mixed mode, this method returns rows for index access and
|
134
|
+
# columns for header access. You can force the index association by first
|
135
|
+
# calling by_col!() or by_row!().
|
136
|
+
#
|
137
|
+
# Columns are returned as an Array of values. Altering that Array has no
|
138
|
+
# effect on the table.
|
139
|
+
#
|
140
|
+
def [](index_or_header)
|
141
|
+
if @mode == :row or # by index
|
142
|
+
(@mode == :col_or_row and (index_or_header.is_a?(Integer) or index_or_header.is_a?(Range)))
|
143
|
+
@table[index_or_header]
|
144
|
+
else # by header
|
145
|
+
@table.map { |row| row[index_or_header] }
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
#
|
150
|
+
# In the default mixed mode, this method assigns rows for index access and
|
151
|
+
# columns for header access. You can force the index association by first
|
152
|
+
# calling by_col!() or by_row!().
|
153
|
+
#
|
154
|
+
# Rows may be set to an Array of values (which will inherit the table's
|
155
|
+
# headers()) or a CSV::Row.
|
156
|
+
#
|
157
|
+
# Columns may be set to a single value, which is copied to each row of the
|
158
|
+
# column, or an Array of values. Arrays of values are assigned to rows top
|
159
|
+
# to bottom in row major order. Excess values are ignored and if the Array
|
160
|
+
# does not have a value for each row the extra rows will receive a +nil+.
|
161
|
+
#
|
162
|
+
# Assigning to an existing column or row clobbers the data. Assigning to
|
163
|
+
# new columns creates them at the right end of the table.
|
164
|
+
#
|
165
|
+
def []=(index_or_header, value)
|
166
|
+
if @mode == :row or # by index
|
167
|
+
(@mode == :col_or_row and index_or_header.is_a? Integer)
|
168
|
+
if value.is_a? Array
|
169
|
+
@table[index_or_header] = Row.new(headers, value)
|
170
|
+
else
|
171
|
+
@table[index_or_header] = value
|
172
|
+
end
|
173
|
+
else # set column
|
174
|
+
if value.is_a? Array # multiple values
|
175
|
+
@table.each_with_index do |row, i|
|
176
|
+
if row.header_row?
|
177
|
+
row[index_or_header] = index_or_header
|
178
|
+
else
|
179
|
+
row[index_or_header] = value[i]
|
180
|
+
end
|
181
|
+
end
|
182
|
+
else # repeated value
|
183
|
+
@table.each do |row|
|
184
|
+
if row.header_row?
|
185
|
+
row[index_or_header] = index_or_header
|
186
|
+
else
|
187
|
+
row[index_or_header] = value
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
#
|
195
|
+
# The mixed mode default is to treat a list of indices as row access,
|
196
|
+
# returning the rows indicated. Anything else is considered columnar
|
197
|
+
# access. For columnar access, the return set has an Array for each row
|
198
|
+
# with the values indicated by the headers in each Array. You can force
|
199
|
+
# column or row mode using by_col!() or by_row!().
|
200
|
+
#
|
201
|
+
# You cannot mix column and row access.
|
202
|
+
#
|
203
|
+
def values_at(*indices_or_headers)
|
204
|
+
if @mode == :row or # by indices
|
205
|
+
( @mode == :col_or_row and indices_or_headers.all? do |index|
|
206
|
+
index.is_a?(Integer) or
|
207
|
+
( index.is_a?(Range) and
|
208
|
+
index.first.is_a?(Integer) and
|
209
|
+
index.last.is_a?(Integer) )
|
210
|
+
end )
|
211
|
+
@table.values_at(*indices_or_headers)
|
212
|
+
else # by headers
|
213
|
+
@table.map { |row| row.values_at(*indices_or_headers) }
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
#
|
218
|
+
# Adds a new row to the bottom end of this table. You can provide an Array,
|
219
|
+
# which will be converted to a CSV::Row (inheriting the table's headers()),
|
220
|
+
# or a CSV::Row.
|
221
|
+
#
|
222
|
+
# This method returns the table for chaining.
|
223
|
+
#
|
224
|
+
def <<(row_or_array)
|
225
|
+
if row_or_array.is_a? Array # append Array
|
226
|
+
@table << Row.new(headers, row_or_array)
|
227
|
+
else # append Row
|
228
|
+
@table << row_or_array
|
229
|
+
end
|
230
|
+
|
231
|
+
self # for chaining
|
232
|
+
end
|
233
|
+
|
234
|
+
#
|
235
|
+
# A shortcut for appending multiple rows. Equivalent to:
|
236
|
+
#
|
237
|
+
# rows.each { |row| self << row }
|
238
|
+
#
|
239
|
+
# This method returns the table for chaining.
|
240
|
+
#
|
241
|
+
def push(*rows)
|
242
|
+
rows.each { |row| self << row }
|
243
|
+
|
244
|
+
self # for chaining
|
245
|
+
end
|
246
|
+
|
247
|
+
#
|
248
|
+
# Removes and returns the indicated columns or rows. In the default mixed
|
249
|
+
# mode indices refer to rows and everything else is assumed to be a column
|
250
|
+
# headers. Use by_col!() or by_row!() to force the lookup.
|
251
|
+
#
|
252
|
+
def delete(*indexes_or_headers)
|
253
|
+
if indexes_or_headers.empty?
|
254
|
+
raise ArgumentError, "wrong number of arguments (given 0, expected 1+)"
|
255
|
+
end
|
256
|
+
deleted_values = indexes_or_headers.map do |index_or_header|
|
257
|
+
if @mode == :row or # by index
|
258
|
+
(@mode == :col_or_row and index_or_header.is_a? Integer)
|
259
|
+
@table.delete_at(index_or_header)
|
260
|
+
else # by header
|
261
|
+
@table.map { |row| row.delete(index_or_header).last }
|
262
|
+
end
|
263
|
+
end
|
264
|
+
if indexes_or_headers.size == 1
|
265
|
+
deleted_values[0]
|
266
|
+
else
|
267
|
+
deleted_values
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
#
|
272
|
+
# Removes any column or row for which the block returns +true+. In the
|
273
|
+
# default mixed mode or row mode, iteration is the standard row major
|
274
|
+
# walking of rows. In column mode, iteration will +yield+ two element
|
275
|
+
# tuples containing the column name and an Array of values for that column.
|
276
|
+
#
|
277
|
+
# This method returns the table for chaining.
|
278
|
+
#
|
279
|
+
# If no block is given, an Enumerator is returned.
|
280
|
+
#
|
281
|
+
def delete_if(&block)
|
282
|
+
return enum_for(__method__) { @mode == :row or @mode == :col_or_row ? size : headers.size } unless block_given?
|
283
|
+
|
284
|
+
if @mode == :row or @mode == :col_or_row # by index
|
285
|
+
@table.delete_if(&block)
|
286
|
+
else # by header
|
287
|
+
deleted = []
|
288
|
+
headers.each do |header|
|
289
|
+
deleted << delete(header) if yield([header, self[header]])
|
290
|
+
end
|
291
|
+
end
|
292
|
+
|
293
|
+
self # for chaining
|
294
|
+
end
|
295
|
+
|
296
|
+
include Enumerable
|
297
|
+
|
298
|
+
#
|
299
|
+
# In the default mixed mode or row mode, iteration is the standard row major
|
300
|
+
# walking of rows. In column mode, iteration will +yield+ two element
|
301
|
+
# tuples containing the column name and an Array of values for that column.
|
302
|
+
#
|
303
|
+
# This method returns the table for chaining.
|
304
|
+
#
|
305
|
+
# If no block is given, an Enumerator is returned.
|
306
|
+
#
|
307
|
+
def each(&block)
|
308
|
+
return enum_for(__method__) { @mode == :col ? headers.size : size } unless block_given?
|
309
|
+
|
310
|
+
if @mode == :col
|
311
|
+
headers.each { |header| yield([header, self[header]]) }
|
312
|
+
else
|
313
|
+
@table.each(&block)
|
314
|
+
end
|
315
|
+
|
316
|
+
self # for chaining
|
317
|
+
end
|
318
|
+
|
319
|
+
# Returns +true+ if all rows of this table ==() +other+'s rows.
|
320
|
+
def ==(other)
|
321
|
+
return @table == other.table if other.is_a? CSV::Table
|
322
|
+
@table == other
|
323
|
+
end
|
324
|
+
|
325
|
+
#
|
326
|
+
# Returns the table as an Array of Arrays. Headers will be the first row,
|
327
|
+
# then all of the field rows will follow.
|
328
|
+
#
|
329
|
+
def to_a
|
330
|
+
array = [headers]
|
331
|
+
@table.each do |row|
|
332
|
+
array.push(row.fields) unless row.header_row?
|
333
|
+
end
|
334
|
+
|
335
|
+
array
|
336
|
+
end
|
337
|
+
|
338
|
+
#
|
339
|
+
# Returns the table as a complete CSV String. Headers will be listed first,
|
340
|
+
# then all of the field rows.
|
341
|
+
#
|
342
|
+
# This method assumes you want the Table.headers(), unless you explicitly
|
343
|
+
# pass <tt>:write_headers => false</tt>.
|
344
|
+
#
|
345
|
+
def to_csv(write_headers: true, **options)
|
346
|
+
array = write_headers ? [headers.to_csv(options)] : []
|
347
|
+
@table.each do |row|
|
348
|
+
array.push(row.fields.to_csv(options)) unless row.header_row?
|
349
|
+
end
|
350
|
+
|
351
|
+
array.join("")
|
352
|
+
end
|
353
|
+
alias_method :to_s, :to_csv
|
354
|
+
|
355
|
+
#
|
356
|
+
# Extracts the nested value specified by the sequence of +index+ or +header+ objects by calling dig at each step,
|
357
|
+
# returning nil if any intermediate step is nil.
|
358
|
+
#
|
359
|
+
def dig(index_or_header, *index_or_headers)
|
360
|
+
value = self[index_or_header]
|
361
|
+
if value.nil?
|
362
|
+
nil
|
363
|
+
elsif index_or_headers.empty?
|
364
|
+
value
|
365
|
+
else
|
366
|
+
unless value.respond_to?(:dig)
|
367
|
+
raise TypeError, "#{value.class} does not have \#dig method"
|
368
|
+
end
|
369
|
+
value.dig(*index_or_headers)
|
370
|
+
end
|
371
|
+
end
|
372
|
+
|
373
|
+
# Shows the mode and size of this table in a US-ASCII String.
|
374
|
+
def inspect
|
375
|
+
"#<#{self.class} mode:#{@mode} row_count:#{to_a.size}>".encode("US-ASCII")
|
376
|
+
end
|
377
|
+
end
|
378
|
+
end
|