Class: ETL::Transform::DecodeTransform

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

Overview

Transform which decodes coded values

Instance Attribute Summary (collapse)

Attributes inherited from Transform

#configuration, #control, #name

Instance Method Summary (collapse)

Methods inherited from Transform

benchmarks, transform

Constructor Details

- (DecodeTransform) initialize(control, name, configuration = {})

Initialize the transformer

Configuration options:

  • :decode_table_path: The path to the decode table (defaults to 'decode.txt')

  • :decode_table_delimiter: The decode table delimiter (defaults to ':')

  • :default_value: The default value to use (defaults to 'No Value')



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/etl/transform/decode_transform.rb', line 17

def initialize(control, name, configuration={})
  super
  
  if configuration[:decode_table_path]
    configuration[:decode_table_path] = File.join(File.dirname(control.file), configuration[:decode_table_path])
  end
  
  @decode_table_path = (configuration[:decode_table_path] || 'decode.txt')
  @decode_table_delimiter = (configuration[:decode_table_delimiter] || ':')
  @default_value = (configuration[:default_value] || 'No Value')
end

Instance Attribute Details

- (Object) decode_table_delimiter

Returns the value of attribute decode_table_delimiter



7
8
9
# File 'lib/etl/transform/decode_transform.rb', line 7

def decode_table_delimiter
  @decode_table_delimiter
end

- (Object) decode_table_path

Returns the value of attribute decode_table_path



5
6
7
# File 'lib/etl/transform/decode_transform.rb', line 5

def decode_table_path
  @decode_table_path
end

- (Object) default_value

Returns the value of attribute default_value



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

def default_value
  @default_value
end

Instance Method Details

- (Object) decode_table

Get the decode table



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/etl/transform/decode_transform.rb', line 35

def decode_table
  unless @decode_table
    @decode_table = {}
    open(decode_table_path).each do |line|
      code, value = line.strip.split(decode_table_delimiter)
      if code && code.length > 0
        @decode_table[code] = value
      else
        @default_value = value
      end
    end
  end
  @decode_table
end

- (Object) transform(name, value, row)

Transform the value



30
31
32
# File 'lib/etl/transform/decode_transform.rb', line 30

def transform(name, value, row)
  decode_table[value] || default_value
end