Class: FasterCSV::Row
- Inherits:
-
Object
- Object
- FasterCSV::Row
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/faster_csv.rb
Overview
A FasterCSV::Row is part Array and part Hash. It retains an order for the fields and allows duplicates just as an Array would, but also allows you to access fields by name just as you could if they were in a Hash.
All rows returned by FasterCSV will be constructed from this class, if header row processing is activated.
Instance Method Summary collapse
-
#<<(arg) ⇒ Object
:call-seq: <<( field ) <<( header_and_field_array ) <<( header_and_field_hash ).
-
#==(other) ⇒ Object
Returns
true
if this row contains the same headers and fields in the same order asother
. -
#[]=(*args) ⇒ Object
:call-seq: []=( header, value ) []=( header, offset, value ) []=( index, value ).
-
#delete(header_or_index, minimum_index = 0) ⇒ Object
:call-seq: delete( header ) delete( header, offset ) delete( index ).
-
#delete_if(&block) ⇒ Object
The provided
block
is passed a header and field for each pair in the row and expected to returntrue
orfalse
, depending on whether the pair should be deleted. -
#each(&block) ⇒ Object
Yields each pair of the row as header and field tuples (much like iterating over a Hash).
-
#field(header_or_index, minimum_index = 0) ⇒ Object
(also: #[])
:call-seq: field( header ) field( header, offset ) field( index ).
-
#field?(data) ⇒ Boolean
Returns
true
ifdata
matches a field in this row, andfalse
otherwise. -
#field_row? ⇒ Boolean
Returns
true
if this is a field row. -
#fields(*headers_and_or_indices) ⇒ Object
(also: #values_at)
This method accepts any number of arguments which can be headers, indices, Ranges of either, or two-element Arrays containing a header and offset.
-
#header?(name) ⇒ Boolean
(also: #include?)
Returns
true
ifname
is a header for this row, andfalse
otherwise. -
#header_row? ⇒ Boolean
Returns
true
if this is a header row. -
#headers ⇒ Object
Returns the headers of this row.
-
#index(header, minimum_index = 0) ⇒ Object
:call-seq: index( header ) index( header, offset ).
-
#initialize(headers, fields, header_row = false) ⇒ Row
constructor
Construct a new FasterCSV::Row from
headers
andfields
, which are expected to be Arrays. -
#inspect ⇒ Object
A summary of fields, by header.
-
#push(*args) ⇒ Object
A shortcut for appending multiple fields.
-
#to_csv(options = Hash.new) ⇒ Object
(also: #to_s)
Returns the row as a CSV String.
-
#to_hash ⇒ Object
Collapses the row into a simple Hash.
Constructor Details
#initialize(headers, fields, header_row = false) ⇒ Row
Construct a new FasterCSV::Row from headers
and fields
, which are expected to be Arrays. If one Array is shorter than the other, it will be padded with nil
objects.
The optional header_row
parameter can be set to true
to indicate, via FasterCSV::Row.header_row?() and FasterCSV::Row.field_row?(), that this is a header row. Otherwise, the row is assumes to be a field row.
A FasterCSV::Row object supports the following Array methods through delegation:
-
empty?()
-
length()
-
size()
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/faster_csv.rb', line 112 def initialize(headers, fields, header_row = false) @header_row = header_row # handle extra headers or fields @row = if headers.size > fields.size headers.zip(fields) else fields.zip(headers).map { |pair| pair.reverse } end end |
Instance Method Details
#<<(arg) ⇒ Object
:call-seq:
<<( field )
<<( header_and_field_array )
<<( header_and_field_hash )
If a two-element Array is provided, it is assumed to be a header and field and the pair is appended. A Hash works the same way with the key being the header and the value being the field. Anything else is assumed to be a lone field which is appended with a nil
header.
This method returns the row for chaining.
216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/faster_csv.rb', line 216 def <<(arg) if arg.is_a?(Array) and arg.size == 2 # appending a header and name @row << arg elsif arg.is_a?(Hash) # append header and name pairs arg.each { |pair| @row << pair } else # append field value @row << [nil, arg] end self # for chaining end |
#==(other) ⇒ Object
Returns true
if this row contains the same headers and fields in the same order as other
.
353 354 355 |
# File 'lib/faster_csv.rb', line 353 def ==(other) @row == other.row end |
#[]=(*args) ⇒ Object
:call-seq:
[]=( header, value )
[]=( header, offset, value )
[]=( index, value )
Looks up the field by the semantics described in FasterCSV::Row.field() and assigns the value
.
Assigning past the end of the row with an index will set all pairs between to [nil, nil]
. Assigning to an unused header appends the new pair.
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/faster_csv.rb', line 183 def []=(*args) value = args.pop if args.first.is_a? Integer if @row[args.first].nil? # extending past the end with index @row[args.first] = [nil, value] @row.map! { |pair| pair.nil? ? [nil, nil] : pair } else # normal index assignment @row[args.first][1] = value end else index = index(*args) if index.nil? # appending a field self << [args.first, value] else # normal header assignment @row[index][1] = value end end end |
#delete(header_or_index, minimum_index = 0) ⇒ Object
:call-seq:
delete( header )
delete( header, offset )
delete( index )
Used to remove a pair from the row by header
or index
. The pair is located as described in FasterCSV::Row.field(). The deleted pair is returned, or nil
if a pair could not be found.
251 252 253 254 255 256 257 258 259 |
# File 'lib/faster_csv.rb', line 251 def delete(header_or_index, minimum_index = 0) if header_or_index.is_a? Integer # by index @row.delete_at(header_or_index) elsif i = index(header_or_index, minimum_index) # by header @row.delete_at(i) else [ ] end end |
#delete_if(&block) ⇒ Object
The provided block
is passed a header and field for each pair in the row and expected to return true
or false
, depending on whether the pair should be deleted.
This method returns the row for chaining.
268 269 270 271 272 |
# File 'lib/faster_csv.rb', line 268 def delete_if(&block) @row.delete_if(&block) self # for chaining end |
#each(&block) ⇒ Object
Yields each pair of the row as header and field tuples (much like iterating over a Hash).
Support for Enumerable.
This method returns the row for chaining.
343 344 345 346 347 |
# File 'lib/faster_csv.rb', line 343 def each(&block) @row.each(&block) self # for chaining end |
#field(header_or_index, minimum_index = 0) ⇒ Object Also known as: []
:call-seq:
field( header )
field( header, offset )
field( index )
This method will fetch the field value by header
or index
. If a field is not found, nil
is returned.
When provided, offset
ensures that a header match occurrs on or later than the offset
index. You can use this to find duplicate headers, without resorting to hard-coding exact indices.
160 161 162 163 164 165 166 167 |
# File 'lib/faster_csv.rb', line 160 def field(header_or_index, minimum_index = 0) # locate the pair finder = header_or_index.is_a?(Integer) ? :[] : :assoc pair = @row[minimum_index..-1].send(finder, header_or_index) # return the field if we have a pair pair.nil? ? nil : pair.last end |
#field?(data) ⇒ Boolean
Returns true
if data
matches a field in this row, and false
otherwise.
329 330 331 |
# File 'lib/faster_csv.rb', line 329 def field?(data) fields.include? data end |
#field_row? ⇒ Boolean
Returns true
if this is a field row.
138 139 140 |
# File 'lib/faster_csv.rb', line 138 def field_row? not header_row? end |
#fields(*headers_and_or_indices) ⇒ Object Also known as: values_at
This method accepts any number of arguments which can be headers, indices, Ranges of either, or two-element Arrays containing a header and offset.
Each argument will be replaced with a field lookup as described in FasterCSV::Row.field().
If called with no arguments, all fields are returned.
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/faster_csv.rb', line 282 def fields(*headers_and_or_indices) if headers_and_or_indices.empty? # return all fields--no arguments @row.map { |pair| pair.last } else # or work like values_at() headers_and_or_indices.inject(Array.new) do |all, h_or_i| all + if h_or_i.is_a? Range index_begin = h_or_i.begin.is_a?(Integer) ? h_or_i.begin : index(h_or_i.begin) index_end = h_or_i.end.is_a?(Integer) ? h_or_i.end : index(h_or_i.end) new_range = h_or_i.exclude_end? ? (index_begin...index_end) : (index_begin..index_end) fields.values_at(new_range) else [field(*Array(h_or_i))] end end end end |
#header?(name) ⇒ Boolean Also known as: include?
Returns true
if name
is a header for this row, and false
otherwise.
320 321 322 |
# File 'lib/faster_csv.rb', line 320 def header?(name) headers.include? name end |
#header_row? ⇒ Boolean
Returns true
if this is a header row.
133 134 135 |
# File 'lib/faster_csv.rb', line 133 def header_row? @header_row end |
#headers ⇒ Object
Returns the headers of this row.
143 144 145 |
# File 'lib/faster_csv.rb', line 143 def headers @row.map { |pair| pair.first } end |
#index(header, minimum_index = 0) ⇒ Object
:call-seq:
index( header )
index( header, offset )
This method will return the index of a field with the provided header
. The offset
can be used to locate duplicate header names, as described in FasterCSV::Row.field().
312 313 314 315 316 317 |
# File 'lib/faster_csv.rb', line 312 def index(header, minimum_index = 0) # find the pair index = headers[minimum_index..-1].index(header) # return the index at the right offset, if we found one index.nil? ? nil : index + minimum_index end |
#inspect ⇒ Object
A summary of fields, by header.
377 378 379 380 381 382 383 384 |
# File 'lib/faster_csv.rb', line 377 def inspect str = "#<#{self.class}" each do |header, field| str << " #{header.is_a?(Symbol) ? header.to_s : header.inspect}:" << field.inspect end str << ">" end |
#push(*args) ⇒ Object
A shortcut for appending multiple fields. Equivalent to:
args.each { |arg| faster_csv_row << arg }
This method returns the row for chaining.
235 236 237 238 239 |
# File 'lib/faster_csv.rb', line 235 def push(*args) args.each { |arg| self << arg } self # for chaining end |
#to_csv(options = Hash.new) ⇒ Object Also known as: to_s
Returns the row as a CSV String. Headers are not used. Equivalent to:
faster_csv_row.fields.to_csv( )
371 372 373 |
# File 'lib/faster_csv.rb', line 371 def to_csv( = Hash.new) fields.to_csv() end |
#to_hash ⇒ Object
Collapses the row into a simple Hash. Be warning that this discards field order and clobbers duplicate fields.
361 362 363 364 |
# File 'lib/faster_csv.rb', line 361 def to_hash # flatten just one level of the internal Array Hash[*@row.inject(Array.new) { |ary, pair| ary.push(*pair) }] end |