lib/csv-diff/csv_source.rb
# Given an array of lines, where each line is an array of fields, indexes
# the array contents so that it can be looked up by key.
Given an array of lines, where each line is an array of fields, indexes
the array contents so that it can be looked up by key.
Given an array of lines where each line is an array of fields indexes the array contents so that it can be looked up by key .
def index_source(lines, options)
            @lines = {}
            @index = Hash.new{ |h, k| h[k] = [] }
            if @field_names
                index_fields
            end
            @case_sensitive = options.fetch(:case_sensitive, true)
            @trim_whitespace = options.fetch(:trim_whitespace, false)
            line_num = 0
            lines.each do |row|
                line_num += 1
                next if line_num == 1 && @field_names && options[:ignore_header]
                unless @field_names
                    @field_names = row
                    index_fields
                    next
                end
                field_vals = row
                line = {}
                @field_names.each_with_index do |field, i|
                    line[field] = field_vals[i]
                    line[field].strip! if @trim_whitespace && line[field]
                end
                key_values = @key_field_indexes.map{ |kf| field_vals[kf].to_s.upcase }
                key = key_values.join('~')
                parent_key = key_values[0...(@parent_fields.length)].join('~')
                parent_key.upcase! unless @case_sensitive
                if @lines[key]
                    @warnings << "Duplicate key '#{key}' encountered and ignored at line #{line_num}"
                else
                    @index[parent_key] << key
                    @lines[key] = line
                end
            end
        end
def index_source ( lines , options ) @lines = { } @index = Hash . new { | h , k | h [ k ] = [ ] } if @field_names index_fields end @case_sensitive = options . fetch ( :case_sensitive , true ) @trim_whitespace = options . fetch ( :trim_whitespace , false ) line_num = 0 lines . each do | row | line_num += 1 next if line_num == 1 && @field_names && options [ :ignore_header ] unless @field_names @field_names = row index_fields next end field_vals = row line = { } @field_names . each_with_index do | field , i | line [ field ] = field_vals [ i ] line [ field ] . strip! if @trim_whitespace && line [ field ] end key_values = @key_field_indexes . map { | kf | field_vals [ kf ] . to_s . upcase } key = key_values . join ( '~' ) parent_key = key_values [ 0 ... ( @parent_fields . length ) ] . join ( '~' ) parent_key . upcase! unless @case_sensitive if @lines [ key ] @warnings << "Duplicate key '#{key}' encountered and ignored at line #{line_num}" else @index [ parent_key ] << key @lines [ key ] = line end end end
Given an array of lines, where each line is an array of fields, indexes
the array contents so that it can be looked up by key.
Given an array of lines where each line is an array of fields indexes the array contents so that it can be looked up by key .
lines
<not_specific>
options
<not_specific>