Class: ETL::Transform::CalculationTransform

Inherits:
Transform show all
Defined in:
lib/etl/transform/calculation_transform.rb

Instance Attribute Summary collapse

Attributes inherited from Transform

#configuration, #control, #name

Instance Method Summary collapse

Methods inherited from Transform

benchmarks, transform

Constructor Details

#initialize(control, name, configuration) ⇒ CalculationTransform

Returns a new instance of CalculationTransform



7
8
9
10
11
# File 'lib/etl/transform/calculation_transform.rb', line 7

def initialize(control, name, configuration)
  @function = configuration[:function]
  @fields = configuration[:fields]
  super
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields



5
6
7
# File 'lib/etl/transform/calculation_transform.rb', line 5

def fields
  @fields
end

#functionObject (readonly)

Returns the value of attribute function



4
5
6
# File 'lib/etl/transform/calculation_transform.rb', line 4

def function
  @function
end

Instance Method Details

#transform(name, value, row) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/etl/transform/calculation_transform.rb', line 13

def transform(name, value, row)
  return nil if row.nil?
  return nil if row[@fields[0]].nil?

  if (@function.eql? "A + B")
    result = ""
    @fields.each do |field|
      next if field.nil?

      string = ""
      if field.to_s.eql? field
        string = field
        begin
          string = eval('"' + field + '"')
        rescue
        end
      else
        string = row[field]
      end
      next if string.nil?

      result = result + string
    end

    row[name] = result
  end

  if (@function.eql? "date A")
    first = row[@fields[0]]
    row[name] = Time.parse(first)
  end

  if (@function.eql? "trim A")
    first = row[@fields[0]]
    row[name] = first.strip
  end

  if (@function.eql? "lower A")
    first = row[@fields[0]]
    row[name] = first.downcase
  end

  if (@function.eql? "upper A")
    first = row[@fields[0]]
    row[name] = first.upcase
  end

  if (@function.eql? "encoding A")
    # Bug from ruby 1.8 http://po-ru.com/diary/fixing-invalid-utf-8-in-ruby-revisited/
    first = row[@fields[0]]
    row[name] = Iconv.conv(@fields[1], @fields[2], first + ' ')[0..-2]
  end

  row[name]
end