Class: ETL::Control::CsvDestination
- Inherits:
-
Destination
- Object
- Destination
- ETL::Control::CsvDestination
- Defined in:
- lib/etl/control/destination/csv_destination.rb
Instance Attribute Summary collapse
-
#append ⇒ Object
readonly
Returns the value of attribute append.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
Attributes inherited from Destination
#append_rows, #buffer_size, #condition, #configuration, #control, #mapping, #unique
Instance Method Summary collapse
-
#close ⇒ Object
Close the destination.
-
#flush ⇒ Object
Flush the destination buffer.
-
#initialize(control, configuration, mapping = {}) ⇒ CsvDestination
constructor
Initialize the object.
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
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
#append ⇒ Object (readonly)
Returns the value of attribute append.
4 5 6 |
# File 'lib/etl/control/destination/csv_destination.rb', line 4 def append @append end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
4 5 6 |
# File 'lib/etl/control/destination/csv_destination.rb', line 4 def file @file end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
4 5 6 |
# File 'lib/etl/control/destination/csv_destination.rb', line 4 def headers @headers end |
#order ⇒ Object (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
#close ⇒ Object
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 |
#flush ⇒ Object
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 |