Class: Itiel::Transform::CalculatedColumns

Inherits:
Object
  • Object
show all
Includes:
Nameable, ChainedStep
Defined in:
lib/itiel/transform/calculated_columns.rb

Overview

This class allow us to create multiple columns or replace the current value of a column by the result of the execution of a block, you must return a hash with the columns you wish to add as the hash keys.

Example:

calculated = CalculatedColumns.new do |row|

total = row['price'] * row['quantity']
tax   = total * 0.2
{ :total => total, :tax => tax }

end

calculated.input = [=> 12.50, :quantity => 3,=> 4.95, :quantity => 5] calculated.output

> => 12.50, :quantity => 3, :total => 37.5, :tax => 7.5,=> 4.95, :quantity => 5, :total => 24.75, :tax => 4.95

It is important to note that you have to use casting on numeric fields just to make sure that it is from the right type. Stored column types may vary depending on the source.

Instance Attribute Summary collapse

Attributes included from Nameable

#debug, #step_name

Attributes included from ChainedStep

#next_step

Instance Method Summary collapse

Methods included from ChainedStep

#input=

Constructor Details

#initialize(&block) ⇒ CalculatedColumns

Returns a new instance of CalculatedColumns.



30
31
32
33
# File 'lib/itiel/transform/calculated_columns.rb', line 30

def initialize(&block)
  raise "Missing block" unless block_given?
  @block = block
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



28
29
30
# File 'lib/itiel/transform/calculated_columns.rb', line 28

def arguments
  @arguments
end

Instance Method Details

#sanity_checkObject



42
43
44
# File 'lib/itiel/transform/calculated_columns.rb', line 42

def sanity_check
  raise Itiel::UndefinedNextStepException.new "Undefined next_step" unless self.next_step
end

#transform!(input_stream) ⇒ Object



35
36
37
38
39
40
# File 'lib/itiel/transform/calculated_columns.rb', line 35

def transform!(input_stream)
  sanity_check
  input_stream.each do |object|
    object.merge! @block.call(object)
  end
end