Class: Importu::Importer::Csv

Inherits:
Importu::Importer show all
Defined in:
lib/importu/importer/csv.rb

Instance Attribute Summary

Attributes inherited from Importu::Importer

#created, #infile, #invalid, #options, #outfile, #total, #unchanged, #updated, #validation_errors

Instance Method Summary collapse

Methods inherited from Importu::Importer

#import!, #result_msg

Constructor Details

#initialize(infile, options = {}) ⇒ Csv

Returns a new instance of Csv.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/importu/importer/csv.rb', line 4

def initialize(infile, options = {})
  super

  @csv_options = {
    :headers        => true,
    :return_headers => true,
    :write_headers  => true,
    :skip_blanks    => true,
  }.merge(options[:csv_options]||{})

  @reader = ::CSV.new(@infile, @csv_options)
  @header = @reader.readline
  @data_pos = @infile.pos
end

Instance Method Details

#import_record(record, finder_scope, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/importu/importer/csv.rb', line 28

def import_record(record, finder_scope, &block)
  begin
    super
  rescue Importu::MissingField => e
    # if one record missing field, all are, major error
    raise Importu::InvalidInput, "missing required field: #{e.message}"
  rescue Importu::InvalidRecord => e
    write_error(record.raw_data, e.message)
  end
end

#recordsObject



19
20
21
22
23
24
25
26
# File 'lib/importu/importer/csv.rb', line 19

def records
  @infile.pos = @data_pos
  Enumerator.new do |yielder|
    @reader.each do |row|
      yielder.yield record_class.new(self, row.to_hash, row)
    end
  end
end