Class: AbstractImporter::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/abstract_importer/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, source, options = {}) ⇒ Base

Returns a new instance of Base.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/abstract_importer/base.rb', line 27

def initialize(parent, source, options={})
  @source       = source
  @parent       = parent

  io            = options.fetch(:io, $stderr)
  @reporter     = default_reporter(io)
  @dry_run      = options.fetch(:dry_run, false)

  @id_map       = IdMap.new
  @results      = {}
  @import_plan  = self.class.import_plan.to_h
  @atomic       = options.fetch(:atomic, false)
  @strategies   = options.fetch(:strategy, {})
  @skip         = Array(options[:skip])
  @only         = Array(options[:only]) if options.key?(:only)
  @collections  = []
end

Class Attribute Details

.dependenciesObject (readonly)

Returns the value of attribute dependencies.



22
23
24
# File 'lib/abstract_importer/base.rb', line 22

def dependencies
  @dependencies
end

.import_planObject (readonly)

Returns the value of attribute import_plan.



22
23
24
# File 'lib/abstract_importer/base.rb', line 22

def import_plan
  @import_plan
end

Instance Attribute Details

#id_mapObject (readonly)

Returns the value of attribute id_map.



45
46
47
# File 'lib/abstract_importer/base.rb', line 45

def id_map
  @id_map
end

#parentObject (readonly)

Returns the value of attribute parent.



45
46
47
# File 'lib/abstract_importer/base.rb', line 45

def parent
  @parent
end

#reporterObject (readonly)

Returns the value of attribute reporter.



45
46
47
# File 'lib/abstract_importer/base.rb', line 45

def reporter
  @reporter
end

#resultsObject (readonly)

Returns the value of attribute results.



45
46
47
# File 'lib/abstract_importer/base.rb', line 45

def results
  @results
end

#sourceObject (readonly)

Returns the value of attribute source.



45
46
47
# File 'lib/abstract_importer/base.rb', line 45

def source
  @source
end

Class Method Details

.depends_on(*dependencies) ⇒ Object



18
19
20
# File 'lib/abstract_importer/base.rb', line 18

def depends_on(*dependencies)
  @dependencies = dependencies
end

.import {|@import_plan = ImportPlan.new| ... } ⇒ Object

Yields:



14
15
16
# File 'lib/abstract_importer/base.rb', line 14

def import
  yield @import_plan = ImportPlan.new
end

Instance Method Details

#atomic?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/abstract_importer/base.rb', line 47

def atomic?
  @atomic
end

#describe_destinationObject



112
113
114
# File 'lib/abstract_importer/base.rb', line 112

def describe_destination
  parent.to_s
end

#describe_sourceObject



108
109
110
# File 'lib/abstract_importer/base.rb', line 108

def describe_source
  source.to_s
end

#dry_run?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/abstract_importer/base.rb', line 51

def dry_run?
  @dry_run
end

#import_collection(collection) ⇒ Object



85
86
87
88
# File 'lib/abstract_importer/base.rb', line 85

def import_collection(collection)
  return if skip? collection
  results[collection.name] = CollectionImporter.new(self, collection).perform!
end

#map_foreign_key(legacy_id, plural, foreign_key, depends_on) ⇒ Object



124
125
126
127
128
129
# File 'lib/abstract_importer/base.rb', line 124

def map_foreign_key(legacy_id, plural, foreign_key, depends_on)
  id_map.apply!(legacy_id, depends_on)
rescue IdMap::IdNotMappedError
  record_no_id_in_map_error(legacy_id, plural, foreign_key, depends_on)
  nil
end

#perform!Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/abstract_importer/base.rb', line 59

def perform!
  reporter.start_all(self)

  ms = Benchmark.ms do
    setup
  end
  reporter.finish_setup(ms)

  ms = Benchmark.ms do
    with_transaction do
      collections.each &method(:import_collection)
    end
  end

  teardown
  reporter.finish_all(self, ms)
  results
end

#remap_foreign_key?(plural, foreign_key) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/abstract_importer/base.rb', line 120

def remap_foreign_key?(plural, foreign_key)
  true
end

#setupObject



78
79
80
81
82
83
# File 'lib/abstract_importer/base.rb', line 78

def setup
  verify_source!
  verify_parent!
  instantiate_collections!
  prepopulate_id_map!
end

#skip?(collection) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
# File 'lib/abstract_importer/base.rb', line 93

def skip?(collection)
  return true if skip.member?(collection.name)
  return true if only && !only.member?(collection.name)
  false
end

#strategy_for(collection) ⇒ Object



99
100
101
102
# File 'lib/abstract_importer/base.rb', line 99

def strategy_for(collection)
  strategy_name = @strategies.fetch collection.name, :default
  AbstractImporter::Strategies.const_get :"#{strategy_name.capitalize}Strategy"
end

#teardownObject



90
91
# File 'lib/abstract_importer/base.rb', line 90

def teardown
end