Class: ETL::Control::YamlDestination

Inherits:
Destination show all
Defined in:
lib/etl/control/destination/yaml_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

- (YamlDestination) initialize(control, configuration, mapping = {})

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)

  • :only

  • :except

Raises:



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

def initialize(control, configuration, mapping={})
  super
  @file = File.join(File.dirname(control.file), configuration[:file])
  @append = configuration[:append] ||= false
  @only = configuration[:only]
  @except = configuration[:except]
  raise ControlError, "the :only and :except options must be used seperately, do not specify both" if @only && @except
end

Instance Attribute Details

- (Object) append (readonly)

Returns the value of attribute append



6
7
8
# File 'lib/etl/control/destination/yaml_destination.rb', line 6

def append
  @append
end

- (Object) except (readonly)

Returns the value of attribute except



6
7
8
# File 'lib/etl/control/destination/yaml_destination.rb', line 6

def except
  @except
end

- (Object) file (readonly)

Returns the value of attribute file



6
7
8
# File 'lib/etl/control/destination/yaml_destination.rb', line 6

def file
  @file
end

- (Object) only (readonly)

Returns the value of attribute only



6
7
8
# File 'lib/etl/control/destination/yaml_destination.rb', line 6

def only
  @only
end

Instance Method Details

- (Object) close

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



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

def close
  flush
  f.close
end

- (Object) flush

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
# File 'lib/etl/control/destination/yaml_destination.rb', line 33

def flush
  #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)

    yaml = {}
    row.each do |key, value|
      next if only && !only.include?(key)
      next if except && except.include?(key)

      case value
      when Date, Time, DateTime
        value = value.to_s(:db)
      end

      yaml[key] = value
    end
   
    # write the values
    YAML.dump(yaml, f)
  end
  f.flush
  buffer.clear
end