Class: Sycsvpro::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/sycsvpro/collector.rb

Overview

Collects values from rows and groups them in categories

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Collector

Creates a new Collector



20
21
22
23
24
25
26
# File 'lib/sycsvpro/collector.rb', line 20

def initialize(options={})
  @infile = options[:infile]
  @outfile = options[:outfile]
  @row_filter = RowFilter.new(options[:rows], df: options[:df])
  @collection = {}
  init_collection(options[:cols])
end

Instance Attribute Details

#collectionObject (readonly)

collected values assigned to categories



17
18
19
# File 'lib/sycsvpro/collector.rb', line 17

def collection
  @collection
end

#infileObject (readonly)

infile contains the data that is operated on



11
12
13
# File 'lib/sycsvpro/collector.rb', line 11

def infile
  @infile
end

#outfileObject (readonly)

outfile is the file where the result is written to



13
14
15
# File 'lib/sycsvpro/collector.rb', line 13

def outfile
  @outfile
end

#row_filterObject (readonly)

filter that is used for rows



15
16
17
# File 'lib/sycsvpro/collector.rb', line 15

def row_filter
  @row_filter
end

Instance Method Details

#executeObject

Execute the collector



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sycsvpro/collector.rb', line 29

def execute
  File.new(infile).each_with_index do |line, index|
    row = row_filter.process(line, row: index)
    next if row.nil? or row.chomp.empty?
    collection.each do |category, elements|
      values = elements[:filter].process(row) 
      values.chomp.split(';').each do |value|
        elements[:entries] << value.chomp if elements[:entries].index(value.chomp).nil?
      end
    end
  end

  File.open(outfile, 'w') do |out|
    collection.each do |category, elements|
      out.puts "[#{category}]"
      elements[:entries].sort.each { |c| out.puts c }
    end
  end
end