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.
8 9 10 11 12 |
# File 'lib/abstract_importer/collection_importer.rb', line 8 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.
14 15 16 |
# File 'lib/abstract_importer/collection_importer.rb', line 14 def collection @collection end |
#importer ⇒ Object (readonly)
Returns the value of attribute importer.
14 15 16 |
# File 'lib/abstract_importer/collection_importer.rb', line 14 def importer @importer end |
#strategy ⇒ Object (readonly)
Returns the value of attribute strategy.
14 15 16 |
# File 'lib/abstract_importer/collection_importer.rb', line 14 def strategy @strategy end |
#summary ⇒ Object (readonly)
Returns the value of attribute summary.
14 15 16 |
# File 'lib/abstract_importer/collection_importer.rb', line 14 def summary @summary end |
Instance Method Details
#each_new_record ⇒ Object
90 91 92 93 94 |
# File 'lib/abstract_importer/collection_importer.rb', line 90 def each_new_record source.public_send(name).each do |attrs| yield attrs.dup end end |
#invoke_callback(callback, *args) ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/abstract_importer/collection_importer.rb', line 120 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
84 85 86 |
# File 'lib/abstract_importer/collection_importer.rb', line 84 def map_foreign_key(legacy_id, foreign_key, depends_on) importer.map_foreign_key(legacy_id, name, foreign_key, depends_on) end |
#perform! ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/abstract_importer/collection_importer.rb', line 37 def perform! reporter.start_collection(self) prepare! invoke_callback(:before_all) summary.ms = Benchmark.ms 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
56 57 58 59 |
# File 'lib/abstract_importer/collection_importer.rb', line 56 def prepare! @summary = Summary.new @mappings = prepare_mappings! end |
#prepare_mappings! ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/abstract_importer/collection_importer.rb', line 61 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
108 109 110 111 112 113 114 115 116 |
# File 'lib/abstract_importer/collection_importer.rb', line 108 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
98 99 100 101 102 103 104 105 106 |
# File 'lib/abstract_importer/collection_importer.rb', line 98 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 |