Class: Sycsvpro::Mapper

Inherits:
Object
  • Object
show all
Includes:
Dsl
Defined in:
lib/sycsvpro/mapper.rb

Overview

Map values to new values described in a mapping file

in.csv

| ID | Name | | — | —- | | 1 | Hank | | 2 | Jane |

mapping

1:01 2:02

Sycsvpro::Mapping.new(infile: “in.csv”,

outfile: "out.csv",
mapping: "mapping",
cols:    "0").execute

out.csv

| ID | Name | | — | —- | | 01 | Hank | | 02 | Jane |

Constant Summary

Constants included from Dsl

Dsl::COMMA_SPLITTER_REGEX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Dsl

#clean_up, #params, #rows, #split_by_comma_regex, #str2utf8, #unstring, #write_to

Constructor Details

#initialize(options = {}) ⇒ Mapper

Creates new mapper :call-seq:

Sycsvpro::Mapper.new(infile: "in.csv",
                     outfile: "out.csv",
                     mapping: "mapping.csv",
                     rows:    "1,3-5",
                     cols:    "3,4-7"
                     df:      "%Y-%m-%d").execute
infile

File that contains columns to be mapped

outfile

File that contains the mapping result after execute

mapping

File that contains the mappings. Mappings are separated by ‘:’

rows

Rows to consider for mappings

cols

Columns that should be mapped

df

Date format for row filter if rows are filtered on date values



58
59
60
61
62
63
64
65
# File 'lib/sycsvpro/mapper.rb', line 58

def initialize(options={})
  @infile = options[:infile]
  @outfile = options[:outfile]
  @row_filter = RowFilter.new(options[:rows], df: options[:df])
  @col_filter = init_col_filter(options[:cols], @infile)
  @mapper = {}
  init_mapper(options[:mapping])
end

Instance Attribute Details

#col_filterObject (readonly)

filter that contains columns that are considered for mappings



41
42
43
# File 'lib/sycsvpro/mapper.rb', line 41

def col_filter
  @col_filter
end

#infileObject (readonly)

infile contains the data that is operated on



33
34
35
# File 'lib/sycsvpro/mapper.rb', line 33

def infile
  @infile
end

#mapperObject (readonly)

file that contains the mappings from existing column values to new values



37
38
39
# File 'lib/sycsvpro/mapper.rb', line 37

def mapper
  @mapper
end

#outfileObject (readonly)

outfile is the file where the result is written to



35
36
37
# File 'lib/sycsvpro/mapper.rb', line 35

def outfile
  @outfile
end

#row_filterObject (readonly)

filter that is used for rows



39
40
41
# File 'lib/sycsvpro/mapper.rb', line 39

def row_filter
  @row_filter
end

Instance Method Details

#executeObject

Executes the mapper



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sycsvpro/mapper.rb', line 68

def execute
  File.open(outfile, 'w') do |out|
    File.new(infile, 'r').each_with_index do |line, index|
      result = row_filter.process(line, row: index)
      next if result.chomp.empty? or result.nil?
      result += ' ' if result =~ /;$/
      cols = result.split(';')
      @col_filter.each do |key|
        substitute = mapper[cols[key]]
        cols[key] = substitute if substitute
      end
      out.puts cols.join(';').strip
    end
  end
end