Class: ETL::Transform::Transform

Inherits:
Object
  • Object
show all
Defined in:
lib/etl/transform/transform.rb

Overview

Base class for transforms.

A transform converts one value to another value using some sort of algorithm.

A simple transform has two arguments, the field to transform and the name of the transform:

transform :ssn, :sha1

Transforms can also be blocks:

transform(:ssn){ |v| v[0,24] }

Finally, a transform can include a configuration hash:

transform :sex, :decode, {:decode_table_path => 'delimited_decode.txt'}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(control, name, configuration = {}) ⇒ Transform

Initialize the transform object with the given control object, field name and configuration hash



50
51
52
53
54
# File 'lib/etl/transform/transform.rb', line 50

def initialize(control, name, configuration={})
  @control = control
  @name = name
  @configuration = configuration
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



46
47
48
# File 'lib/etl/transform/transform.rb', line 46

def configuration
  @configuration
end

#controlObject (readonly)

Returns the value of attribute control.



46
47
48
# File 'lib/etl/transform/transform.rb', line 46

def control
  @control
end

#nameObject (readonly)

Returns the value of attribute name.



46
47
48
# File 'lib/etl/transform/transform.rb', line 46

def name
  @name
end

Class Method Details

.benchmarksObject



41
42
43
# File 'lib/etl/transform/transform.rb', line 41

def benchmarks
  @benchmarks ||= {}
end

.transform(name, value, row, transforms) ⇒ Object

Transform the specified value using the given transforms. The transforms can either be Proc objects or objects which extend from Transform and implement the method transform(value). Any other object will result in a ControlError being raised.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/etl/transform/transform.rb', line 23

def transform(name, value, row, transforms)
  transforms.each do |transform|
    benchmarks[transform.class] ||= 0
    benchmarks[transform.class] += Benchmark.realtime do
      Engine.logger.debug "Transforming field #{name} with #{transform.inspect}"
      case transform
      when Proc
        value = transform.call([name, value, row])
      when Transform
        value = transform.transform(name, value, row)
      else
        raise ControlError, "Unsupported transform configuration type: #{transform}"
      end
    end
  end
  value
end

Instance Method Details

#transform(name, value, row) ⇒ Object



56
57
58
# File 'lib/etl/transform/transform.rb', line 56

def transform(name, value, row)
  raise "transform is an abstract method"
end