Class: ScriptImportManager

Inherits:
ImportManager show all
Defined in:
lib/import_manager/script_import_manager.rb

Overview

ImportManager for scripts. Main difference is you don’t have an act card and you can choose a error policy. For example throw an exception on the first error or collect all errors and report in the console at the end. You can also specify a user who does the imports. Unlike the other ImportManagers the ScriptImportManager import doesn’t support extra data to override fields.

Instance Attribute Summary

Attributes inherited from ImportManager

#conflict_strategy

Instance Method Summary collapse

Methods inherited from ImportManager

#add_card, #add_extra_data, #extra_data, #handle_import, #import, #import_card

Methods included from ImportManager::Conflicts

#check_for_duplicates, #duplicate, #handle_conflict, #override?, #with_conflict_resolution, #with_conflict_strategy

Methods included from ImportManager::StatusLog

#error_list, #errors, #errors?, #errors_by_row_index, #import_status, #pick_up_card_errors, #report, #report_error

Constructor Details

#initialize(csv_file, conflict_strategy: :skip, error_policy: :fail, user: nil) ⇒ ScriptImportManager

Returns a new instance of ScriptImportManager.



11
12
13
14
15
# File 'lib/import_manager/script_import_manager.rb', line 11

def initialize csv_file, conflict_strategy: :skip, error_policy: :fail, user: nil
  super(csv_file, conflict_strategy, {})
  @error_policy = error_policy
  @user = user
end

Instance Method Details

#import_rows(row_indices) ⇒ Object



17
18
19
20
21
# File 'lib/import_manager/script_import_manager.rb', line 17

def import_rows row_indices
  with_user do
    super
  end
end

#log_statusObject



44
45
46
47
# File 'lib/import_manager/script_import_manager.rb', line 44

def log_status
  puts "#{@current_row.row_index}:  #{@current_row.name}"
  super
end

#row_failed(_csv_row) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/import_manager/script_import_manager.rb', line 33

def row_failed _csv_row
  case @error_policy
  when :fail then
    raise ImportError, @import_status[:errors].inspect
  when :report then
    puts @import_status[:errors].inspect
  when :skip then
    nil
  end
end

#with_userObject



23
24
25
26
27
28
29
30
31
# File 'lib/import_manager/script_import_manager.rb', line 23

def with_user
  if @user
    Card::Auth.with(@user) { yield }
  elsif Card::Auth.signed_in?
    yield
  else
    raise StandardError, "can't import as anonymous"
  end
end