Class: ETL::Transform::ForeignKeyLookupTransform

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

Overview

Transform which looks up the value and replaces it with a foriegn key reference

Direct Known Subclasses

FkLookupTransform

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

Initialize the foreign key lookup transform.

Configuration options: *:collection: A Hash of natural keys mapped to surrogate keys. If this is not specified then

an empty Hash will be used. This Hash will be used to cache values that have been resolved already
for future use.

*:resolver: Object or Class which implements the method resolve(value)



15
16
17
18
19
20
21
# File 'lib/etl/transform/foreign_key_lookup_transform.rb', line 15

def initialize(control, name, configuration={})
  super
  
  @collection = (configuration[:collection] || {})
  @resolver = configuration[:resolver]
  @resolver = @resolver.new if @resolver.is_a?(Class)
end

Instance Attribute Details

#resolverObject

The resolver to use if the foreign key is not found in the collection



6
7
8
# File 'lib/etl/transform/foreign_key_lookup_transform.rb', line 6

def resolver
  @resolver
end

Instance Method Details

#transform(name, value, row) ⇒ Object

Transform the value by resolving it to a foriegn key



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/etl/transform/foreign_key_lookup_transform.rb', line 24

def transform(name, value, row)
  fk = @collection[value]
  unless fk
    raise ResolverError, "Foreign key for #{value} not found and no resolver specified" unless resolver
    raise ResolverError, "Resolver does not appear to respond to resolve method" unless resolver.respond_to?(:resolve)
    fk = resolver.resolve(value)
    raise ResolverError, "Unable to resolve #{value} to foreign key for #{name} in row #{ETL::Engine.rows_read}" unless fk
    @collection[value] = fk
  end
  fk
end