Module: Dsl

Overview

Methods to be used in customer specific script files

Instance Method Summary collapse

Instance Method Details

#rows(options = {}) ⇒ Object

Retrieves rows and columns from the file and returns them to the block provided by the caller



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sycsvpro/dsl.rb', line 7

def rows(options={})
  infile     = File.expand_path(options[:infile])
  row_filter = Sycsvpro::RowFilter.new(options[:row_filter]) if options[:row_filter]

  File.new(infile).each_with_index do |line, index|
    next if line.chomp.empty? 
    next if !row_filter.nil? and row_filter.process(line.chomp, row: index).nil?

    values = line.chomp.split(';') 
    params = []
    options.each { |k,v| params << extract_values(values, k, v) if k =~ /column$|columns$/ }

    yield *params
  end
end

#str2utf8(str) ⇒ Object

Remove non-UTF chars from string



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

def str2utf8(str)
  str.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
end

#unstring(line) ⇒ Object

Remove leading and trailing “ and spaces as well as reducing more than 2 spaces between words from csv values. Replac ; with , from values as ; is used as value separator



32
33
34
35
36
37
38
# File 'lib/sycsvpro/dsl.rb', line 32

def unstring(line)
  line = str2utf8(line)
  line.scan(/(?<=^"|;")[^"]+(?=;)+[^"]*|;+[^"](?=";|"$)/).each do |value|
    line = line.gsub(value, value.gsub(';', ','))
  end
  line.gsub(/(?<=^|;)\s*"?\s*|\s*"?\s*(?=;|$)/, "").gsub(/\s{2,}/, " ") unless line.nil?
end

#write_to(file) ⇒ Object

writes values provided by a block to the given file



24
25
26
27
28
# File 'lib/sycsvpro/dsl.rb', line 24

def write_to(file)
  File.open(file, 'w') do |out|
    yield out
  end
end