Class: Sycsvpro::Calculator
- Inherits:
-
Object
- Object
- Sycsvpro::Calculator
- 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
-
#columns ⇒ Object
readonly
filter that is used for columns.
-
#formulae ⇒ Object
readonly
the operations on columns.
-
#header ⇒ Object
readonly
header of the outfile.
-
#infile ⇒ Object
readonly
infile contains the data that is operated on.
-
#outfile ⇒ Object
readonly
outfile is the file where the result is written to.
-
#row_filter ⇒ Object
readonly
filter that is used for rows.
Instance Method Summary collapse
-
#execute ⇒ Object
Executes the calculator.
-
#initialize(options = {}) ⇒ Calculator
constructor
Creates a new Calculator.
-
#method_missing(id, *args, &block) ⇒ Object
Retrieves the values from a row as the result of a arithmetic operation.
Methods included from Dsl
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(={}) @infile = [:infile] @outfile = [:outfile] @row_filter = RowFilter.new([:rows]) @header = Header.new([:header]) @formulae = {} create_calculator([: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
#columns ⇒ Object (readonly)
filter that is used for columns
26 27 28 |
# File 'lib/sycsvpro/calculator.rb', line 26 def columns @columns end |
#formulae ⇒ Object (readonly)
the operations on columns
22 23 24 |
# File 'lib/sycsvpro/calculator.rb', line 22 def formulae @formulae end |
#header ⇒ Object (readonly)
header of the outfile
24 25 26 |
# File 'lib/sycsvpro/calculator.rb', line 24 def header @header end |
#infile ⇒ Object (readonly)
infile contains the data that is operated on
16 17 18 |
# File 'lib/sycsvpro/calculator.rb', line 16 def infile @infile end |
#outfile ⇒ Object (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_filter ⇒ Object (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
#execute ⇒ Object
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 |