Class: DataImport::Importer

Inherits:
Object
  • Object
show all
Defined in:
lib/data-import/importer.rb

Instance Method Summary collapse

Constructor Details

#initialize(context, definition) ⇒ Importer

Returns a new instance of Importer.



4
5
6
7
# File 'lib/data-import/importer.rb', line 4

def initialize(context, definition)
  @context = context
  @definition = definition
end

Instance Method Details

#runObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/data-import/importer.rb', line 9

def run
  @definition.target_database.transaction do
    options = {}
    options[:primary_key] = @definition.source_primary_key
    options[:columns] = @definition.source_columns
    options[:distinct] = @definition.source_distinct_columns
    options[:order] = @definition.source_order_columns
    @definition.source_database.each_row(@definition.source_table_name, options) do |row|
      @context.before_filter.call(row) if @context.before_filter
      import_row row
      yield if block_given?
    end
  end
  @definition.after_blocks.each do |block|
    @definition.instance_exec(@context, &block)
  end
end