Sha256: 5c1615177eba5af0aeb445e014afcc805ace7ec2bfd9e04637a494c21ae65930

Contents?: true

Size: 928 Bytes

Versions: 1

Compression:

Stored size: 928 Bytes

Contents

module DataImporter
  class ImporterService
    attr_accessor :reader, :report, :klass, :verbose

    def initialize(file_name, klass, args={})
      @klass = klass
      @report = Report.new
      @reader = CsvReader.new(file_name, args[:mappings])
      @verbose = args[:verbose]
    end

    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

    def print_message(message)
      puts message if verbose
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
data_importer-0.2.0 lib/data_importer/importer_service.rb