class Topographer::Importer
Attributes
fatal_errors[R]
logger[R]
Public Class Methods
build_mapper(model_class, &mapper_definition)
click to toggle source
# 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 = {})
click to toggle source
# 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
new(input, mapping_generator, strategy, logger, options = {})
click to toggle source
@param mapping_generator [#get_mapper] the object responsible for deciding which mapping to use for the strategy @param strategy either a Class that inherits from Topographer::Importer::Strategy::Base
or an instance of a strategy
# File lib/topographer/importer.rb, line 22 def initialize(input, mapping_generator, strategy, logger, options = {}) @logger = logger @fatal_errors = [] dry_run = options.fetch(:dry_run, false) ignore_unmapped_columns = options.fetch(:ignore_unmapped_columns, false) strategy_class = strategy.is_a?(Class) ? strategy : strategy.class mapper = mapping_generator.get_mapper(strategy_class) if importable?(input, mapper, ignore_unmapped_columns) strategy = setup_strategy(mapper, strategy, strategy_class) strategy.dry_run = dry_run import_data(strategy, input, mapper.model_class.name) else log_fatal_errors(input) end end
Public Instance Methods
import_data(strategy, input, import_class)
click to toggle source
# File lib/topographer/importer.rb, line 51 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
setup_strategy(mapper, strategy, strategy_class)
click to toggle source
# File lib/topographer/importer.rb, line 42 def setup_strategy(mapper, strategy, strategy_class) if strategy == strategy_class strategy_class.new(mapper) # supports legacy code else strategy.mapper = mapper strategy end end
Private Instance Methods
importable?(input, mapper, ignore_unmapped_columns)
click to toggle source
# File lib/topographer/importer.rb, line 78 def importable?(input, mapper, ignore_unmapped_columns) valid_header?(input, mapper, ignore_unmapped_columns) && input_ready?(input) end
input_ready?(input)
click to toggle source
# File lib/topographer/importer.rb, line 82 def input_ready?(input) fatal_errors << input.failure_message unless input.importable? input.importable? end
invalid_header_message(mapper, ignore_unmapped_columns = false)
click to toggle source
# File lib/topographer/importer.rb, line 67 def invalid_header_message(mapper, ignore_unmapped_columns = false) error = 'Invalid Input Header -' if mapper.missing_columns.any? error << " Missing Columns: #{mapper.missing_columns.join(', ')}" end if mapper.bad_columns.any? && !ignore_unmapped_columns error << " Invalid Columns: #{mapper.bad_columns.join(', ')}" end error end
log_fatal_errors(input)
click to toggle source
# File lib/topographer/importer.rb, line 61 def log_fatal_errors(input) fatal_errors.each do |fatal_error_message| @logger.log_fatal input.input_identifier, fatal_error_message end end
valid_header?(input, mapper, ignore_unmapped_columns)
click to toggle source
# File lib/topographer/importer.rb, line 88 def valid_header?(input, mapper, ignore_unmapped_columns) valid = mapper.input_structure_valid?(input.get_header, ignore_unmapped_columns: ignore_unmapped_columns) fatal_errors << invalid_header_message(mapper, ignore_unmapped_columns) unless valid valid end