Class: DataSeeder::Loader::CSV

Inherits:
Object
  • Object
show all
Includes:
DataSeeder::Loader
Defined in:
lib/data_seeder/loader/csv.rb

Instance Attribute Summary collapse

Attributes included from DataSeeder::Loader

#config, #key_attribute, #klass, #logger, #path, #path_minus_ext

Instance Method Summary collapse

Methods included from DataSeeder::Loader

#call_config, #call_method, #default_config, #destroy_model, #destroy_models, #initialize, #log_destroy, #log_indent, #log_save, #log_update, #model_info, #process, #save, #save_model, #setup, #teardown

Instance Attribute Details

#line_numberObject (readonly)

Returns the value of attribute line_number.



8
9
10
# File 'lib/data_seeder/loader/csv.rb', line 8

def line_number
  @line_number
end

Instance Method Details

#load(io) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/data_seeder/loader/csv.rb', line 10

def load(io)
  @line_number = 0
  csv = ::CSV.new(io, headers: true)
  csv.each do |row|
    begin
      @line_number += 1
      save(row.to_hash)
    rescue Exception => e
      # TODO: Consider counting the header in the line_number count, but anyone using
      # config[:use_line_number_as_id] would have all there id's incremented
      logger.error "Exception at line #{@line_number+1}: #{e.message}"
      raise unless config[:continue_on_exception]
    end
  end
end