Class: ETL::Transform::ForeignKeyLookupTransform

Inherits:
Transform 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) *:default: A default foreign key to use if no foreign key is found *:cache: If true and the resolver responds to load_cache, load_cache will be called



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

def initialize(control, name, configuration={})
  super
  
  @collection = (configuration[:collection] || {})
  @resolver = configuration[:resolver]
  @resolver = @resolver.new if @resolver.is_a?(Class)
  @default = configuration[:default]
  
  configuration[:cache] = true if configuration[:cache].nil?
  
  if configuration[:cache]
    if resolver.respond_to?(:load_cache)
      resolver.load_cache
    else
      ETL::Engine.logger.info "#{resolver.class.name} does not support caching"
    end
  end
end

Instance Attribute Details

#defaultObject

The default foreign key to use if none is found.



9
10
11
# File 'lib/etl/transform/foreign_key_lookup_transform.rb', line 9

def default
  @default
end

#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



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/etl/transform/foreign_key_lookup_transform.rb', line 40

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)
    fk ||= @default
    raise ResolverError, "Unable to resolve #{value} to foreign key for #{name} in row #{ETL::Engine.rows_read}. You may want to specify a :default value." unless fk
    @collection[value] = fk
  end
  fk
end