Class: Sycsvpro::SpreadSheetBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/sycsvpro/spread_sheet_builder.rb

Overview

SpreadSheetBuilder is used in the command line interface of sycsvpro to use SpreadSheet from the command line

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ SpreadSheetBuilder

A spread sheet builder is doing arithmetic operations and can be called like this:

SpreadSheetBuilder.new(outfile:   "out.csv",
                       files:     "f1.csv,f2.csv",
                       r:         "true,false",
                       c:         "false,true",
                       aliases:   "a,b",
                       operation: "(a*b).transpose",
                       ds:        ",",
                       equalize:  "true",
                       print:     "true").execute
outfile

file where the result of the operation is written to

files

files that hold the spread sheet data

r

indication whether the corresponding file has row labels

c

indication whether the corresponding file has column labels

aliases

symbols that correspond to the spread sheet created from the files. The symbols are used in the operation. The symbols have to be choosen carefully not to conflict with existing methods and variables

ds

decimal spearator ‘.’ or ‘,’ where ‘.’ is default

equalize

indicates whether different column sizes should be equalized

operation

arithmetic operation on spread sheets using the aliases as place holders for the spread sheets. The last evaluated operation is returned as result and saved to outfile in case the result is a spread sheet. In all other cases the result can be printed with the print flag.

print

print the result



47
48
49
50
51
52
# File 'lib/sycsvpro/spread_sheet_builder.rb', line 47

def initialize(opts = {})
  @print     = opts[:print]
  @operands  = create_operands(opts)
  @outfile   = opts[:outfile]
  @operation = opts[:operation]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Returns the spread sheet operands when called in the arithmetic operation



55
56
57
58
# File 'lib/sycsvpro/spread_sheet_builder.rb', line 55

def method_missing(name, *args, &block)
  super unless operands.keys.index(name.to_s)
  operands[name.to_s]
end

Instance Attribute Details

#operandsObject (readonly)

The operands, that is the spread sheets that are used in the arithmetic operation



12
13
14
# File 'lib/sycsvpro/spread_sheet_builder.rb', line 12

def operands
  @operands
end

#operationObject (readonly)

The spread sheet operation where the operands are used



14
15
16
# File 'lib/sycsvpro/spread_sheet_builder.rb', line 14

def operation
  @operation
end

#outfileObject (readonly)

The result of the SpreadSheet operation is written to this file



9
10
11
# File 'lib/sycsvpro/spread_sheet_builder.rb', line 9

def outfile
  @outfile
end

Indicates whether the result should be printed



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

def print
  @print
end

Instance Method Details

#executeObject

Executes the operation and writes the result to the outfile



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sycsvpro/spread_sheet_builder.rb', line 61

def execute
  result = eval(operation)      
  if outfile
    if result.is_a?(SpreadSheet)
      result.write(outfile)
    else
      puts
      puts "Warning: Result is no spread sheet and not written to file!"
      puts "         To view the result use -p flag" unless print
    end
  end

  if print
    puts
    puts "Operation"
    puts "---------"
    operation.split(';').each { |o| puts o }
    puts
    puts "Result"
    puts "------"
    if result.nil? || result.empty?
      puts result.inspect
    else
      puts result
    end
    puts
  end
end