Class: AbstractImporter::Base
- Inherits:
-
Object
- Object
- AbstractImporter::Base
- Defined in:
- lib/abstract_importer/base.rb
Class Attribute Summary collapse
-
.import_plan ⇒ Object
readonly
Returns the value of attribute import_plan.
Instance Attribute Summary collapse
-
#collection_importers ⇒ Object
readonly
Returns the value of attribute collection_importers.
-
#collections ⇒ Object
readonly
Returns the value of attribute collections.
-
#generate_id ⇒ Object
readonly
Returns the value of attribute generate_id.
-
#id_map ⇒ Object
readonly
Returns the value of attribute id_map.
-
#import_plan ⇒ Object
readonly
Returns the value of attribute import_plan.
-
#only ⇒ Object
readonly
Returns the value of attribute only.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#reporter ⇒ Object
readonly
Returns the value of attribute reporter.
-
#skip ⇒ Object
readonly
Returns the value of attribute skip.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
- .dependencies ⇒ Object
- .depends_on(*dependencies) ⇒ Object
- .import {|@import_plan = ImportPlan.new| ... } ⇒ Object
Instance Method Summary collapse
- #atomic? ⇒ Boolean
- #collection_counts ⇒ Object
- #count_collection(collection) ⇒ Object
- #describe_destination ⇒ Object
- #describe_source ⇒ Object
- #dry_run? ⇒ Boolean
-
#initialize(parent, source, options = {}) ⇒ Base
constructor
A new instance of Base.
- #map_foreign_key(legacy_id, plural, foreign_key, depends_on) ⇒ Object
- #perform! ⇒ Object
- #remap_foreign_key?(plural, foreign_key) ⇒ Boolean
- #setup ⇒ Object
- #skip?(collection) ⇒ Boolean
- #strategy_for(collection_importer) ⇒ Object
- #teardown ⇒ Object
- #use_id_map_for?(collection) ⇒ Boolean
Constructor Details
#initialize(parent, source, options = {}) ⇒ Base
Returns a new instance of Base.
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 59 60 61 62 |
# File 'lib/abstract_importer/base.rb', line 33 def initialize(parent, source, ={}) @source = source @parent = parent @options = io = .fetch(:io, $stderr) @reporter = default_reporter(, io) @dry_run = .fetch(:dry_run, false) id_map = .fetch(:id_map, true) @import_plan = self.class.import_plan.to_h @atomic = .fetch(:atomic, false) @strategies = .fetch(:strategy, {}) @generate_id = [:generate_id] @skip = Array([:skip]) @only = Array([:only]) if .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 |
Class Attribute Details
.import_plan ⇒ Object (readonly)
Returns the value of attribute import_plan.
28 29 30 |
# File 'lib/abstract_importer/base.rb', line 28 def import_plan @import_plan end |
Instance Attribute Details
#collection_importers ⇒ Object (readonly)
Returns the value of attribute collection_importers.
64 65 66 |
# File 'lib/abstract_importer/base.rb', line 64 def collection_importers @collection_importers end |
#collections ⇒ Object (readonly)
Returns the value of attribute collections.
64 65 66 |
# File 'lib/abstract_importer/base.rb', line 64 def collections @collections end |
#generate_id ⇒ Object (readonly)
Returns the value of attribute generate_id.
64 65 66 |
# File 'lib/abstract_importer/base.rb', line 64 def generate_id @generate_id end |
#id_map ⇒ Object (readonly)
Returns the value of attribute id_map.
64 65 66 |
# File 'lib/abstract_importer/base.rb', line 64 def id_map @id_map end |
#import_plan ⇒ Object (readonly)
Returns the value of attribute import_plan.
64 65 66 |
# File 'lib/abstract_importer/base.rb', line 64 def import_plan @import_plan end |
#only ⇒ Object (readonly)
Returns the value of attribute only.
64 65 66 |
# File 'lib/abstract_importer/base.rb', line 64 def only @only end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
64 65 66 |
# File 'lib/abstract_importer/base.rb', line 64 def @options end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
64 65 66 |
# File 'lib/abstract_importer/base.rb', line 64 def parent @parent end |
#reporter ⇒ Object (readonly)
Returns the value of attribute reporter.
64 65 66 |
# File 'lib/abstract_importer/base.rb', line 64 def reporter @reporter end |
#skip ⇒ Object (readonly)
Returns the value of attribute skip.
64 65 66 |
# File 'lib/abstract_importer/base.rb', line 64 def skip @skip end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
64 65 66 |
# File 'lib/abstract_importer/base.rb', line 64 def source @source end |
Class Method Details
.dependencies ⇒ Object
24 25 26 |
# File 'lib/abstract_importer/base.rb', line 24 def dependencies @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
16 17 18 |
# File 'lib/abstract_importer/base.rb', line 16 def import yield @import_plan = ImportPlan.new end |
Instance Method Details
#atomic? ⇒ Boolean
82 83 84 |
# File 'lib/abstract_importer/base.rb', line 82 def atomic? @atomic end |
#collection_counts ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/abstract_importer/base.rb', line 129 def collection_counts @collection_counts ||= Hash.new do |counts, collection_name| counts[collection_name] = if self.source.respond_to?(:"#{collection_name}_count") self.source.public_send(:"#{collection_name}_count") else self.source.public_send(collection_name).count end end end |
#count_collection(collection) ⇒ Object
124 125 126 127 |
# File 'lib/abstract_importer/base.rb', line 124 def count_collection(collection) collection_name = collection.respond_to?(:name) ? collection.name : collection collection_counts[collection_name] end |
#describe_destination ⇒ Object
169 170 171 |
# File 'lib/abstract_importer/base.rb', line 169 def describe_destination parent.to_s end |
#describe_source ⇒ Object
165 166 167 |
# File 'lib/abstract_importer/base.rb', line 165 def describe_source source.to_s end |
#dry_run? ⇒ Boolean
86 87 88 |
# File 'lib/abstract_importer/base.rb', line 86 def dry_run? @dry_run end |
#map_foreign_key(legacy_id, plural, foreign_key, depends_on) ⇒ Object
181 182 183 184 185 186 187 |
# File 'lib/abstract_importer/base.rb', line 181 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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/abstract_importer/base.rb', line 94 def perform! {}.tap do |results| reporter.start_all(self) ms = Benchmark.ms do setup end reporter.finish_setup(self, ms) ms = Benchmark.ms do with_transaction do collection_importers.each do |importer| results[importer.name] = importer.perform! end end end ms = Benchmark.ms do teardown end reporter.finish_teardown(self, ms) reporter.finish_all(self, ms) end end |
#remap_foreign_key?(plural, foreign_key) ⇒ Boolean
177 178 179 |
# File 'lib/abstract_importer/base.rb', line 177 def remap_foreign_key?(plural, foreign_key) true end |
#setup ⇒ Object
120 121 122 |
# File 'lib/abstract_importer/base.rb', line 120 def setup prepopulate_id_map! end |
#skip?(collection) ⇒ Boolean
142 143 144 145 146 147 |
# File 'lib/abstract_importer/base.rb', line 142 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
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/abstract_importer/base.rb', line 149 def strategy_for(collection_importer) collection = collection_importer.collection strategy_name = @strategies.fetch collection.name, :default = {} if strategy_name.is_a?(Hash) = strategy_name strategy_name = strategy_name[:name] end strategy_klass = AbstractImporter::Strategies.const_get :"#{strategy_name.capitalize}Strategy" strategy_klass.new(collection_importer, ) end |
#teardown ⇒ Object
139 140 |
# File 'lib/abstract_importer/base.rb', line 139 def teardown end |
#use_id_map_for?(collection) ⇒ Boolean
76 77 78 79 80 |
# File 'lib/abstract_importer/base.rb', line 76 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 |