Class: ETL::Transform::HierarchyLookupTransform

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

Overview

Transform which walks up the hierarchy tree to find a value of the current level’s value is nil.

TODO: Let the resolver be implemented in a class so different resolution methods are possible.

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 = {}) ⇒ HierarchyLookupTransform

Initialize the transform

Configuration options:

  • :target: The target connection name (required)

  • :parent_id_field: The name of the field to use for the parent ID (defaults to :parent_id)



20
21
22
23
24
# File 'lib/etl/transform/hierarchy_lookup_transform.rb', line 20

def initialize(control, name, configuration={})
  super
  @parent_id_field = configuration[:parent_id_field] || :parent_id
  @target = configuration[:target]
end

Instance Attribute Details

#parent_id_fieldObject

The name of the field to use for the parent ID



10
11
12
# File 'lib/etl/transform/hierarchy_lookup_transform.rb', line 10

def parent_id_field
  @parent_id_field
end

#targetObject

The target connection name



13
14
15
# File 'lib/etl/transform/hierarchy_lookup_transform.rb', line 13

def target
  @target
end

Instance Method Details

#lookup(field, table, parent_id, parent_id_field) ⇒ Object

Lookup the parent value.



42
43
44
45
46
# File 'lib/etl/transform/hierarchy_lookup_transform.rb', line 42

def lookup(field, table, parent_id, parent_id_field)
  q = "SELECT #{parent_id_field}, #{field} FROM #{table} WHERE id = #{parent_id}"
  row = ETL::Engine.connection(target).select_one(q)
  return row[parent_id_field.to_s], row[field.to_s]
end

#transform(name, value, row) ⇒ Object

Transform the value.



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

def transform(name, value, row)
  if parent_id = row[parent_id_field]
    # TODO: should use more than just the first source out of the control
    parent_id, value = lookup(name, 
      control.sources.first.configuration[:table], parent_id, parent_id_field)        
    until value || parent_id.nil?
      # TODO: should use more than just the first source out of the control
      parent_id, value = lookup(name, 
        control.sources.first.configuration[:table], parent_id, parent_id_field)
    end
  end
  value
end