Class: Sycsvpro::Counter
- Inherits:
-
Object
- Object
- Sycsvpro::Counter
- Includes:
- Dsl
- Defined in:
- lib/sycsvpro/counter.rb
Overview
Counter counts values and uses the values as column names and uses the count as the column value
Constant Summary
Constants included from Dsl
Instance Attribute Summary collapse
-
#col_filter ⇒ Object
readonly
filter that is used for columns.
-
#heading ⇒ Object
readonly
header of the out file.
-
#heading_sort ⇒ Object
readonly
indicates whether the headline values should be sorted.
-
#infile ⇒ Object
readonly
infile contains the data that is operated on.
-
#key_columns ⇒ Object
readonly
values are assigned to the key columns.
-
#key_titles ⇒ Object
readonly
key columns headers.
-
#key_values ⇒ Object
readonly
values that are assigned to the key column.
-
#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_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 counter.
-
#initialize(options = {}) ⇒ Counter
constructor
Creates a new counter.
-
#process_count ⇒ Object
Processes the counting on the in file.
-
#write_result ⇒ Object
Writes the count results.
Methods included from Dsl
#clean_up, #params, #rows, #split_by_comma_regex, #str2utf8, #unstring, #write_to
Constructor Details
#initialize(options = {}) ⇒ Counter
Creates a new counter. Takes as attributes infile, outfile, key, rows, cols, date-format and indicator whether to add a sum row
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/sycsvpro/counter.rb', line 43 def initialize(={}) @infile = [:infile] @outfile = [:outfile] init_key_columns([:key]) @row_filter = RowFilter.new([:rows], df: [:df]) @col_filter = ColumnFilter.new([:cols], df: [:df]) @key_values = {} @heading = [] @heading_sort = [:sort].nil? ? true : [:sort] init_sum_scheme([:sum]) @sums = Hash.new(0) end |
Instance Attribute Details
#col_filter ⇒ Object (readonly)
filter that is used for columns
25 26 27 |
# File 'lib/sycsvpro/counter.rb', line 25 def col_filter @col_filter end |
#heading ⇒ Object (readonly)
header of the out file
29 30 31 |
# File 'lib/sycsvpro/counter.rb', line 29 def heading @heading end |
#heading_sort ⇒ Object (readonly)
indicates whether the headline values should be sorted
31 32 33 |
# File 'lib/sycsvpro/counter.rb', line 31 def heading_sort @heading_sort end |
#infile ⇒ Object (readonly)
infile contains the data that is operated on
15 16 17 |
# File 'lib/sycsvpro/counter.rb', line 15 def infile @infile end |
#key_columns ⇒ Object (readonly)
values are assigned to the key columns
19 20 21 |
# File 'lib/sycsvpro/counter.rb', line 19 def key_columns @key_columns end |
#key_titles ⇒ Object (readonly)
key columns headers
21 22 23 |
# File 'lib/sycsvpro/counter.rb', line 21 def key_titles @key_titles end |
#key_values ⇒ Object (readonly)
values that are assigned to the key column
27 28 29 |
# File 'lib/sycsvpro/counter.rb', line 27 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/counter.rb', line 17 def outfile @outfile end |
#row_filter ⇒ Object (readonly)
filter that is used for rows
23 24 25 |
# File 'lib/sycsvpro/counter.rb', line 23 def row_filter @row_filter end |
#sum_col_title ⇒ Object (readonly)
Title of the sum column
37 38 39 |
# File 'lib/sycsvpro/counter.rb', line 37 def sum_col_title @sum_col_title end |
#sum_row ⇒ Object (readonly)
row where to add the sums of the columns
35 36 37 |
# File 'lib/sycsvpro/counter.rb', line 35 def sum_row @sum_row end |
#sum_row_title ⇒ Object (readonly)
Title of the sum row
33 34 35 |
# File 'lib/sycsvpro/counter.rb', line 33 def sum_row_title @sum_row_title end |
#sums ⇒ Object (readonly)
sums of the column values
39 40 41 |
# File 'lib/sycsvpro/counter.rb', line 39 def sums @sums end |
Instance Method Details
#execute ⇒ Object
Executes the counter
57 58 59 60 |
# File 'lib/sycsvpro/counter.rb', line 57 def execute process_count write_result end |
#process_count ⇒ Object
Processes the counting on the in file
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/sycsvpro/counter.rb', line 63 def process_count 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? key = unstring(line).split(';').values_at(*key_columns) key_value = key_values[key] || key_values[key] = { name: key, elements: Hash.new(0), sum: 0 } result.chomp.split(';').each do |column| heading << column if heading.index(column).nil? key_value[:elements][column] += 1 key_value[:sum] += 1 sums[column] += 1 end end end end |
#write_result ⇒ Object
Writes the count results
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/sycsvpro/counter.rb', line 82 def write_result sum_line = [sum_row_title] + [''] * (key_titles.size - 1) headline = heading_sort ? heading.sort : original_pivot_sequence_heading headline << add_sum_col unless sum_col_title.nil? headline.each do |h| sum_line << sums[h] end row = 0; File.open(outfile, 'w') do |out| out.puts sum_line.join(';') if row == sum_row ; row += 1 out.puts (key_titles + headline).join(';') key_values.each do |k,v| out.puts sum_line.join(';') if row == sum_row ; row += 1 line = [k] headline.each do |h| line << v[:elements][h] unless h == sum_col_title end line << v[:sum] unless sum_col_title.nil? out.puts line.join(';') end end end |