Class: AbstractImporter::CollectionImporter
- Inherits:
-
Object
- Object
- AbstractImporter::CollectionImporter
- Defined in:
- lib/abstract_importer/collection_importer.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#importer ⇒ Object
readonly
Returns the value of attribute importer.
-
#strategy ⇒ Object
readonly
Returns the value of attribute strategy.
-
#summary ⇒ Object
readonly
Returns the value of attribute summary.
Instance Method Summary collapse
- #each_new_record ⇒ Object
-
#initialize(importer, collection) ⇒ CollectionImporter
constructor
A new instance of CollectionImporter.
- #invoke_callback(callback, *args) ⇒ Object
- #map_foreign_key(legacy_id, foreign_key, depends_on) ⇒ Object
- #perform! ⇒ Object
- #prepare! ⇒ Object
- #prepare_mappings! ⇒ Object
- #redundant_record?(hash) ⇒ Boolean
- #remap_foreign_keys!(hash) ⇒ Object
Constructor Details
#initialize(importer, collection) ⇒ CollectionImporter
Returns a new instance of CollectionImporter.
9 10 11 12 13 |
# File 'lib/abstract_importer/collection_importer.rb', line 9 def initialize(importer, collection) @importer = importer @collection = collection @strategy = importer.strategy_for(self) end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
15 16 17 |
# File 'lib/abstract_importer/collection_importer.rb', line 15 def collection @collection end |
#importer ⇒ Object (readonly)
Returns the value of attribute importer.
15 16 17 |
# File 'lib/abstract_importer/collection_importer.rb', line 15 def importer @importer end |
#strategy ⇒ Object (readonly)
Returns the value of attribute strategy.
15 16 17 |
# File 'lib/abstract_importer/collection_importer.rb', line 15 def strategy @strategy end |
#summary ⇒ Object (readonly)
Returns the value of attribute summary.
15 16 17 |
# File 'lib/abstract_importer/collection_importer.rb', line 15 def summary @summary end |
Instance Method Details
#each_new_record ⇒ Object
91 92 93 94 95 |
# File 'lib/abstract_importer/collection_importer.rb', line 91 def each_new_record source.public_send(name).each do |attrs| yield attrs.dup end end |
#invoke_callback(callback, *args) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/abstract_importer/collection_importer.rb', line 121 def invoke_callback(callback, *args) callback_name = :"#{callback}_callback" callback = .public_send(callback_name) return unless callback callback = importer.method(callback) if callback.is_a?(Symbol) callback.call(*args) end |
#map_foreign_key(legacy_id, foreign_key, depends_on) ⇒ Object
85 86 87 |
# File 'lib/abstract_importer/collection_importer.rb', line 85 def map_foreign_key(legacy_id, foreign_key, depends_on) importer.map_foreign_key(legacy_id, name, foreign_key, depends_on) end |
#perform! ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/abstract_importer/collection_importer.rb', line 38 def perform! reporter.start_collection(self) prepare! invoke_callback(:before_all) summary.ms = 1000 * Benchmark.realtime do each_new_record do |attributes| strategy.process_record(attributes) end end strategy.flush invoke_callback(:after_all) reporter.finish_collection(self, summary) summary end |
#prepare! ⇒ Object
57 58 59 60 |
# File 'lib/abstract_importer/collection_importer.rb', line 57 def prepare! @summary = Summary.new @mappings = prepare_mappings! end |
#prepare_mappings! ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/abstract_importer/collection_importer.rb', line 62 def prepare_mappings! mappings = [] model.reflect_on_all_associations.each do |association| # We only want the associations where this record # has foreign keys that refer to another next unless association.macro == :belongs_to # We support skipping some mappings entirely. I believe # this is largely to cut down on verbosity in the log # files and should be refactored to another place in time. foreign_key = association.foreign_key.to_sym next unless remap_foreign_key?(name, foreign_key) if association.[:polymorphic] mappings << AbstractImporter::PolymorphicMapping.new(self, association) else mappings << AbstractImporter::Mapping.new(self, association) end end mappings end |
#redundant_record?(hash) ⇒ Boolean
109 110 111 112 113 114 115 116 117 |
# File 'lib/abstract_importer/collection_importer.rb', line 109 def redundant_record?(hash) existing_record = invoke_callback(:finder, hash) if existing_record id_map.register(record: existing_record, legacy_id: hash[:id]) true else false end end |
#remap_foreign_keys!(hash) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/abstract_importer/collection_importer.rb', line 99 def remap_foreign_keys!(hash) @mappings.each_with_index do |mapping, i| if mapping.applicable?(hash) mapping.apply!(hash) else reporter.count_notice "#{mapping} will not be mapped because it is not used" end end end |