Module: Dsl
- Included in:
- Sycsvpro::Calculator, Sycsvpro::Counter, Sycsvpro::Header, Sycsvpro::Profiler, Sycsvpro::RowFilter
- Defined in:
- lib/sycsvpro/dsl.rb
Overview
Methods to be used in customer specific script files
Instance Method Summary collapse
-
#rows(options = {}) ⇒ Object
Retrieves rows and columns from the file and returns them to the block provided by the caller.
-
#unstring(line) ⇒ Object
Remove leading and trailing “ and spaces as well as reducing more than 2 spaces between words from csv values like ”a“;ba g;c;”d“;e to a;b ag;c;d;e.
-
#write_to(file) ⇒ Object
writes values provided by a block to the given file.
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(={}) infile = File.([:infile]) row_filter = Sycsvpro::RowFilter.new([:row_filter]) if [: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 = [] .each { |k,v| params << extract_values(values, k, v) if k =~ /column$|columns$/ } yield *params end end |
#unstring(line) ⇒ Object
Remove leading and trailing “ and spaces as well as reducing more than 2 spaces between words from csv values like ”a“;ba g;c;”d“;e to a;b ag;c;d;e
32 33 34 |
# File 'lib/sycsvpro/dsl.rb', line 32 def unstring(line) 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 |