lib/sycsvpro/join.rb
def execute
      processed_header = headerless ? true : false

      File.open(outfile, 'w') do |out|
        File.open(infile).each_with_index do |line, index|
          line = line.chomp

          next if line.empty?

          line = unstring(line).chomp

          unless processed_header
            header_line = header.process(line)
            out.puts header unless header_line.empty?
            processed_header = true
            next
          end

          next if row_filter.process(line, row: index).nil?

          values = line.split(';')
          target = values.dup

          @positions.sort.each { |p| target.insert(p, "") }
          
          @joiners.each do |joiner|
            key = values[joiner.join[1]]
            row = joiner.lookup[:rows][key] || []
            joiner.pos.each_with_index { |p,i| target[p] = row[i] }
          end

          out.puts target.join(';')
        end
      end
    end
def execute
      processed_header = headerless ? true : false

      File.open(outfile, 'w') do |out|
        File.open(infile).each_with_index do |line, index|
          line = line.chomp

          next if line.empty?

          line = unstring(line).chomp

          unless processed_header
            header_line = header.process(line)
            out.puts header unless header_line.empty?
            processed_header = true
            next
          end

          next if row_filter.process(line, row: index).nil?

          values = line.split(';')
          target = values.dup

          @positions.sort.each { |p| target.insert(p, "") }
          
          @joiners.each do |joiner|
            key = values[joiner.join[1]]
            row = joiner.lookup[:rows][key] || []
            joiner.pos.each_with_index { |p,i| target[p] = row[i] }
          end

          out.puts target.join(';')
        end
      end
    end
def execute processed_header = headerless ? true : false File . open ( outfile , 'w' ) do | out | File . open ( infile ) . each_with_index do | line , index | line = line . chomp next if line . empty? line = unstring ( line ) . chomp unless processed_header header_line = header . process ( line ) out . puts header unless header_line . empty? processed_header = true next end next if row_filter . process ( line , row : index ) . nil? values = line . split ( ';' ) target = values . dup @positions . sort . each { | p | target . insert ( p , "" ) } @joiners . each do | joiner | key = values [ joiner . join [ 1 ] ] row = joiner . lookup [ :rows ] [ key ] || [ ] joiner . pos . each_with_index { | p , i | target [ p ] = row [ i ] } end out . puts target . join ( ';' ) end end end
Creates a Join which can be invoked as follows
 :call-seq:
   Sycsvpro::Join.new(infile:  "in.csv",
                      outfile: "out.csv",
                      source:  "source.csv",
                      rows:    "1-eof",
                      cols:    "0,1",
                      pos:     "2,3",
                      joins:   "2=1",
                      headerless: true,
                      header:  "*",
                      insert_header: "Company,Phone").execute

 infile:: csv file to operate on
 outfile:: csv file with the result
 source:: csv file that contains the values to join to infile
 rows:: rows to consider for operation. Rows that don't match the pattern
        will be skipped for operation
 cols:: columns to insert from the source to the infile
 pos:: column positions where to insert the values and the insert_header
       columns
 joins:: columns that match in infile and source.
         source_column=infile_column
 headerless:: indicates whether the infile has a header (default true)
 header:: Header of the csv file
 insert_header:: column names of the to be inserted values
 Executes the join
Creates a Join which can be invoked as follows : call - seq : Sycsvpro :: Join . new ( infile : in . csv outfile : out . csv source : source . csv rows : 1 - eof cols : 0 1 pos : 2 3 joins : 2 = 1 headerless : true header : * insert_header : Company Phone ) . execute