Class: Importu::DuplicateManager

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(finder_fields: []) ⇒ Importu::DuplicateManager

Creates a new instance of the duplicate manager.

Examples:

manager = Importu::DuplicateManager.new

Parameters:

  • finder_fields (Array<Array<Symbol>>) (defaults to: [])

    A list of finder field groups that should be used when checking if records are duplicates.



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.

Examples:

manager.check_object!(71)
manager.check_object!("0aefe55a-58bb-4a16-b873-ba3425e443bb")
manager.check_object!(71) # raises Importu::DuplicateManager

Parameters:

  • unique_id (#eql, #hash)

    A unique object identifier that can be compared against other object identifiers.

Raises:



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.

Examples:

manager.check_record!(record)
manager.check_record!(record) # raises Importu::DuplicateRecord

Parameters:

Raises:



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