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