Class: Sycsvpro::Collector
- Inherits:
-
Object
- Object
- Sycsvpro::Collector
- Defined in:
- lib/sycsvpro/collector.rb
Overview
Collects values from rows and groups them in categories
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
collected values assigned to categories.
-
#infile ⇒ Object
readonly
infile contains the data that is operated on.
-
#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
Execute the collector.
-
#initialize(options = {}) ⇒ Collector
constructor
Creates a new Collector.
Constructor Details
Instance Attribute Details
#collection ⇒ Object (readonly)
collected values assigned to categories
17 18 19 |
# File 'lib/sycsvpro/collector.rb', line 17 def collection @collection end |
#infile ⇒ Object (readonly)
infile contains the data that is operated on
11 12 13 |
# File 'lib/sycsvpro/collector.rb', line 11 def infile @infile end |
#outfile ⇒ Object (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_filter ⇒ Object (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
#execute ⇒ Object
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 |