class DataImporter::ImporterService

Attributes

klass[RW]
reader[RW]
report[RW]
verbose[RW]

Public Class Methods

new(file_name, klass, args={}) click to toggle source
# File lib/data_importer/importer_service.rb, line 5
def initialize(file_name, klass, args={})
  @klass = klass
  @report = Report.new
  @reader = CsvReader.new(file_name, args[:mappings])
  @verbose = args[:verbose]
end

Public Instance Methods

print_message(message) click to toggle source
run() click to toggle source
# File lib/data_importer/importer_service.rb, line 12
def run
  @report.time_elapsed = Benchmark.realtime do
    while row = @reader.get_mapped_row_hash
      new_entry = @klass.new(row)
      if new_entry.save
        @report.successful += 1
        print_message("Successful #{row.values.join(', ')}")
      else
        @report.unsuccessful += 1
        print_message("UnSuccessful #{row.values.join(', ')} Error: #{new_entry.errors.full_messages.first}")
      end
      @report.total = @report.successful + @report.unsuccessful
    end
  end
end