Class: AbstractImporter::Strategies::DefaultStrategy

Inherits:
Base
  • Object
show all
Defined in:
lib/abstract_importer/strategies/default_strategy.rb

Direct Known Subclasses

ReplaceStrategy

Instance Attribute Summary

Attributes inherited from Base

#collection

Instance Method Summary collapse

Methods inherited from Base

#already_imported?, #flush, #initialize, #prepare_attributes

Constructor Details

This class inherits a constructor from AbstractImporter::Strategies::Base

Instance Method Details

#build_record(hash) ⇒ Object



60
61
62
# File 'lib/abstract_importer/strategies/default_strategy.rb', line 60

def build_record(hash)
  collection.model.new prepare_attributes(hash)
end

#clean_record(record) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/abstract_importer/strategies/default_strategy.rb', line 64

def clean_record(record)
  # If this record isn't able to be garbage-collected,
  # then we will print out all of the objects that are
  # retaining a reference to this one. Ruby's garbage-
  # collector is smart enough to clean up objects with
  # circular references; but if we free these now, we
  # will have fewer results to consider later.
  record.remove_instance_variable :@association_cache
  record.remove_instance_variable :@errors
end

#create_record(hash) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/abstract_importer/strategies/default_strategy.rb', line 33

def create_record(hash)
  record = build_record(hash)

  return true if dry_run?

  invoke_callback(:before_create, record)
  invoke_callback(:before_save, record)

  # rescue_callback has one shot to fix things
  invoke_callback(:rescue, record) unless record.valid?

  if record.valid? && record.save
    invoke_callback(:after_create, hash, record)
    invoke_callback(:after_save, hash, record)
    id_map << record

    reporter.record_created(record)
    clean_record(record)
    true
  else

    reporter.record_failed(record, hash)
    clean_record(record)
    false
  end
end

#process_record(hash) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/abstract_importer/strategies/default_strategy.rb', line 8

def process_record(hash)
  summary.total += 1

  if already_imported?(hash)
    summary.already_imported += 1
    return
  end

  remap_foreign_keys!(hash)

  if redundant_record?(hash)
    summary.redundant += 1
    return
  end

  if create_record(hash)
    summary.created += 1
  else
    summary.invalid += 1
  end
rescue ::AbstractImporter::Skip
  summary.skipped += 1
end