lib/marty/data_importer.rb in marty-2.1.5 vs lib/marty/data_importer.rb in marty-2.3.0

- old
+ new

@@ -1,19 +1,18 @@ module Marty - class DataImporterError < StandardError - attr_reader :lines + class DataImporter + class Error < StandardError + attr_reader :lines - def initialize(message, lines) - msg = lines && lines.respond_to?(:join) ? - "#{message} - lines: #{lines.join(',')}" : message + def initialize(message, lines) + msg = lines && lines.respond_to?(:join) ? + "#{message} - lines: #{lines.join(',')}" : message - super(msg) - @lines = lines + super(msg) + @lines = lines + end end - end - - class DataImporter # perform cleaning and do_import and summarize its results def self.do_import_summary(klass, data, dt = 'infinity', cleaner_function = nil, @@ -79,18 +78,18 @@ Marty::DataConversion.create_or_update(klass, row, dt) end rescue => exc # to find problems with the importer, comment out the rescue block - raise Marty::DataImporterError.new(exc.to_s, [eline]) + raise Error.new(exc.to_s, [eline]) end ids = {} # raise an error if record referenced more than once. res.each_with_index do |(op, id), line| - raise Marty::DataImporterError. + raise Error. new("record referenced more than once", [ids[id], line]) if op != :blank && ids.member?(id) && !allow_dups ids[id] = line end @@ -98,15 +97,15 @@ begin # Validate affected rows if necessary klass.send(validation_function.to_sym, ids.keys) if validation_function rescue => exc - raise Marty::DataImporterError.new(exc.to_s, []) + raise Error.new(exc.to_s, []) end remainder_ids = cleaner_ids - ids.keys - raise Marty::DataImporterError. + raise Error. new("Missing import data. " + "Please provide header line and at least one data line.", [1]) if ids.keys.compact.count == 0 klass.delete(remainder_ids)