Class: Sycsvpro::Allocator
- Inherits:
-
Object
- Object
- Sycsvpro::Allocator
- Defined in:
- lib/sycsvpro/allocator.rb
Overview
Allocates columns to a key column
Instance Attribute Summary collapse
-
#col_filter ⇒ Object
readonly
Filter for columns to allocate.
-
#infile ⇒ Object
readonly
File from that values are read.
-
#key_filter ⇒ Object
readonly
Filter for the key column that the values are allocated at.
-
#outfile ⇒ Object
readonly
File to that result of allocation is written.
-
#row_filter ⇒ Object
readonly
Filter for rows to consider.
Instance Method Summary collapse
-
#execute ⇒ Object
Executes the allocator and assigns column values to the key.
-
#initialize(options = {}) ⇒ Allocator
constructor
Creates a new allocator.
Constructor Details
#initialize(options = {}) ⇒ Allocator
Creates a new allocator. Options are infile, outfile, key, rows and columns to allocate to key
19 20 21 22 23 24 25 |
# File 'lib/sycsvpro/allocator.rb', line 19 def initialize(={}) @infile = [:infile] @outfile = [:outfile] @key_filter = ColumnFilter.new([:key]) @row_filter = RowFilter.new([:rows]) @col_filter = ColumnFilter.new([:cols]) end |
Instance Attribute Details
#col_filter ⇒ Object (readonly)
Filter for columns to allocate
14 15 16 |
# File 'lib/sycsvpro/allocator.rb', line 14 def col_filter @col_filter end |
#infile ⇒ Object (readonly)
File from that values are read
8 9 10 |
# File 'lib/sycsvpro/allocator.rb', line 8 def infile @infile end |
#key_filter ⇒ Object (readonly)
Filter for the key column that the values are allocated at
16 17 18 |
# File 'lib/sycsvpro/allocator.rb', line 16 def key_filter @key_filter end |
#outfile ⇒ Object (readonly)
File to that result of allocation is written
10 11 12 |
# File 'lib/sycsvpro/allocator.rb', line 10 def outfile @outfile end |
#row_filter ⇒ Object (readonly)
Filter for rows to consider
12 13 14 |
# File 'lib/sycsvpro/allocator.rb', line 12 def row_filter @row_filter end |
Instance Method Details
#execute ⇒ Object
Executes the allocator and assigns column values to the key
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/sycsvpro/allocator.rb', line 28 def execute allocation = {} File.open(infile).each_with_index do |line, index| row = row_filter.process(line, row: index) next if row.nil? key = key_filter.process(row) allocation[key] = [] if allocation[key].nil? allocation[key] << col_filter.process(row).split(';') end File.open(outfile, 'w') do |out| allocation.each do |key, values| out.puts "#{key};#{values.flatten.uniq.sort.join(';')}" end end end |