Class: Sycsvpro::Aggregator
- Inherits:
-
Object
- Object
- Sycsvpro::Aggregator
- Includes:
- Dsl
- Defined in:
- lib/sycsvpro/aggregator.rb
Overview
An Aggregator counts specified row values and adds a sum to the end of the row
Instance Attribute Summary collapse
-
#col_filter ⇒ Object
readonly
filter that is used for columns.
-
#headerless ⇒ Object
readonly
file doesn’t contain a header.
-
#heading ⇒ Object
readonly
header of the out file.
-
#infile ⇒ Object
readonly
infile contains the data that is operated on.
-
#key_values ⇒ Object
readonly
values that are aggregated.
-
#outfile ⇒ Object
readonly
outfile is the file where the result is written to.
-
#row_filter ⇒ Object
readonly
filter that is used for rows.
-
#sum_col ⇒ Object
readonly
column where to add the sum of the row sum.
-
#sum_col_title ⇒ Object
readonly
Title of the sum column.
-
#sum_row ⇒ Object
readonly
row where to add the sums of the columns.
-
#sum_row_title ⇒ Object
readonly
Title of the sum row.
-
#sums ⇒ Object
readonly
sums of the column values.
Instance Method Summary collapse
-
#execute ⇒ Object
Executes the aggregator.
-
#initialize(options = {}) ⇒ Aggregator
constructor
Creates a new aggregator.
-
#process_aggregation ⇒ Object
Process the aggregation of the key values.
-
#write_result ⇒ Object
Writes the aggration results.
Methods included from Dsl
#clean_up, #params, #rows, #str2utf8, #unstring, #write_to
Constructor Details
#initialize(options = {}) ⇒ Aggregator
Creates a new aggregator. Takes as attributes infile, outfile, key, rows, cols, date-format and indicator whether to add a sum row :call-seq:
Sycsvpro::Aggregator.new(infile: "in.csv",
outfile: "out.csv",
headerless: false,
rows: "1,2-4,/\S/"
cols: "0,5",
df: "%d.%m.%Y",
sum: "Total:1,Items").execute
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/sycsvpro/aggregator.rb', line 49 def initialize(={}) @infile = [:infile] @outfile = [:outfile] @headerless = [:headerless] || false @row_filter = RowFilter.new([:rows], df: [:df]) @col_filter = ColumnFilter.new([:cols], df: [:df]) @key_values = Hash.new(0) @heading = [] @sums = Hash.new(0) init_sum_scheme([:sum]) end |
Instance Attribute Details
#col_filter ⇒ Object (readonly)
filter that is used for columns
23 24 25 |
# File 'lib/sycsvpro/aggregator.rb', line 23 def col_filter @col_filter end |
#headerless ⇒ Object (readonly)
file doesn’t contain a header
19 20 21 |
# File 'lib/sycsvpro/aggregator.rb', line 19 def headerless @headerless end |
#heading ⇒ Object (readonly)
header of the out file
27 28 29 |
# File 'lib/sycsvpro/aggregator.rb', line 27 def heading @heading end |
#infile ⇒ Object (readonly)
infile contains the data that is operated on
15 16 17 |
# File 'lib/sycsvpro/aggregator.rb', line 15 def infile @infile end |
#key_values ⇒ Object (readonly)
values that are aggregated
25 26 27 |
# File 'lib/sycsvpro/aggregator.rb', line 25 def key_values @key_values end |
#outfile ⇒ Object (readonly)
outfile is the file where the result is written to
17 18 19 |
# File 'lib/sycsvpro/aggregator.rb', line 17 def outfile @outfile end |
#row_filter ⇒ Object (readonly)
filter that is used for rows
21 22 23 |
# File 'lib/sycsvpro/aggregator.rb', line 21 def row_filter @row_filter end |
#sum_col ⇒ Object (readonly)
column where to add the sum of the row sum
35 36 37 |
# File 'lib/sycsvpro/aggregator.rb', line 35 def sum_col @sum_col end |
#sum_col_title ⇒ Object (readonly)
Title of the sum column
33 34 35 |
# File 'lib/sycsvpro/aggregator.rb', line 33 def sum_col_title @sum_col_title end |
#sum_row ⇒ Object (readonly)
row where to add the sums of the columns
31 32 33 |
# File 'lib/sycsvpro/aggregator.rb', line 31 def sum_row @sum_row end |
#sum_row_title ⇒ Object (readonly)
Title of the sum row
29 30 31 |
# File 'lib/sycsvpro/aggregator.rb', line 29 def sum_row_title @sum_row_title end |
#sums ⇒ Object (readonly)
sums of the column values
37 38 39 |
# File 'lib/sycsvpro/aggregator.rb', line 37 def sums @sums end |
Instance Method Details
#execute ⇒ Object
Executes the aggregator
62 63 64 65 |
# File 'lib/sycsvpro/aggregator.rb', line 62 def execute process_aggregation write_result end |
#process_aggregation ⇒ Object
Process the aggregation of the key values. The result will be written to outfile
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/sycsvpro/aggregator.rb', line 69 def process_aggregation File.new(infile).each_with_index do |line, index| result = col_filter.process(row_filter.process(line.chomp, row: index)) unless result.nil? or result.empty? if heading.empty? and not headerless heading << result.split(';') next else @sum_col = [result.split(';').size, sum_col].max end key_values[result] += 1 sums[sum_col_title] += 1 end end heading.flatten! heading[sum_col] = sum_col_title end |
#write_result ⇒ Object
Writes the aggration results
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/sycsvpro/aggregator.rb', line 88 def write_result sum_line = [sum_row_title] (heading.size - 2).times { sum_line << "" } sum_line << sums[sum_col_title] row = 0; File.open(outfile, 'w') do |out| out.puts sum_line.join(';') if row == sum_row ; row += 1 out.puts heading.join(';') key_values.each do |k, v| out.puts sum_line.join(';') if row == sum_row ; row += 1 out.puts [k, v].join(';') end end end |