Class: DataForge::Transformation::RubyTransformationContext

Inherits:
Object
  • Object
show all
Defined in:
lib/data_forge/transformation/ruby_transformation_context.rb

Instance Method Summary collapse

Constructor Details

#initialize(writers) ⇒ RubyTransformationContext

Returns a new instance of RubyTransformationContext.



5
6
7
8
9
# File 'lib/data_forge/transformation/ruby_transformation_context.rb', line 5

def initialize(writers)
  @_writer_names = writers.map { |writer| writer.name }
  @_writers_hash = Hash[@_writer_names.zip writers]
  @_default_writer = writers.first
end

Instance Method Details

#output(record, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/data_forge/transformation/ruby_transformation_context.rb', line 13

def output(record, options = {})
  if options.has_key? :to
    Array(options[:to]).each do |target_writer_name|
      raise "Unknown target file '#{target_writer_name}' for `output` command" unless @_writer_names.include? target_writer_name
      @_writers_hash[target_writer_name].write record
    end
  else
    raise "Missing :to directive for `output` command in multiple file transformation" if @_writers_hash.count > 1
    @_default_writer.write record
  end
end