Class: AbstractImporter::Base
- Inherits:
-
Object
- Object
- AbstractImporter::Base
- Defined in:
- lib/abstract_importer/base.rb
Class Attribute Summary collapse
-
.dependencies ⇒ Object
readonly
Returns the value of attribute dependencies.
-
.import_plan ⇒ Object
readonly
Returns the value of attribute import_plan.
Instance Attribute Summary collapse
-
#id_map ⇒ Object
readonly
Returns the value of attribute id_map.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#reporter ⇒ Object
readonly
Returns the value of attribute reporter.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
Instance Method Summary collapse
- #atomic? ⇒ Boolean
- #describe_destination ⇒ Object
- #describe_source ⇒ Object
- #dry_run? ⇒ Boolean
- #import_collection(collection) ⇒ Object
-
#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) ⇒ Object
- #teardown ⇒ Object
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, ={}) @source = source @parent = parent io = .fetch(:io, $stderr) @reporter = default_reporter(io) @dry_run = .fetch(:dry_run, false) @id_map = IdMap.new @results = {} @import_plan = self.class.import_plan.to_h @atomic = .fetch(:atomic, false) @strategies = .fetch(:strategy, {}) @skip = Array([:skip]) @only = Array([:only]) if .key?(:only) @collections = [] end |
Class Attribute Details
.dependencies ⇒ Object (readonly)
Returns the value of attribute dependencies.
22 23 24 |
# File 'lib/abstract_importer/base.rb', line 22 def dependencies @dependencies end |
.import_plan ⇒ Object (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_map ⇒ Object (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 |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
45 46 47 |
# File 'lib/abstract_importer/base.rb', line 45 def parent @parent end |
#reporter ⇒ Object (readonly)
Returns the value of attribute reporter.
45 46 47 |
# File 'lib/abstract_importer/base.rb', line 45 def reporter @reporter end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
45 46 47 |
# File 'lib/abstract_importer/base.rb', line 45 def results @results end |
#source ⇒ Object (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
14 15 16 |
# File 'lib/abstract_importer/base.rb', line 14 def import yield @import_plan = ImportPlan.new end |
Instance Method Details
#atomic? ⇒ Boolean
47 48 49 |
# File 'lib/abstract_importer/base.rb', line 47 def atomic? @atomic end |
#describe_destination ⇒ Object
112 113 114 |
# File 'lib/abstract_importer/base.rb', line 112 def describe_destination parent.to_s end |
#describe_source ⇒ Object
108 109 110 |
# File 'lib/abstract_importer/base.rb', line 108 def describe_source source.to_s end |
#dry_run? ⇒ 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
120 121 122 |
# File 'lib/abstract_importer/base.rb', line 120 def remap_foreign_key?(plural, foreign_key) true end |
#setup ⇒ Object
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
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 |
#teardown ⇒ Object
90 91 |
# File 'lib/abstract_importer/base.rb', line 90 def teardown end |