Class: Sycsvpro::Mapper
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
Instance Attribute Summary collapse
-
#col_filter ⇒ Object
readonly
filter that contains columns that are considered for mappings.
-
#infile ⇒ Object
readonly
infile contains the data that is operated on.
-
#mapper ⇒ Object
readonly
file that contains the mappings from existing column values to new values.
-
#outfile ⇒ Object
readonly
outfile is the file where the result is written to.
-
#row_filter ⇒ Object
readonly
filter that is used for rows.
Instance Method Summary collapse
-
#execute ⇒ Object
Executes the mapper.
-
#initialize(options = {}) ⇒ Mapper
constructor
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.
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(={}) @infile = [:infile] @outfile = [:outfile] @row_filter = RowFilter.new([:rows], df: [:df]) @col_filter = init_col_filter([:cols], @infile) @mapper = {} init_mapper([:mapping]) end |
Instance Attribute Details
#col_filter ⇒ Object (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 |
#infile ⇒ Object (readonly)
infile contains the data that is operated on
33 34 35 |
# File 'lib/sycsvpro/mapper.rb', line 33 def infile @infile end |
#mapper ⇒ Object (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 |
#outfile ⇒ Object (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_filter ⇒ Object (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
#execute ⇒ Object
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 |