Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../proc/self/root/usr/share/ruby
File: csv.rb
# encoding: US-ASCII
[0] Fix | Delete
# frozen_string_literal: true
[1] Fix | Delete
# = csv.rb -- CSV Reading and Writing
[2] Fix | Delete
#
[3] Fix | Delete
# Created by James Edward Gray II on 2005-10-31.
[4] Fix | Delete
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code
[5] Fix | Delete
# under the terms of Ruby's license.
[6] Fix | Delete
#
[7] Fix | Delete
# See CSV for documentation.
[8] Fix | Delete
#
[9] Fix | Delete
# == Description
[10] Fix | Delete
#
[11] Fix | Delete
# Welcome to the new and improved CSV.
[12] Fix | Delete
#
[13] Fix | Delete
# This version of the CSV library began its life as FasterCSV. FasterCSV was
[14] Fix | Delete
# intended as a replacement to Ruby's then standard CSV library. It was
[15] Fix | Delete
# designed to address concerns users of that library had and it had three
[16] Fix | Delete
# primary goals:
[17] Fix | Delete
#
[18] Fix | Delete
# 1. Be significantly faster than CSV while remaining a pure Ruby library.
[19] Fix | Delete
# 2. Use a smaller and easier to maintain code base. (FasterCSV eventually
[20] Fix | Delete
# grew larger, was also but considerably richer in features. The parsing
[21] Fix | Delete
# core remains quite small.)
[22] Fix | Delete
# 3. Improve on the CSV interface.
[23] Fix | Delete
#
[24] Fix | Delete
# Obviously, the last one is subjective. I did try to defer to the original
[25] Fix | Delete
# interface whenever I didn't have a compelling reason to change it though, so
[26] Fix | Delete
# hopefully this won't be too radically different.
[27] Fix | Delete
#
[28] Fix | Delete
# We must have met our goals because FasterCSV was renamed to CSV and replaced
[29] Fix | Delete
# the original library as of Ruby 1.9. If you are migrating code from 1.8 or
[30] Fix | Delete
# earlier, you may have to change your code to comply with the new interface.
[31] Fix | Delete
#
[32] Fix | Delete
# == What's Different From the Old CSV?
[33] Fix | Delete
#
[34] Fix | Delete
# I'm sure I'll miss something, but I'll try to mention most of the major
[35] Fix | Delete
# differences I am aware of, to help others quickly get up to speed:
[36] Fix | Delete
#
[37] Fix | Delete
# === CSV Parsing
[38] Fix | Delete
#
[39] Fix | Delete
# * This parser is m17n aware. See CSV for full details.
[40] Fix | Delete
# * This library has a stricter parser and will throw MalformedCSVErrors on
[41] Fix | Delete
# problematic data.
[42] Fix | Delete
# * This library has a less liberal idea of a line ending than CSV. What you
[43] Fix | Delete
# set as the <tt>:row_sep</tt> is law. It can auto-detect your line endings
[44] Fix | Delete
# though.
[45] Fix | Delete
# * The old library returned empty lines as <tt>[nil]</tt>. This library calls
[46] Fix | Delete
# them <tt>[]</tt>.
[47] Fix | Delete
# * This library has a much faster parser.
[48] Fix | Delete
#
[49] Fix | Delete
# === Interface
[50] Fix | Delete
#
[51] Fix | Delete
# * CSV now uses Hash-style parameters to set options.
[52] Fix | Delete
# * CSV no longer has generate_row() or parse_row().
[53] Fix | Delete
# * The old CSV's Reader and Writer classes have been dropped.
[54] Fix | Delete
# * CSV::open() is now more like Ruby's open().
[55] Fix | Delete
# * CSV objects now support most standard IO methods.
[56] Fix | Delete
# * CSV now has a new() method used to wrap objects like String and IO for
[57] Fix | Delete
# reading and writing.
[58] Fix | Delete
# * CSV::generate() is different from the old method.
[59] Fix | Delete
# * CSV no longer supports partial reads. It works line-by-line.
[60] Fix | Delete
# * CSV no longer allows the instance methods to override the separators for
[61] Fix | Delete
# performance reasons. They must be set in the constructor.
[62] Fix | Delete
#
[63] Fix | Delete
# If you use this library and find yourself missing any functionality I have
[64] Fix | Delete
# trimmed, please {let me know}[mailto:james@grayproductions.net].
[65] Fix | Delete
#
[66] Fix | Delete
# == Documentation
[67] Fix | Delete
#
[68] Fix | Delete
# See CSV for documentation.
[69] Fix | Delete
#
[70] Fix | Delete
# == What is CSV, really?
[71] Fix | Delete
#
[72] Fix | Delete
# CSV maintains a pretty strict definition of CSV taken directly from
[73] Fix | Delete
# {the RFC}[http://www.ietf.org/rfc/rfc4180.txt]. I relax the rules in only one
[74] Fix | Delete
# place and that is to make using this library easier. CSV will parse all valid
[75] Fix | Delete
# CSV.
[76] Fix | Delete
#
[77] Fix | Delete
# What you don't want to do is feed CSV invalid data. Because of the way the
[78] Fix | Delete
# CSV format works, it's common for a parser to need to read until the end of
[79] Fix | Delete
# the file to be sure a field is invalid. This eats a lot of time and memory.
[80] Fix | Delete
#
[81] Fix | Delete
# Luckily, when working with invalid CSV, Ruby's built-in methods will almost
[82] Fix | Delete
# always be superior in every way. For example, parsing non-quoted fields is as
[83] Fix | Delete
# easy as:
[84] Fix | Delete
#
[85] Fix | Delete
# data.split(",")
[86] Fix | Delete
#
[87] Fix | Delete
# == Questions and/or Comments
[88] Fix | Delete
#
[89] Fix | Delete
# Feel free to email {James Edward Gray II}[mailto:james@grayproductions.net]
[90] Fix | Delete
# with any questions.
[91] Fix | Delete
[92] Fix | Delete
require "forwardable"
[93] Fix | Delete
require "English"
[94] Fix | Delete
require "date"
[95] Fix | Delete
require "stringio"
[96] Fix | Delete
[97] Fix | Delete
#
[98] Fix | Delete
# This class provides a complete interface to CSV files and data. It offers
[99] Fix | Delete
# tools to enable you to read and write to and from Strings or IO objects, as
[100] Fix | Delete
# needed.
[101] Fix | Delete
#
[102] Fix | Delete
# == Reading
[103] Fix | Delete
#
[104] Fix | Delete
# === From a File
[105] Fix | Delete
#
[106] Fix | Delete
# ==== A Line at a Time
[107] Fix | Delete
#
[108] Fix | Delete
# CSV.foreach("path/to/file.csv") do |row|
[109] Fix | Delete
# # use row here...
[110] Fix | Delete
# end
[111] Fix | Delete
#
[112] Fix | Delete
# ==== All at Once
[113] Fix | Delete
#
[114] Fix | Delete
# arr_of_arrs = CSV.read("path/to/file.csv")
[115] Fix | Delete
#
[116] Fix | Delete
# === From a String
[117] Fix | Delete
#
[118] Fix | Delete
# ==== A Line at a Time
[119] Fix | Delete
#
[120] Fix | Delete
# CSV.parse("CSV,data,String") do |row|
[121] Fix | Delete
# # use row here...
[122] Fix | Delete
# end
[123] Fix | Delete
#
[124] Fix | Delete
# ==== All at Once
[125] Fix | Delete
#
[126] Fix | Delete
# arr_of_arrs = CSV.parse("CSV,data,String")
[127] Fix | Delete
#
[128] Fix | Delete
# == Writing
[129] Fix | Delete
#
[130] Fix | Delete
# === To a File
[131] Fix | Delete
#
[132] Fix | Delete
# CSV.open("path/to/file.csv", "wb") do |csv|
[133] Fix | Delete
# csv << ["row", "of", "CSV", "data"]
[134] Fix | Delete
# csv << ["another", "row"]
[135] Fix | Delete
# # ...
[136] Fix | Delete
# end
[137] Fix | Delete
#
[138] Fix | Delete
# === To a String
[139] Fix | Delete
#
[140] Fix | Delete
# csv_string = CSV.generate do |csv|
[141] Fix | Delete
# csv << ["row", "of", "CSV", "data"]
[142] Fix | Delete
# csv << ["another", "row"]
[143] Fix | Delete
# # ...
[144] Fix | Delete
# end
[145] Fix | Delete
#
[146] Fix | Delete
# == Convert a Single Line
[147] Fix | Delete
#
[148] Fix | Delete
# csv_string = ["CSV", "data"].to_csv # to CSV
[149] Fix | Delete
# csv_array = "CSV,String".parse_csv # from CSV
[150] Fix | Delete
#
[151] Fix | Delete
# == Shortcut Interface
[152] Fix | Delete
#
[153] Fix | Delete
# CSV { |csv_out| csv_out << %w{my data here} } # to $stdout
[154] Fix | Delete
# CSV(csv = "") { |csv_str| csv_str << %w{my data here} } # to a String
[155] Fix | Delete
# CSV($stderr) { |csv_err| csv_err << %w{my data here} } # to $stderr
[156] Fix | Delete
# CSV($stdin) { |csv_in| csv_in.each { |row| p row } } # from $stdin
[157] Fix | Delete
#
[158] Fix | Delete
# == Advanced Usage
[159] Fix | Delete
#
[160] Fix | Delete
# === Wrap an IO Object
[161] Fix | Delete
#
[162] Fix | Delete
# csv = CSV.new(io, options)
[163] Fix | Delete
# # ... read (with gets() or each()) from and write (with <<) to csv here ...
[164] Fix | Delete
#
[165] Fix | Delete
# == CSV and Character Encodings (M17n or Multilingualization)
[166] Fix | Delete
#
[167] Fix | Delete
# This new CSV parser is m17n savvy. The parser works in the Encoding of the IO
[168] Fix | Delete
# or String object being read from or written to. Your data is never transcoded
[169] Fix | Delete
# (unless you ask Ruby to transcode it for you) and will literally be parsed in
[170] Fix | Delete
# the Encoding it is in. Thus CSV will return Arrays or Rows of Strings in the
[171] Fix | Delete
# Encoding of your data. This is accomplished by transcoding the parser itself
[172] Fix | Delete
# into your Encoding.
[173] Fix | Delete
#
[174] Fix | Delete
# Some transcoding must take place, of course, to accomplish this multiencoding
[175] Fix | Delete
# support. For example, <tt>:col_sep</tt>, <tt>:row_sep</tt>, and
[176] Fix | Delete
# <tt>:quote_char</tt> must be transcoded to match your data. Hopefully this
[177] Fix | Delete
# makes the entire process feel transparent, since CSV's defaults should just
[178] Fix | Delete
# magically work for your data. However, you can set these values manually in
[179] Fix | Delete
# the target Encoding to avoid the translation.
[180] Fix | Delete
#
[181] Fix | Delete
# It's also important to note that while all of CSV's core parser is now
[182] Fix | Delete
# Encoding agnostic, some features are not. For example, the built-in
[183] Fix | Delete
# converters will try to transcode data to UTF-8 before making conversions.
[184] Fix | Delete
# Again, you can provide custom converters that are aware of your Encodings to
[185] Fix | Delete
# avoid this translation. It's just too hard for me to support native
[186] Fix | Delete
# conversions in all of Ruby's Encodings.
[187] Fix | Delete
#
[188] Fix | Delete
# Anyway, the practical side of this is simple: make sure IO and String objects
[189] Fix | Delete
# passed into CSV have the proper Encoding set and everything should just work.
[190] Fix | Delete
# CSV methods that allow you to open IO objects (CSV::foreach(), CSV::open(),
[191] Fix | Delete
# CSV::read(), and CSV::readlines()) do allow you to specify the Encoding.
[192] Fix | Delete
#
[193] Fix | Delete
# One minor exception comes when generating CSV into a String with an Encoding
[194] Fix | Delete
# that is not ASCII compatible. There's no existing data for CSV to use to
[195] Fix | Delete
# prepare itself and thus you will probably need to manually specify the desired
[196] Fix | Delete
# Encoding for most of those cases. It will try to guess using the fields in a
[197] Fix | Delete
# row of output though, when using CSV::generate_line() or Array#to_csv().
[198] Fix | Delete
#
[199] Fix | Delete
# I try to point out any other Encoding issues in the documentation of methods
[200] Fix | Delete
# as they come up.
[201] Fix | Delete
#
[202] Fix | Delete
# This has been tested to the best of my ability with all non-"dummy" Encodings
[203] Fix | Delete
# Ruby ships with. However, it is brave new code and may have some bugs.
[204] Fix | Delete
# Please feel free to {report}[mailto:james@grayproductions.net] any issues you
[205] Fix | Delete
# find with it.
[206] Fix | Delete
#
[207] Fix | Delete
class CSV
[208] Fix | Delete
# The version of the installed library.
[209] Fix | Delete
VERSION = "2.4.8"
[210] Fix | Delete
[211] Fix | Delete
#
[212] Fix | Delete
# A CSV::Row is part Array and part Hash. It retains an order for the fields
[213] Fix | Delete
# and allows duplicates just as an Array would, but also allows you to access
[214] Fix | Delete
# fields by name just as you could if they were in a Hash.
[215] Fix | Delete
#
[216] Fix | Delete
# All rows returned by CSV will be constructed from this class, if header row
[217] Fix | Delete
# processing is activated.
[218] Fix | Delete
#
[219] Fix | Delete
class Row
[220] Fix | Delete
#
[221] Fix | Delete
# Construct a new CSV::Row from +headers+ and +fields+, which are expected
[222] Fix | Delete
# to be Arrays. If one Array is shorter than the other, it will be padded
[223] Fix | Delete
# with +nil+ objects.
[224] Fix | Delete
#
[225] Fix | Delete
# The optional +header_row+ parameter can be set to +true+ to indicate, via
[226] Fix | Delete
# CSV::Row.header_row?() and CSV::Row.field_row?(), that this is a header
[227] Fix | Delete
# row. Otherwise, the row is assumes to be a field row.
[228] Fix | Delete
#
[229] Fix | Delete
# A CSV::Row object supports the following Array methods through delegation:
[230] Fix | Delete
#
[231] Fix | Delete
# * empty?()
[232] Fix | Delete
# * length()
[233] Fix | Delete
# * size()
[234] Fix | Delete
#
[235] Fix | Delete
def initialize(headers, fields, header_row = false)
[236] Fix | Delete
@header_row = header_row
[237] Fix | Delete
headers.each { |h| h.freeze if h.is_a? String }
[238] Fix | Delete
[239] Fix | Delete
# handle extra headers or fields
[240] Fix | Delete
@row = if headers.size >= fields.size
[241] Fix | Delete
headers.zip(fields)
[242] Fix | Delete
else
[243] Fix | Delete
fields.zip(headers).each(&:reverse!)
[244] Fix | Delete
end
[245] Fix | Delete
end
[246] Fix | Delete
[247] Fix | Delete
# Internal data format used to compare equality.
[248] Fix | Delete
attr_reader :row
[249] Fix | Delete
protected :row
[250] Fix | Delete
[251] Fix | Delete
### Array Delegation ###
[252] Fix | Delete
[253] Fix | Delete
extend Forwardable
[254] Fix | Delete
def_delegators :@row, :empty?, :length, :size
[255] Fix | Delete
[256] Fix | Delete
# Returns +true+ if this is a header row.
[257] Fix | Delete
def header_row?
[258] Fix | Delete
@header_row
[259] Fix | Delete
end
[260] Fix | Delete
[261] Fix | Delete
# Returns +true+ if this is a field row.
[262] Fix | Delete
def field_row?
[263] Fix | Delete
not header_row?
[264] Fix | Delete
end
[265] Fix | Delete
[266] Fix | Delete
# Returns the headers of this row.
[267] Fix | Delete
def headers
[268] Fix | Delete
@row.map(&:first)
[269] Fix | Delete
end
[270] Fix | Delete
[271] Fix | Delete
#
[272] Fix | Delete
# :call-seq:
[273] Fix | Delete
# field( header )
[274] Fix | Delete
# field( header, offset )
[275] Fix | Delete
# field( index )
[276] Fix | Delete
#
[277] Fix | Delete
# This method will return the field value by +header+ or +index+. If a field
[278] Fix | Delete
# is not found, +nil+ is returned.
[279] Fix | Delete
#
[280] Fix | Delete
# When provided, +offset+ ensures that a header match occurs on or later
[281] Fix | Delete
# than the +offset+ index. You can use this to find duplicate headers,
[282] Fix | Delete
# without resorting to hard-coding exact indices.
[283] Fix | Delete
#
[284] Fix | Delete
def field(header_or_index, minimum_index = 0)
[285] Fix | Delete
# locate the pair
[286] Fix | Delete
finder = (header_or_index.is_a?(Integer) || header_or_index.is_a?(Range)) ? :[] : :assoc
[287] Fix | Delete
pair = @row[minimum_index..-1].send(finder, header_or_index)
[288] Fix | Delete
[289] Fix | Delete
# return the field if we have a pair
[290] Fix | Delete
if pair.nil?
[291] Fix | Delete
nil
[292] Fix | Delete
else
[293] Fix | Delete
header_or_index.is_a?(Range) ? pair.map(&:last) : pair.last
[294] Fix | Delete
end
[295] Fix | Delete
end
[296] Fix | Delete
alias_method :[], :field
[297] Fix | Delete
[298] Fix | Delete
#
[299] Fix | Delete
# :call-seq:
[300] Fix | Delete
# fetch( header )
[301] Fix | Delete
# fetch( header ) { |row| ... }
[302] Fix | Delete
# fetch( header, default )
[303] Fix | Delete
#
[304] Fix | Delete
# This method will fetch the field value by +header+. It has the same
[305] Fix | Delete
# behavior as Hash#fetch: if there is a field with the given +header+, its
[306] Fix | Delete
# value is returned. Otherwise, if a block is given, it is yielded the
[307] Fix | Delete
# +header+ and its result is returned; if a +default+ is given as the
[308] Fix | Delete
# second argument, it is returned; otherwise a KeyError is raised.
[309] Fix | Delete
#
[310] Fix | Delete
def fetch(header, *varargs)
[311] Fix | Delete
raise ArgumentError, "Too many arguments" if varargs.length > 1
[312] Fix | Delete
pair = @row.assoc(header)
[313] Fix | Delete
if pair
[314] Fix | Delete
pair.last
[315] Fix | Delete
else
[316] Fix | Delete
if block_given?
[317] Fix | Delete
yield header
[318] Fix | Delete
elsif varargs.empty?
[319] Fix | Delete
raise KeyError, "key not found: #{header}"
[320] Fix | Delete
else
[321] Fix | Delete
varargs.first
[322] Fix | Delete
end
[323] Fix | Delete
end
[324] Fix | Delete
end
[325] Fix | Delete
[326] Fix | Delete
# Returns +true+ if there is a field with the given +header+.
[327] Fix | Delete
def has_key?(header)
[328] Fix | Delete
!!@row.assoc(header)
[329] Fix | Delete
end
[330] Fix | Delete
alias_method :include?, :has_key?
[331] Fix | Delete
alias_method :key?, :has_key?
[332] Fix | Delete
alias_method :member?, :has_key?
[333] Fix | Delete
[334] Fix | Delete
#
[335] Fix | Delete
# :call-seq:
[336] Fix | Delete
# []=( header, value )
[337] Fix | Delete
# []=( header, offset, value )
[338] Fix | Delete
# []=( index, value )
[339] Fix | Delete
#
[340] Fix | Delete
# Looks up the field by the semantics described in CSV::Row.field() and
[341] Fix | Delete
# assigns the +value+.
[342] Fix | Delete
#
[343] Fix | Delete
# Assigning past the end of the row with an index will set all pairs between
[344] Fix | Delete
# to <tt>[nil, nil]</tt>. Assigning to an unused header appends the new
[345] Fix | Delete
# pair.
[346] Fix | Delete
#
[347] Fix | Delete
def []=(*args)
[348] Fix | Delete
value = args.pop
[349] Fix | Delete
[350] Fix | Delete
if args.first.is_a? Integer
[351] Fix | Delete
if @row[args.first].nil? # extending past the end with index
[352] Fix | Delete
@row[args.first] = [nil, value]
[353] Fix | Delete
@row.map! { |pair| pair.nil? ? [nil, nil] : pair }
[354] Fix | Delete
else # normal index assignment
[355] Fix | Delete
@row[args.first][1] = value
[356] Fix | Delete
end
[357] Fix | Delete
else
[358] Fix | Delete
index = index(*args)
[359] Fix | Delete
if index.nil? # appending a field
[360] Fix | Delete
self << [args.first, value]
[361] Fix | Delete
else # normal header assignment
[362] Fix | Delete
@row[index][1] = value
[363] Fix | Delete
end
[364] Fix | Delete
end
[365] Fix | Delete
end
[366] Fix | Delete
[367] Fix | Delete
#
[368] Fix | Delete
# :call-seq:
[369] Fix | Delete
# <<( field )
[370] Fix | Delete
# <<( header_and_field_array )
[371] Fix | Delete
# <<( header_and_field_hash )
[372] Fix | Delete
#
[373] Fix | Delete
# If a two-element Array is provided, it is assumed to be a header and field
[374] Fix | Delete
# and the pair is appended. A Hash works the same way with the key being
[375] Fix | Delete
# the header and the value being the field. Anything else is assumed to be
[376] Fix | Delete
# a lone field which is appended with a +nil+ header.
[377] Fix | Delete
#
[378] Fix | Delete
# This method returns the row for chaining.
[379] Fix | Delete
#
[380] Fix | Delete
def <<(arg)
[381] Fix | Delete
if arg.is_a?(Array) and arg.size == 2 # appending a header and name
[382] Fix | Delete
@row << arg
[383] Fix | Delete
elsif arg.is_a?(Hash) # append header and name pairs
[384] Fix | Delete
arg.each { |pair| @row << pair }
[385] Fix | Delete
else # append field value
[386] Fix | Delete
@row << [nil, arg]
[387] Fix | Delete
end
[388] Fix | Delete
[389] Fix | Delete
self # for chaining
[390] Fix | Delete
end
[391] Fix | Delete
[392] Fix | Delete
#
[393] Fix | Delete
# A shortcut for appending multiple fields. Equivalent to:
[394] Fix | Delete
#
[395] Fix | Delete
# args.each { |arg| csv_row << arg }
[396] Fix | Delete
#
[397] Fix | Delete
# This method returns the row for chaining.
[398] Fix | Delete
#
[399] Fix | Delete
def push(*args)
[400] Fix | Delete
args.each { |arg| self << arg }
[401] Fix | Delete
[402] Fix | Delete
self # for chaining
[403] Fix | Delete
end
[404] Fix | Delete
[405] Fix | Delete
#
[406] Fix | Delete
# :call-seq:
[407] Fix | Delete
# delete( header )
[408] Fix | Delete
# delete( header, offset )
[409] Fix | Delete
# delete( index )
[410] Fix | Delete
#
[411] Fix | Delete
# Used to remove a pair from the row by +header+ or +index+. The pair is
[412] Fix | Delete
# located as described in CSV::Row.field(). The deleted pair is returned,
[413] Fix | Delete
# or +nil+ if a pair could not be found.
[414] Fix | Delete
#
[415] Fix | Delete
def delete(header_or_index, minimum_index = 0)
[416] Fix | Delete
if header_or_index.is_a? Integer # by index
[417] Fix | Delete
@row.delete_at(header_or_index)
[418] Fix | Delete
elsif i = index(header_or_index, minimum_index) # by header
[419] Fix | Delete
@row.delete_at(i)
[420] Fix | Delete
else
[421] Fix | Delete
[ ]
[422] Fix | Delete
end
[423] Fix | Delete
end
[424] Fix | Delete
[425] Fix | Delete
#
[426] Fix | Delete
# The provided +block+ is passed a header and field for each pair in the row
[427] Fix | Delete
# and expected to return +true+ or +false+, depending on whether the pair
[428] Fix | Delete
# should be deleted.
[429] Fix | Delete
#
[430] Fix | Delete
# This method returns the row for chaining.
[431] Fix | Delete
#
[432] Fix | Delete
# If no block is given, an Enumerator is returned.
[433] Fix | Delete
#
[434] Fix | Delete
def delete_if(&block)
[435] Fix | Delete
block or return enum_for(__method__) { size }
[436] Fix | Delete
[437] Fix | Delete
@row.delete_if(&block)
[438] Fix | Delete
[439] Fix | Delete
self # for chaining
[440] Fix | Delete
end
[441] Fix | Delete
[442] Fix | Delete
#
[443] Fix | Delete
# This method accepts any number of arguments which can be headers, indices,
[444] Fix | Delete
# Ranges of either, or two-element Arrays containing a header and offset.
[445] Fix | Delete
# Each argument will be replaced with a field lookup as described in
[446] Fix | Delete
# CSV::Row.field().
[447] Fix | Delete
#
[448] Fix | Delete
# If called with no arguments, all fields are returned.
[449] Fix | Delete
#
[450] Fix | Delete
def fields(*headers_and_or_indices)
[451] Fix | Delete
if headers_and_or_indices.empty? # return all fields--no arguments
[452] Fix | Delete
@row.map(&:last)
[453] Fix | Delete
else # or work like values_at()
[454] Fix | Delete
all = []
[455] Fix | Delete
headers_and_or_indices.each do |h_or_i|
[456] Fix | Delete
if h_or_i.is_a? Range
[457] Fix | Delete
index_begin = h_or_i.begin.is_a?(Integer) ? h_or_i.begin :
[458] Fix | Delete
index(h_or_i.begin)
[459] Fix | Delete
index_end = h_or_i.end.is_a?(Integer) ? h_or_i.end :
[460] Fix | Delete
index(h_or_i.end)
[461] Fix | Delete
new_range = h_or_i.exclude_end? ? (index_begin...index_end) :
[462] Fix | Delete
(index_begin..index_end)
[463] Fix | Delete
all.concat(fields.values_at(new_range))
[464] Fix | Delete
else
[465] Fix | Delete
all << field(*Array(h_or_i))
[466] Fix | Delete
end
[467] Fix | Delete
end
[468] Fix | Delete
return all
[469] Fix | Delete
end
[470] Fix | Delete
end
[471] Fix | Delete
alias_method :values_at, :fields
[472] Fix | Delete
[473] Fix | Delete
#
[474] Fix | Delete
# :call-seq:
[475] Fix | Delete
# index( header )
[476] Fix | Delete
# index( header, offset )
[477] Fix | Delete
#
[478] Fix | Delete
# This method will return the index of a field with the provided +header+.
[479] Fix | Delete
# The +offset+ can be used to locate duplicate header names, as described in
[480] Fix | Delete
# CSV::Row.field().
[481] Fix | Delete
#
[482] Fix | Delete
def index(header, minimum_index = 0)
[483] Fix | Delete
# find the pair
[484] Fix | Delete
index = headers[minimum_index..-1].index(header)
[485] Fix | Delete
# return the index at the right offset, if we found one
[486] Fix | Delete
index.nil? ? nil : index + minimum_index
[487] Fix | Delete
end
[488] Fix | Delete
[489] Fix | Delete
# Returns +true+ if +name+ is a header for this row, and +false+ otherwise.
[490] Fix | Delete
def header?(name)
[491] Fix | Delete
headers.include? name
[492] Fix | Delete
end
[493] Fix | Delete
alias_method :include?, :header?
[494] Fix | Delete
[495] Fix | Delete
#
[496] Fix | Delete
# Returns +true+ if +data+ matches a field in this row, and +false+
[497] Fix | Delete
# otherwise.
[498] Fix | Delete
#
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function