Object
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.
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()
# File lib/faster_csv.rb, line 130 130: def initialize(headers, fields, header_row = false) 131: @header_row = header_row 132: 133: # handle extra headers or fields 134: @row = if headers.size > fields.size 135: headers.zip(fields) 136: else 137: fields.zip(headers).map { |pair| pair.reverse } 138: end 139: end
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.
# File lib/faster_csv.rb, line 234 234: def <<(arg) 235: if arg.is_a?(Array) and arg.size == 2 # appending a header and name 236: @row << arg 237: elsif arg.is_a?(Hash) # append header and name pairs 238: arg.each { |pair| @row << pair } 239: else # append field value 240: @row << [nil, arg] 241: end 242: 243: self # for chaining 244: end
Returns true if this row contains the same headers and fields in the same order as other.
# File lib/faster_csv.rb, line 371 371: def ==(other) 372: @row == other.row 373: end
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.
# File lib/faster_csv.rb, line 201 201: def []=(*args) 202: value = args.pop 203: 204: if args.first.is_a? Integer 205: if @row[args.first].nil? # extending past the end with index 206: @row[args.first] = [nil, value] 207: @row.map! { |pair| pair.nil? ? [nil, nil] : pair } 208: else # normal index assignment 209: @row[args.first][1] = value 210: end 211: else 212: index = index(*args) 213: if index.nil? # appending a field 214: self << [args.first, value] 215: else # normal header assignment 216: @row[index][1] = value 217: end 218: end 219: end
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.
# File lib/faster_csv.rb, line 269 269: def delete(header_or_index, minimum_index = 0) 270: if header_or_index.is_a? Integer # by index 271: @row.delete_at(header_or_index) 272: elsif i = index(header_or_index, minimum_index) # by header 273: @row.delete_at(i) 274: else 275: [ ] 276: end 277: end
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.
# File lib/faster_csv.rb, line 286 286: def delete_if(&block) 287: @row.delete_if(&block) 288: 289: self # for chaining 290: end
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.
# File lib/faster_csv.rb, line 361 361: def each(&block) 362: @row.each(&block) 363: 364: self # for chaining 365: end
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.
# File lib/faster_csv.rb, line 178 178: def field(header_or_index, minimum_index = 0) 179: # locate the pair 180: finder = header_or_index.is_a?(Integer) ? :[] : :assoc 181: pair = @row[minimum_index..1].send(finder, header_or_index) 182: 183: # return the field if we have a pair 184: pair.nil? ? nil : pair.last 185: end
Returns true if data matches a field in this row, and false otherwise.
# File lib/faster_csv.rb, line 347 347: def field?(data) 348: fields.include? data 349: end
Returns true if this is a field row.
# File lib/faster_csv.rb, line 156 156: def field_row? 157: not header_row? 158: end
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.
# File lib/faster_csv.rb, line 300 300: def fields(*headers_and_or_indices) 301: if headers_and_or_indices.empty? # return all fields--no arguments 302: @row.map { |pair| pair.last } 303: else # or work like values_at() 304: headers_and_or_indices.inject(Array.new) do |all, h_or_i| 305: all + if h_or_i.is_a? Range 306: index_begin = h_or_i.begin.is_a?(Integer) ? h_or_i.begin : 307: index(h_or_i.begin) 308: index_end = h_or_i.end.is_a?(Integer) ? h_or_i.end : 309: index(h_or_i.end) 310: new_range = h_or_i.exclude_end? ? (index_begin...index_end) : 311: (index_begin..index_end) 312: fields.values_at(new_range) 313: else 314: [field(*Array(h_or_i))] 315: end 316: end 317: end 318: end
Returns true if name is a header for this row, and false otherwise.
# File lib/faster_csv.rb, line 338 338: def header?(name) 339: headers.include? name 340: end
Returns true if this is a header row.
# File lib/faster_csv.rb, line 151 151: def header_row? 152: @header_row 153: end
Returns the headers of this row.
# File lib/faster_csv.rb, line 161 161: def headers 162: @row.map { |pair| pair.first } 163: end
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().
# File lib/faster_csv.rb, line 330 330: def index(header, minimum_index = 0) 331: # find the pair 332: index = headers[minimum_index..1].index(header) 333: # return the index at the right offset, if we found one 334: index.nil? ? nil : index + minimum_index 335: end
A summary of fields, by header.
# File lib/faster_csv.rb, line 395 395: def inspect 396: str = "#<#{self.class}" 397: each do |header, field| 398: str << " #{header.is_a?(Symbol) ? header.to_s : header.inspect}:" << 399: field.inspect 400: end 401: str << ">" 402: end
A shortcut for appending multiple fields. Equivalent to:
args.each { |arg| faster_csv_row << arg }
This method returns the row for chaining.
# File lib/faster_csv.rb, line 253 253: def push(*args) 254: args.each { |arg| self << arg } 255: 256: self # for chaining 257: end
Returns the row as a CSV String. Headers are not used. Equivalent to:
faster_csv_row.fields.to_csv( options )
# File lib/faster_csv.rb, line 389 389: def to_csv(options = Hash.new) 390: fields.to_csv(options) 391: end
Collapses the row into a simple Hash. Be warning that this discards field order and clobbers duplicate fields.
# File lib/faster_csv.rb, line 379 379: def to_hash 380: # flatten just one level of the internal Array 381: Hash[*@row.inject(Array.new) { |ary, pair| ary.push(*pair) }] 382: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.