Sha256: 08d2037369b5aece41b8ffb7e5fc0bcfde864d86048c0a03e6308dc7ef09c72e

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

module ETL #:nodoc:
  module Control #:nodoc:
    # File as the final destination.
    class FileDestination < Destination
      attr_reader :file, :order
      attr_accessor :append, :separator, :eol, :enclose
      
      # Initialize the object.
      # * <tt>control</tt>: The Control object
      # * <tt>configuration</tt>: The configuration map
      # * <tt>mapping</tt>: The output mapping
      def initialize(control, configuration, mapping)
        super
        @file = File.join(File.dirname(control.file), configuration[:file])
        @append = configuration[:append] ||= false
        @separator = configuration[:separator] ||= ','
        @eol = configuration[:eol] ||= "\n"
        @enclose = configuration[:enclose] ||= nil
        
        @order = mapping[:order] || order_from_source
        raise ControlError, "Order required in mapping" unless @order
      end
      
      # Close the destination. This will flush the buffer and close the underlying stream or connection.
      def close
        flush
        f.close
      end
      
      def flush
        buffer.each do |row|
          add_virtuals(row)
          values = order.collect { |name| row[name] }
          if !enclose.nil?
            values.collect! { |v| enclose + v.to_s.gsub(/(#{enclose})/, '\\\\\1') + enclose }
          end
          f.write(values.join(separator))
          f.write(eol)
        end
        buffer.clear
      end
      
      private
      # Get the open file stream
      def f
        @f ||= open(file, mode)
      end
      
      # Get the appropriate mode to open the file stream
      def mode
        append ? 'a' : 'w'
      end
      
      def add_virtuals(row)
        if mapping[:virtual]
          mapping[:virtual].each do |key,value|
            row[key] = value
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activewarehouse-etl-0.1.0 lib/etl/control/destination/file_destination.rb