Class: SpeakyCsv::ActiveRecordImport

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/speaky_csv/active_record_import.rb

Overview

Imports a csv file as unsaved active record instances

Constant Summary collapse

QUERY_BATCH_SIZE =
20
TRUE_VALUES =
ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, input_io_or_enumerable, klass) ⇒ ActiveRecordImport

Returns a new instance of ActiveRecordImport.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/speaky_csv/active_record_import.rb', line 14

def initialize(config, input_io_or_enumerable, klass)
  @config = config
  @klass = klass

  @log_output = StringIO.new
  @logger = Logger.new @log_output

  if duck_type_is_io?(input_io_or_enumerable)
    @rx = AttrImport.new @config, input_io_or_enumerable
    @rx.logger = @logger
  else
    @rx = input_io_or_enumerable
  end
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



12
13
14
# File 'lib/speaky_csv/active_record_import.rb', line 12

def logger
  @logger
end

Instance Method Details

#eachObject



29
30
31
# File 'lib/speaky_csv/active_record_import.rb', line 29

def each
  block_given? ? enumerator.each { |a| yield a } : enumerator
end

#eager_load(options) ⇒ Object

Add eager_load options which will be used when querying records.



48
49
50
# File 'lib/speaky_csv/active_record_import.rb', line 48

def eager_load(options)
  @eager_load = options
end

#includes(options) ⇒ Object

Add includes options which will be used when querying records.

Useful to avoid N+1 type problems. Configured has_manys are automaticaly included and don’t need to be specified here.



43
44
45
# File 'lib/speaky_csv/active_record_import.rb', line 43

def includes(options)
  @includes = options
end

#logObject

Returns a string of all the log output from the import. Or returns nothing if a custom logger was used.



35
36
37
# File 'lib/speaky_csv/active_record_import.rb', line 35

def log
  @log_output.string
end