Class: ETL::Control::ExcelDestination

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

Overview

Excel as the final destination.

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 = {}) ⇒ ExcelDestination

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)

  • :unique: Set to true to only write unique records

  • :append_rows: Array of rows to append

Mapping options:

  • :order: The order array

Raises:



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/etl/control/destination/excel_destination.rb', line 29

def initialize(control, configuration, mapping={})
  super
  path = Pathname.new(configuration[:file])
  @file = path.absolute? ? path : Pathname.new(File.dirname(File.expand_path(control.file))) + path
  @append = configuration[:append] ||= false
  @unique = configuration[:unique] ? configuration[:unique] + scd_required_fields : configuration[:unique]
  @unique.uniq! unless @unique.nil?
  @order = mapping[:order] ? mapping[:order] + scd_required_fields : order_from_source
  @order.uniq! unless @order.nil?
  raise ControlError, "Order required in mapping" unless @order
end

Instance Attribute Details

#appendObject

Flag which indicates to append (default is to overwrite)



14
15
16
# File 'lib/etl/control/destination/excel_destination.rb', line 14

def append
  @append
end

#fileObject (readonly)

The File to write to



8
9
10
# File 'lib/etl/control/destination/excel_destination.rb', line 8

def file
  @file
end

#orderObject (readonly)

The output order



11
12
13
# File 'lib/etl/control/destination/excel_destination.rb', line 11

def order
  @order
end

Instance Method Details

#closeObject

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



42
43
44
45
46
# File 'lib/etl/control/destination/excel_destination.rb', line 42

def close
  buffer << append_rows if append_rows
  flush
  book.write(file)
end

#flushObject

Flush the destination buffer



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/etl/control/destination/excel_destination.rb', line 49

def flush
  #puts "Flushing buffer (#{file}) with #{buffer.length} rows"
  buffer.flatten.each_with_index do |row, index|
    #puts "row change type: #{row.change_type}"
    # 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.to_s
      end
    end
    
    # write the values
    sheet.insert_row(index, values)
  end
  buffer.clear
  #puts "After flush there are #{buffer.length} rows"
end