Class: Importu::DuplicateManager
- Inherits:
-
Object
- Object
- Importu::DuplicateManager
- Defined in:
- lib/importu/duplicate_manager.rb
Overview
The duplicate manager provides support for recording records and objects encountered during the import process. When records or objects have been encountered previously, a Importu::DuplicateRecord exception is raised.
Instance Method Summary collapse
-
#check_object!(unique_id) ⇒ void
Checks that the unique id of an object returned from the backend has not been encountered before.
-
#check_record!(record) ⇒ void
Checks that a conflicting record has not been encountered before.
-
#initialize(finder_fields: []) ⇒ Importu::DuplicateManager
constructor
Creates a new instance of the duplicate manager.
Constructor Details
#initialize(finder_fields: []) ⇒ Importu::DuplicateManager
Creates a new instance of the duplicate manager.
21 22 23 24 25 26 27 |
# File 'lib/importu/duplicate_manager.rb', line 21 def initialize(finder_fields: []) # Proc-based finder fields cannot be directly applied to records, as # it requires looking up the corresponding object using the backend. @finder_fields = finder_fields.reject {|fg| fg.respond_to?(:call) } @encountered = Set.new end |
Instance Method Details
#check_object!(unique_id) ⇒ void
This method returns an undefined value.
Checks that the unique id of an object returned from the backend has not been encountered before. Raises a DuplicateError exception if the object has been encountered before, otherwise the object is marked as seen.
44 45 46 47 48 49 |
# File 'lib/importu/duplicate_manager.rb', line 44 def check_object!(unique_id) return unless unique_id result = @encountered.add?(_object_unique_id: unique_id) duplicate_record! if result.nil? end |
#check_record!(record) ⇒ void
This method returns an undefined value.
Checks that a conflicting record has not been encountered before. Uses the configured finder_fields to construct sets of key/value pairs that are considered unique enough to look up objects from the backend. Marks all of the key/value pairs as encountered if not seen before. Raises a DuplicateError exception if any were previously encountered.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/importu/duplicate_manager.rb', line 66 def check_record!(record) results = @finder_fields.map do |field_group| conditions = field_group.to_h {|f| [f, record.fetch(f)] } @encountered.add?(conditions) ? :added : :duplicate rescue KeyError # Field group key not defined on record, always nil so invalid :skipped end duplicate_record! if results.include?(:duplicate) end |