Class: Sycsvpro::Calculator

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

Overview

Processes arithmetic operations on columns of a csv file. A column value has to be a number. Possible operations are +, -, * and /. It is also possible to use values of columns as an operator like c1*2 will multiply the value of column 1 with 2.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Dsl

#rows, #unstring, #write_to

Constructor Details

#initialize(options = {}) ⇒ Calculator

Creates a new Calculator. Options expects :infile, :outfile, :rows and :columns. Optionally a header can be provided. The header can be supplemented with additional column names that are generated due to a arithmetic operation that creates new columns



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

def initialize(options={})
  @infile     = options[:infile]
  @outfile    = options[:outfile]
  @row_filter = RowFilter.new(options[:rows])
  @header     = Header.new(options[:header])
  @formulae   = {}
  create_calculator(options[:cols])
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args, &block) ⇒ Object

Retrieves the values from a row as the result of a arithmetic operation



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

def method_missing(id, *args, &block)
  to_number(columns[$1.to_i]) if id =~ /c(\d+)/
end

Instance Attribute Details

#columnsObject (readonly)

filter that is used for columns



26
27
28
# File 'lib/sycsvpro/calculator.rb', line 26

def columns
  @columns
end

#formulaeObject (readonly)

the operations on columns



22
23
24
# File 'lib/sycsvpro/calculator.rb', line 22

def formulae
  @formulae
end

#headerObject (readonly)

header of the outfile



24
25
26
# File 'lib/sycsvpro/calculator.rb', line 24

def header
  @header
end

#infileObject (readonly)

infile contains the data that is operated on



16
17
18
# File 'lib/sycsvpro/calculator.rb', line 16

def infile
  @infile
end

#outfileObject (readonly)

outfile is the file where the result is written to



18
19
20
# File 'lib/sycsvpro/calculator.rb', line 18

def outfile
  @outfile
end

#row_filterObject (readonly)

filter that is used for rows



20
21
22
# File 'lib/sycsvpro/calculator.rb', line 20

def row_filter
  @row_filter
end

Instance Method Details

#executeObject

Executes the calculator



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/sycsvpro/calculator.rb', line 46

def execute
  processed_header = false

  File.open(outfile, 'w') do |out|
    File.open(infile).each_with_index do |line, index|
      next if line.chomp.empty?

      unless processed_header
        header_row = header.process(line.chomp)
        out.puts header_row unless header_row.empty?
        processed_header = true
        next
      end

      next if row_filter.process(line, row: index).nil?

      @columns = unstring(line).chomp.split(';')
      formulae.each do |col, formula|
        @columns[col.to_i] = eval(formula)
      end
      out.puts @columns.join(';')
    end
  end
end