Class: Sp2db::ImportStrategy::Base
- Inherits:
-
Object
- Object
- Sp2db::ImportStrategy::Base
- Includes:
- Logging
- Defined in:
- lib/sp2db/import_strategy.rb
Instance Attribute Summary collapse
-
#result ⇒ Object
Returns the value of attribute result.
-
#rows ⇒ Object
Returns the value of attribute rows.
-
#table ⇒ Object
Returns the value of attribute table.
Attributes included from Logging
Instance Method Summary collapse
- #after_import ⇒ Object
- #before_import ⇒ Object
- #errors ⇒ Object
- #find_db_row(row) ⇒ Object
- #import ⇒ Object
- #import_row(row) ⇒ Object
-
#initialize(table, rows) ⇒ Base
constructor
A new instance of Base.
- #records ⇒ Object
- #set_record_value(record, row) ⇒ Object
Methods included from Logging
Constructor Details
#initialize(table, rows) ⇒ Base
Returns a new instance of Base.
41 42 43 44 |
# File 'lib/sp2db/import_strategy.rb', line 41 def initialize table, rows self.table = table self.rows = rows end |
Instance Attribute Details
#result ⇒ Object
Returns the value of attribute result.
35 36 37 |
# File 'lib/sp2db/import_strategy.rb', line 35 def result @result end |
#rows ⇒ Object
Returns the value of attribute rows.
35 36 37 |
# File 'lib/sp2db/import_strategy.rb', line 35 def rows @rows end |
#table ⇒ Object
Returns the value of attribute table.
35 36 37 |
# File 'lib/sp2db/import_strategy.rb', line 35 def table @table end |
Instance Method Details
#after_import ⇒ Object
92 93 94 |
# File 'lib/sp2db/import_strategy.rb', line 92 def after_import logger.debug "Run after import table: #{self.table.name}" end |
#before_import ⇒ Object
61 62 63 |
# File 'lib/sp2db/import_strategy.rb', line 61 def before_import logger.debug "Run before import table: #{self.table.name}" end |
#errors ⇒ Object
53 54 55 |
# File 'lib/sp2db/import_strategy.rb', line 53 def errors result[:errors] end |
#find_db_row(row) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/sp2db/import_strategy.rb', line 65 def find_db_row row if find_columns.present? cond = {} find_columns.each do |col| cond[col] = row[col] end model.find_by cond else nil # nil to skip end end |
#import ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/sp2db/import_strategy.rb', line 96 def import logger.debug "Start import table: #{self.table.name}" ActiveRecord::Base.transaction(requires_new: true) do before_import rows.each do |row| row = row.clone begin table.before_import_row row record = import_row row records << record table.after_import_row record rescue ActiveRecord::ActiveRecordError => e logger.error e.try(:message) errors << { message: e.try(:message), exception: e, row: row, table: table.name, } next unless ExceptionHandler.row_import_error e end end after_import table.after_import_table result logger.debug "Import finished: #{self.table.name}" return result end end |
#import_row(row) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/sp2db/import_strategy.rb', line 84 def import_row row record = find_db_row(row) || model.new(row) record = set_record_value record, row return unless record.present? record.save! if record.new_record? || record.changed? record end |
#records ⇒ Object
57 58 59 |
# File 'lib/sp2db/import_strategy.rb', line 57 def records result[:records] end |
#set_record_value(record, row) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/sp2db/import_strategy.rb', line 77 def set_record_value record, row row.each do |k, v| record.send("#{k}=", v) if record.respond_to?("#{k}=") end record end |