Class: Chronicle::ETL::Transformer

Inherits:
Object
  • Object
show all
Extended by:
Registry::SelfRegistering
Includes:
Configurable
Defined in:
lib/chronicle/etl/transformers/transformer.rb

Overview

Abstract class representing an Transformer for an ETL job

Instance Attribute Summary collapse

Attributes included from Registry::SelfRegistering

#connector_registration

Instance Method Summary collapse

Methods included from Registry::SelfRegistering

register_connector

Methods included from Configurable

included

Constructor Details

#initialize(options = {}) ⇒ Transformer

Construct a new instance of this transformer. Options are passed in from a Runner == Parameters: options:: Options for configuring this Transformer



16
17
18
# File 'lib/chronicle/etl/transformers/transformer.rb', line 16

def initialize(options = {})
  apply_options(options)
end

Instance Attribute Details

#stashed_recordsObject (readonly)

Returns the value of attribute stashed_records.



10
11
12
# File 'lib/chronicle/etl/transformers/transformer.rb', line 10

def stashed_records
  @stashed_records
end

Instance Method Details

#call(record, &block) ⇒ Object

Called once for each extracted record. Can return 0 or more transformed records.

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chronicle/etl/transformers/transformer.rb', line 21

def call(record, &block)
  raise ArgumentError, 'Input must be a Chronicle::ETL::Record' unless record.is_a?(Record)

  yielded = false

  transformed_data = transform(record) do |data|
    new_record = update_data(record, data)
    block.call(new_record)

    yielded = true
  end

  return if yielded

  # Handle transformers that don't yield anything and return
  # transformed data directly. Skip nil values.
  [transformed_data].flatten.compact.each do |data|
    new_record = update_data(record, data)
    block.call(new_record)
  end
end

#call_finish(&block) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/chronicle/etl/transformers/transformer.rb', line 43

def call_finish(&block)
  remaining_records = finish
  return if remaining_records.nil?

  remaining_records.each do |record|
    block.call(record)
  end
end

#finishObject

Called once after runner has processed all records



57
# File 'lib/chronicle/etl/transformers/transformer.rb', line 57

def finish; end

#transform(_record) ⇒ Object

Raises:

  • (NotImplementedError)


52
53
54
# File 'lib/chronicle/etl/transformers/transformer.rb', line 52

def transform(_record)
  raise NotImplementedError, 'You must implement the transform method'
end