Class: AbstractImporter::Base

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

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.



47
48
49
50
51
52
53
54
55
56
57
58
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 47

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

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

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

  @use_id_map, @id_map = id_map.is_a?(IdMap) ? [true, id_map] : [id_map, IdMap.new]

  verify_source!
  verify_parent!
  instantiate_collections!

  @collection_importers = []
  collections.each do |collection|
    next if skip? collection
    @collection_importers.push CollectionImporter.new(self, collection)
  end
end

Instance Attribute Details

#collection_importersObject (readonly)

Returns the value of attribute collection_importers.



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

def collection_importers
  @collection_importers
end

#collectionsObject (readonly)

Returns the value of attribute collections.



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

def collections
  @collections
end

#generate_idObject (readonly)

Returns the value of attribute generate_id.



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

def generate_id
  @generate_id
end

#id_mapObject (readonly)

Returns the value of attribute id_map.



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

def id_map
  @id_map
end

#import_planObject (readonly)

Returns the value of attribute import_plan.



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

def import_plan
  @import_plan
end

#onlyObject (readonly)

Returns the value of attribute only.



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

def only
  @only
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#reporterObject (readonly)

Returns the value of attribute reporter.



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

def reporter
  @reporter
end

#skipObject (readonly)

Returns the value of attribute skip.



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

def skip
  @skip
end

#sourceObject (readonly)

Returns the value of attribute source.



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

def source
  @source
end

Class Method Details

.dependenciesObject



24
25
26
# File 'lib/abstract_importer/base.rb', line 24

def dependencies
  read_inheritable_instance_variable(:@dependencies) || []
end

.depends_on(*dependencies) ⇒ Object



20
21
22
# File 'lib/abstract_importer/base.rb', line 20

def depends_on(*dependencies)
  @dependencies = dependencies
end

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

Yields:



16
17
18
# File 'lib/abstract_importer/base.rb', line 16

def import
  yield @import_plan = ImportPlan.new
end

.import_planObject



28
29
30
# File 'lib/abstract_importer/base.rb', line 28

def import_plan
  read_inheritable_instance_variable(:@import_plan)
end

Instance Method Details

#atomic?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/abstract_importer/base.rb', line 96

def atomic?
  @atomic
end

#collection_countsObject



143
144
145
146
147
148
149
150
151
# File 'lib/abstract_importer/base.rb', line 143

def collection_counts
  @collection_counts ||= Hash.new do |counts, collection_name|
    counts[collection_name] = if source.respond_to?(:"#{collection_name}_count")
      source.public_send(:"#{collection_name}_count")
    else
      source.public_send(collection_name).count
    end
  end
end

#count_collection(collection) ⇒ Object



138
139
140
141
# File 'lib/abstract_importer/base.rb', line 138

def count_collection(collection)
  collection_name = collection.respond_to?(:name) ? collection.name : collection
  collection_counts[collection_name]
end

#describe_destinationObject



183
184
185
# File 'lib/abstract_importer/base.rb', line 183

def describe_destination
  parent.to_s
end

#describe_sourceObject



179
180
181
# File 'lib/abstract_importer/base.rb', line 179

def describe_source
  source.to_s
end

#dry_run?Boolean

Returns:

  • (Boolean)


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

def dry_run?
  @dry_run
end

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



195
196
197
198
199
200
201
# File 'lib/abstract_importer/base.rb', line 195

def map_foreign_key(legacy_id, plural, foreign_key, depends_on)
  return nil if legacy_id.nil?
  return legacy_id unless use_id_map_for?(depends_on)
  id_map.apply!(depends_on, legacy_id)
rescue KeyError
  raise IdNotMappedError, "#{plural}.#{foreign_key} will be nil: a #{depends_on.to_s.singularize} with the legacy id #{legacy_id} was not mapped."
end

#perform!Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/abstract_importer/base.rb', line 108

def perform!
  {}.tap do |results|
    reporter.start_all(self)

    setup_ms = Benchmark.ms do
      setup
    end
    reporter.finish_setup(self, setup_ms)

    ms = Benchmark.ms do
      with_transaction do
        collection_importers.each do |importer|
          results[importer.name] = importer.perform!
        end
      end
    end

    teardown_ms = Benchmark.ms do
      teardown
    end
    reporter.finish_teardown(self, teardown_ms)

    reporter.finish_all(self, setup_ms + ms + teardown_ms)
  end
end

#remap_foreign_key?(plural, foreign_key) ⇒ Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/abstract_importer/base.rb', line 191

def remap_foreign_key?(plural, foreign_key)
  true
end

#setupObject



134
135
136
# File 'lib/abstract_importer/base.rb', line 134

def setup
  prepopulate_id_map!
end

#skip?(collection) ⇒ Boolean

Returns:

  • (Boolean)


156
157
158
159
160
161
# File 'lib/abstract_importer/base.rb', line 156

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

#strategy_for(collection_importer) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/abstract_importer/base.rb', line 163

def strategy_for(collection_importer)
  collection = collection_importer.collection
  strategy_name = @strategies.fetch collection.name, :default
  strategy_options = {}
  if strategy_name.is_a?(Hash)
    strategy_options = strategy_name
    strategy_name = strategy_name[:name]
  end
  strategy_klass = AbstractImporter::Strategies.const_get :"#{strategy_name.capitalize}Strategy"
  strategy_klass.new(collection_importer, strategy_options)
end

#teardownObject



153
154
# File 'lib/abstract_importer/base.rb', line 153

def teardown
end

#use_id_map_for?(collection) ⇒ Boolean

Returns:

  • (Boolean)


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

def use_id_map_for?(collection)
  collection = find_collection(collection) if collection.is_a?(Symbol)
  return false unless collection
  @use_id_map && collection.has_legacy_id?
end