Class: CSVImportable::CSVImporter
- Inherits:
-
Object
- Object
- CSVImportable::CSVImporter
- Includes:
- CSVCoercion
- Defined in:
- lib/csv_importable/csv_importer.rb
Defined Under Namespace
Modules: Statuses
Instance Attribute Summary collapse
-
#big_file_threshold ⇒ Object
readonly
Returns the value of attribute big_file_threshold.
-
#file_string ⇒ Object
readonly
Returns the value of attribute file_string.
-
#import_obj ⇒ Object
readonly
Returns the value of attribute import_obj.
-
#importable_class ⇒ Object
readonly
Returns the value of attribute importable_class.
-
#out ⇒ Object
readonly
Returns the value of attribute out.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
-
#row_importer_class ⇒ Object
readonly
Returns the value of attribute row_importer_class.
-
#should_replace ⇒ Object
readonly
Returns the value of attribute should_replace.
Instance Method Summary collapse
- #big_file? ⇒ Boolean
- #import ⇒ Object
-
#initialize(args = {}) ⇒ CSVImporter
constructor
A new instance of CSVImporter.
- #number_imported ⇒ Object
- #succeeded? ⇒ Boolean
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_threshold ⇒ Object (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_string ⇒ Object (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_obj ⇒ Object (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_class ⇒ Object (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 |
#out ⇒ Object (readonly)
Returns the value of attribute out.
6 7 8 |
# File 'lib/csv_importable/csv_importer.rb', line 6 def out @out end |
#results ⇒ Object (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_class ⇒ Object (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_replace ⇒ Object (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
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 |
#import ⇒ Object
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() before_rows @results = parse_csv_string(file_string) do |row, headers| process_row(row, headers) end after_rows(@results.map { |result| result[:value] }) print() @results end @results end |
#number_imported ⇒ Object
56 57 58 |
# File 'lib/csv_importable/csv_importer.rb', line 56 def number_imported results.fetch(:results).length end |