Class: Topographer::Importer

Inherits:
Object
  • Object
show all
Defined in:
lib/topographer/importer.rb

Defined Under Namespace

Modules: Helpers, Importable, Strategy Classes: Input, Logger, Mapper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, import_class, strategy_class, logger, options = {}) ⇒ Importer

Returns a new instance of Importer.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/topographer/importer.rb', line 20

def initialize(input, import_class, strategy_class, logger, options = {})
  @logger = logger
  @fatal_errors = []

  dry_run = options.fetch(:dry_run, false)
  ignore_unmapped_columns = options.fetch(:ignore_unmapped_columns, false)

  mapper = import_class.get_mapper(strategy_class)

  if importable?(input, mapper, ignore_unmapped_columns)
    strategy = strategy_class.new(mapper)
    strategy.dry_run = dry_run
    import_data(strategy, input, mapper.model_class.name)
  else
    log_fatal_errors(input)
  end
end

Instance Attribute Details

#fatal_errorsObject (readonly)

Returns the value of attribute fatal_errors.



9
10
11
# File 'lib/topographer/importer.rb', line 9

def fatal_errors
  @fatal_errors
end

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/topographer/importer.rb', line 9

def logger
  @logger
end

Class Method Details

.build_mapper(model_class, &mapper_definition) ⇒ Object



11
12
13
# File 'lib/topographer/importer.rb', line 11

def self.build_mapper(model_class, &mapper_definition)
  Mapper.build_mapper(model_class, &mapper_definition)
end

.import_data(input, import_class, strategy_class, logger, options = {}) ⇒ Object



15
16
17
18
# File 'lib/topographer/importer.rb', line 15

def self.import_data(input, import_class, strategy_class, logger, options = {})
  importer = new(input, import_class, strategy_class, logger, options)
  importer.logger
end

Instance Method Details

#import_data(strategy, input, import_class) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/topographer/importer.rb', line 39

def import_data(strategy, input, import_class)
  input.each do |data|
    status = strategy.import_record(data)
    log_entry = Logger::LogEntry.new(input.input_identifier, import_class, status)
    @logger.log_import(log_entry)
  end
end