Class: ETL::Control::CsvDestination

Inherits:
Destination show all
Defined in:
lib/etl/control/destination/csv_destination.rb

Instance Attribute Summary collapse

Attributes inherited from Destination

#append_rows, #buffer_size, #condition, #configuration, #control, #mapping, #unique

Instance Method Summary collapse

Methods inherited from Destination

class_for_name, #current_row, #errors, #write

Constructor Details

#initialize(control, configuration, mapping = {}) ⇒ CsvDestination

Initialize the object.

  • control: The Control object

  • configuration: The configuration map

  • mapping: The output mapping

Configuration options:

  • <tt>:file<tt>: The file to write to (REQUIRED)

  • :append: Set to true to append to the file (default is to overwrite)

  • :headers: Set to true to add the headers to the output also (default is true)

Mapping options:

  • :order: The order array

Raises:



17
18
19
20
21
22
23
24
# File 'lib/etl/control/destination/csv_destination.rb', line 17

def initialize(control, configuration, mapping={})
  super
  @file = File.join(File.dirname(control.file), configuration[:file])
  @append = configuration[:append] ||= false
  @headers = configuration[:headers] ||= true
  @order = mapping[:order] || order_from_source
  raise ControlError, "Order required in mapping" unless @order
end

Instance Attribute Details

#appendObject (readonly)

Returns the value of attribute append.



4
5
6
# File 'lib/etl/control/destination/csv_destination.rb', line 4

def append
  @append
end

#fileObject (readonly)

Returns the value of attribute file.



4
5
6
# File 'lib/etl/control/destination/csv_destination.rb', line 4

def file
  @file
end

#headersObject (readonly)

Returns the value of attribute headers.



4
5
6
# File 'lib/etl/control/destination/csv_destination.rb', line 4

def headers
  @headers
end

#orderObject (readonly)

Returns the value of attribute order.



4
5
6
# File 'lib/etl/control/destination/csv_destination.rb', line 4

def order
  @order
end

Instance Method Details

#closeObject

Close the destination. This will flush the buffer and close the underlying stream or connection.



27
28
29
30
# File 'lib/etl/control/destination/csv_destination.rb', line 27

def close
  flush
  f.close
end

#flushObject

Flush the destination buffer



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/etl/control/destination/csv_destination.rb', line 33

def flush
  if write_header?
    f << order
  end

  #puts "Flushing buffer (#{file}) with #{buffer.length} rows"
  buffer.flatten.each do |row|
    # check to see if this row's compound key constraint already exists
    # note that the compound key constraint may not utilize virtual fields
    next unless row_allowed?(row)
    # add any virtual fields
    add_virtuals!(row)
    # collect all of the values using the order designated in the configuration
    values = order.collect do |name|
      value = row[name]
      case value
      when Date, Time, DateTime
        value.to_s(:db)
      else
        value
#              value.to_s
      end
    end
    # write the values
    f << values
  end
  f.flush
  buffer.clear
end