Class: Optimus::Transformers::ColumnCalculator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/transformers/column_calculator.rb

Overview

note: to determine if number: ^(((d1,3)(,d3)*)|(d+))(.d+)?$ Better: If you’ve got funny numbers, coerce into numericness. Make coercion very robust.

Defined Under Namespace

Classes: ComputedColumn, Evaluatable

Constant Summary collapse

DEFAULT_COL_OPTS =
{
  :reset_when => true,
  :count_when => false,
  :count_by => :next
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser = Optimus::ParsedCalculator::ExpressionParser.new) ⇒ ColumnCalculator

Returns a new instance of ColumnCalculator.



33
34
35
36
37
38
39
# File 'lib/transformers/column_calculator.rb', line 33

def initialize(parser = Optimus::ParsedCalculator::ExpressionParser.new)
  @computed_column_names = []
  @computed_columns = {}
  @computed_data = nil
  @parser = parser
  @sort_expression = nil
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



23
24
25
# File 'lib/transformers/column_calculator.rb', line 23

def data
  @data
end

#sort_expressionObject

Returns the value of attribute sort_expression.



24
25
26
# File 'lib/transformers/column_calculator.rb', line 24

def sort_expression
  @sort_expression
end

Instance Method Details

#[](index) ⇒ Object



82
83
84
# File 'lib/transformers/column_calculator.rb', line 82

def [](index)
  computed_data[index]
end

#columnsObject



74
75
76
# File 'lib/transformers/column_calculator.rb', line 74

def columns
  @data.columns + @computed_column_names
end

#computed_column(name, start_val, options = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/transformers/column_calculator.rb', line 50

def computed_column(name, start_val, options = {})
  if columns.include?(name)
    raise DuplicateColumnError.new("Can't add duplicate column name #{name}")
  end
  sve = Evaluatable.new(start_val, @parser)
  @computed_column_names << name
  new_opts = DEFAULT_COL_OPTS.merge(options)
  DEFAULT_COL_OPTS.keys.each do |key|
    new_opts[key] = Evaluatable.new(new_opts[key], @parser)
  end
  @computed_columns[name] = ComputedColumn.new(
    name, sve, new_opts
  )
  reset!
end

#copydown_column(new_name, old_name) ⇒ Object



66
67
68
# File 'lib/transformers/column_calculator.rb', line 66

def copydown_column(new_name, old_name)
  computed_column(new_name, "{#{old_name}}", :reset_when => "{#{old_name}}")
end

#counter_column(name, start_val = 1, options = {}) ⇒ Object



70
71
72
# File 'lib/transformers/column_calculator.rb', line 70

def counter_column(name, start_val = 1, options = {})
  computed_column(name, start_val, options = {})
end

#each(&block) ⇒ Object



78
79
80
# File 'lib/transformers/column_calculator.rb', line 78

def each(&block)
  computed_data.each(&block)
end