Sha256: c5e3088107ff5883e438d8473b4ccef423c42130d2f48041afd915d072829ac1

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 KB

Contents

module ETL #:nodoc:
  module Transform #:nodoc:
    # Transform which decodes coded values
    class DecodeTransform < ETL::Transform::Transform
      attr_accessor :decode_table_path
      
      attr_accessor :decode_table_delimiter
      
      attr_accessor :default_value
      
      # Initialize the transformer
      #
      # Configuration options:
      # * <tt>:decode_table_path</tt>: The path to the decode table (defaults to 'decode.txt')
      # * <tt>:decode_table_delimiter</tt>: The decode table delimiter (defaults to ':')
      # * <tt>:default_value</tt>: The default value to use (defaults to 'No Value')
      def initialize(control, 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
      
      # Transform the value
      def transform(value)
        decode_table[value] || default_value
      end
      
      # Get the decode table
      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
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activewarehouse-etl-0.5.0 lib/etl/transform/decode_transform.rb
activewarehouse-etl-0.5.1 lib/etl/transform/decode_transform.rb
activewarehouse-etl-0.5.2 lib/etl/transform/decode_transform.rb