Class: Impex::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/impex/engine.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Engine

Returns a new instance of Engine.



3
4
# File 'lib/impex/engine.rb', line 3

def initialize(options = {})
end

Class Method Details

.run(options = {}, &block) ⇒ Object



45
46
47
# File 'lib/impex/engine.rb', line 45

def run(options = {}, &block)
  Impex::Engine.new(options).run(&block)
end

Instance Method Details

#run(&block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/impex/engine.rb', line 6

def run(&block)
  @config = Impex.config

  @lookup = Impex::Lookup.new(@config)

  @files_loader    = @lookup.file_loader
  @history_manager = @lookup.history_manager

  @files = @files_loader.load

  @files.each do |file|
    model = file.table

    file.each do |row|
      # the user can re-organize each row before saving.
      row = block.call(row) if block

      row = @history_manager.filter_data_with_history(row)

      reference_column = @config[:history_references][row.table.to_sym] || "reference"
      reference = row.columns[reference_column.to_s]

      record = model.find_or_initialize_by(reference_column => reference)

      insert_row(record, row.columns.except([reference_column]))

      @history_manager.update_history(row)
    end
  end
end