Class: Sycsvpro::Aggregator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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(options={})
  @infile     = options[:infile]
  @outfile    = options[:outfile]
  @headerless = options[:headerless] || false
  @row_filter = RowFilter.new(options[:rows], df: options[:df])
  @col_filter = ColumnFilter.new(options[:cols], df: options[:df])
  @key_values = Hash.new(0)
  @heading    = []
  @sums       = Hash.new(0)
  init_sum_scheme(options[:sum])
end

Instance Attribute Details

#col_filterObject (readonly)

filter that is used for columns



23
24
25
# File 'lib/sycsvpro/aggregator.rb', line 23

def col_filter
  @col_filter
end

#headerlessObject (readonly)

file doesn’t contain a header



19
20
21
# File 'lib/sycsvpro/aggregator.rb', line 19

def headerless
  @headerless
end

#headingObject (readonly)

header of the out file



27
28
29
# File 'lib/sycsvpro/aggregator.rb', line 27

def heading
  @heading
end

#infileObject (readonly)

infile contains the data that is operated on



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

def infile
  @infile
end

#key_valuesObject (readonly)

values that are aggregated



25
26
27
# File 'lib/sycsvpro/aggregator.rb', line 25

def key_values
  @key_values
end

#outfileObject (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_filterObject (readonly)

filter that is used for rows



21
22
23
# File 'lib/sycsvpro/aggregator.rb', line 21

def row_filter
  @row_filter
end

#sum_colObject (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_titleObject (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_rowObject (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_titleObject (readonly)

Title of the sum row



29
30
31
# File 'lib/sycsvpro/aggregator.rb', line 29

def sum_row_title
  @sum_row_title
end

#sumsObject (readonly)

sums of the column values



37
38
39
# File 'lib/sycsvpro/aggregator.rb', line 37

def sums
  @sums
end

Instance Method Details

#executeObject

Executes the aggregator



62
63
64
65
# File 'lib/sycsvpro/aggregator.rb', line 62

def execute
  process_aggregation
  write_result
end

#process_aggregationObject

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_resultObject

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