Class: Sycsvpro::Counter

Inherits:
Object
  • Object
show all
Includes:
Dsl
Defined in:
lib/sycsvpro/counter.rb

Overview

Creates a new counter that counts values and uses the values as column names and uses the count as the column value

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Dsl

#rows, #unstring, #write_to

Constructor Details

#initialize(options = {}) ⇒ Counter

Creates a new counter



30
31
32
33
34
35
36
37
38
# File 'lib/sycsvpro/counter.rb', line 30

def initialize(options={})
  @infile     = options[:infile]
  @outfile    = options[:outfile]
  @key_column = options[:key].to_i
  @row_filter = RowFilter.new(options[:rows])
  @col_filter = ColumnFilter.new(options[:cols], df: options[:df])
  @key_values       = {}
  @heading    = []
end

Instance Attribute Details

#col_filterObject (readonly)

filter that is used for columns



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

def col_filter
  @col_filter
end

#headingObject (readonly)

header of the out file



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

def heading
  @heading
end

#infileObject (readonly)

infile contains the data that is operated on



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

def infile
  @infile
end

#key_columnObject (readonly)

values are assigned to the key column



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

def key_column
  @key_column
end

#key_valuesObject (readonly)

values that are assigned to the key column



25
26
27
# File 'lib/sycsvpro/counter.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/counter.rb', line 17

def outfile
  @outfile
end

#row_filterObject (readonly)

filter that is used for rows



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

def row_filter
  @row_filter
end

Instance Method Details

#executeObject

Executes the counter



41
42
43
44
# File 'lib/sycsvpro/counter.rb', line 41

def execute
  process_file
  write_result
end

#process_fileObject

Processes the counting on the in file



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sycsvpro/counter.rb', line 47

def process_file
  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(';')[key_column]
      key_value = key_values[key] || key_values[key] = { name: key, elements: Hash.new(0) }
      result.chomp.split(';').each do |column|
        heading << column if heading.index(column).nil?
        key_value[:elements][column] += 1
      end
    end
  end
end

#write_resultObject

Writes the results



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sycsvpro/counter.rb', line 62

def write_result
  File.open(outfile, 'w') do |out|
    out.puts (["key"] + heading.sort).join(';')
    key_values.each do |k,v|
      line = [k]
      heading.sort.each do |h|
        line << v[:elements][h]
      end
      out.puts line.join(';')
    end
  end
end