Class: CSVImportable::CSVImporter

Inherits:
Object
  • Object
show all
Includes:
CSVCoercion
Defined in:
lib/csv_importable/csv_importer.rb

Defined Under Namespace

Modules: Statuses

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CSVCoercion

#boolean_type_class, #date_type_class, #float_type_class, #integer_type_class, #percent_type_class, #select_type_class, #string_type_class

Constructor Details

#initialize(args = {}) ⇒ CSVImporter

Returns a new instance of CSVImporter.



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

def initialize(args = {})
  @file_string = args[:file_string]
  import_id = args[:import_id]
  @importable_class = args[:importable_class]
  @big_file_threshold = args[:big_file_threshold]
  @import_obj = importable_class.find(import_id) if import_id
  # because we can't pass file_string to delayed job
  @file_string = @import_obj.read_file if @import_obj
  @should_replace = args.fetch(:should_replace, false)
  @row_importer_class = args[:row_importer_class]
  after_init(args)
  require_args
end

Instance Attribute Details

#big_file_thresholdObject (readonly)

Returns the value of attribute big_file_threshold.



6
7
8
# File 'lib/csv_importable/csv_importer.rb', line 6

def big_file_threshold
  @big_file_threshold
end

#file_stringObject (readonly)

Returns the value of attribute file_string.



6
7
8
# File 'lib/csv_importable/csv_importer.rb', line 6

def file_string
  @file_string
end

#import_objObject (readonly)

Returns the value of attribute import_obj.



6
7
8
# File 'lib/csv_importable/csv_importer.rb', line 6

def import_obj
  @import_obj
end

#importable_classObject (readonly)

Returns the value of attribute importable_class.



6
7
8
# File 'lib/csv_importable/csv_importer.rb', line 6

def importable_class
  @importable_class
end

#outObject (readonly)

Returns the value of attribute out.



6
7
8
# File 'lib/csv_importable/csv_importer.rb', line 6

def out
  @out
end

#resultsObject (readonly)

Returns the value of attribute results.



6
7
8
# File 'lib/csv_importable/csv_importer.rb', line 6

def results
  @results
end

#row_importer_classObject (readonly)

Returns the value of attribute row_importer_class.



6
7
8
# File 'lib/csv_importable/csv_importer.rb', line 6

def row_importer_class
  @row_importer_class
end

#should_replaceObject (readonly)

Returns the value of attribute should_replace.



6
7
8
# File 'lib/csv_importable/csv_importer.rb', line 6

def should_replace
  @should_replace
end

Instance Method Details

#big_file?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/csv_importable/csv_importer.rb', line 48

def big_file?
  parse_csv_string(file_string).count > big_file_threshold
end

#importObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/csv_importable/csv_importer.rb', line 33

def import
  @results = guard_against_errors do
    destroy_records if should_replace?
    print(starting_message)
    before_rows
    @results = parse_csv_string(file_string) do |row, headers|
      process_row(row, headers)
    end
    after_rows(@results.map { |result| result[:value] })
    print(finished_message)
    @results
  end
  @results
end

#number_importedObject



56
57
58
# File 'lib/csv_importable/csv_importer.rb', line 56

def number_imported
  results.fetch(:results).length
end

#succeeded?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/csv_importable/csv_importer.rb', line 52

def succeeded?
  results[:status] == Statuses::SUCCESS
end