Class: ImportManager
- Inherits:
-
Object
- Object
- ImportManager
- Defined in:
- lib/import_manager.rb,
lib/import_manager/status.rb,
lib/import_manager/conflicts.rb,
lib/import_manager/status_log.rb,
lib/import_manager/status/counts.rb
Overview
ImportManager coordinates the import of a CSVFile. It defines the conflict and error policy. It collects all errors and provides extra data like corrections for row fields.
Direct Known Subclasses
Defined Under Namespace
Modules: Conflicts, StatusLog Classes: Status
Instance Attribute Summary collapse
-
#conflict_strategy ⇒ Object
readonly
Returns the value of attribute conflict_strategy.
Instance Method Summary collapse
-
#add_card(args) ⇒ Object
used by csv rows to add additional cards.
- #add_extra_data(index, data) ⇒ Object
- #extra_data(index) ⇒ Object
- #handle_import(row) ⇒ Object
- #import(row_indices = nil) ⇒ Object
-
#import_card(card_args) ⇒ Object
add the final import card.
- #import_rows(row_indices) ⇒ Object
-
#initialize(csv_file, conflict_strategy = :skip, extra_data = {}) ⇒ ImportManager
constructor
A new instance of ImportManager.
Methods included from Conflicts
#check_for_duplicates, #duplicate, #handle_conflict, #override?, #with_conflict_resolution, #with_conflict_strategy
Methods included from StatusLog
#error_list, #errors, #errors?, #errors_by_row_index, #import_status, #log_status, #pick_up_card_errors, #report, #report_error
Constructor Details
#initialize(csv_file, conflict_strategy = :skip, extra_data = {}) ⇒ ImportManager
Returns a new instance of ImportManager.
10 11 12 13 14 15 16 17 18 |
# File 'lib/import_manager.rb', line 10 def initialize csv_file, conflict_strategy=:skip, extra_data={} @csv_file = csv_file @conflict_strategy = conflict_strategy @extra_data = integerfy_keys(extra_data || {}) @extra_data[:all] ||= {} # init_import_status @imported_keys = ::Set.new end |
Instance Attribute Details
#conflict_strategy ⇒ Object (readonly)
Returns the value of attribute conflict_strategy.
8 9 10 |
# File 'lib/import_manager.rb', line 8 def conflict_strategy @conflict_strategy end |
Instance Method Details
#add_card(args) ⇒ Object
used by csv rows to add additional cards
44 45 46 47 48 |
# File 'lib/import_manager.rb', line 44 def add_card args pick_up_card_errors do Card.create args end end |
#add_extra_data(index, data) ⇒ Object
50 51 52 |
# File 'lib/import_manager.rb', line 50 def add_extra_data index, data @extra_data[index].deep_merge! data end |
#extra_data(index) ⇒ Object
30 31 32 |
# File 'lib/import_manager.rb', line 30 def extra_data index (@extra_data[:all] || {}).deep_merge(@extra_data[index] || {}) end |
#handle_import(row) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/import_manager.rb', line 34 def handle_import row @current_row = row status = catch(:skip_row) { yield } status = specify_success_status status @current_row.status = status log_status run_hook status end |
#import(row_indices = nil) ⇒ Object
20 21 22 |
# File 'lib/import_manager.rb', line 20 def import row_indices=nil import_rows row_indices end |
#import_card(card_args) ⇒ Object
add the final import card
55 56 57 58 59 |
# File 'lib/import_manager.rb', line 55 def import_card card_args @current_row.name = card_args[:name] check_for_duplicates card_args[:name] add_card card_args end |
#import_rows(row_indices) ⇒ Object
24 25 26 27 28 |
# File 'lib/import_manager.rb', line 24 def import_rows row_indices row_count = row_indices ? row_indices.size : @csv_file&.row_count init_import_status row_count @csv_file.each_row self, row_indices, &:execute_import end |