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
- #perform! ⇒ Object
- #prepare! ⇒ Object
- #prepare_mapping!(association) ⇒ Object
- #prepare_mappings! ⇒ Object
- #prepare_polymorphic_mapping!(association) ⇒ Object
- #redundant_record?(hash) ⇒ Boolean
- #remap_foreign_keys!(hash) ⇒ Object
Constructor Details
#initialize(importer, collection) ⇒ CollectionImporter
Returns a new instance of CollectionImporter.
6 7 8 9 10 |
# File 'lib/abstract_importer/collection_importer.rb', line 6 def initialize(importer, collection) @importer = importer @collection = collection @strategy = importer.strategy_for(collection).new(self) end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
12 13 14 |
# File 'lib/abstract_importer/collection_importer.rb', line 12 def collection @collection end |
#importer ⇒ Object (readonly)
Returns the value of attribute importer.
12 13 14 |
# File 'lib/abstract_importer/collection_importer.rb', line 12 def importer @importer end |
#strategy ⇒ Object (readonly)
Returns the value of attribute strategy.
12 13 14 |
# File 'lib/abstract_importer/collection_importer.rb', line 12 def strategy @strategy end |
#summary ⇒ Object (readonly)
Returns the value of attribute summary.
12 13 14 |
# File 'lib/abstract_importer/collection_importer.rb', line 12 def summary @summary end |
Instance Method Details
#each_new_record ⇒ Object
110 111 112 113 114 |
# File 'lib/abstract_importer/collection_importer.rb', line 110 def each_new_record source.public_send(name).each do |attrs| yield attrs.dup end end |
#invoke_callback(callback, *args) ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/abstract_importer/collection_importer.rb', line 136 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 |
#perform! ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/abstract_importer/collection_importer.rb', line 33 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
52 53 54 55 |
# File 'lib/abstract_importer/collection_importer.rb', line 52 def prepare! @summary = Summary.new @mappings = prepare_mappings! end |
#prepare_mapping!(association) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/abstract_importer/collection_importer.rb', line 80 def prepare_mapping!(association) depends_on = association.table_name.to_sym foreign_key = association.foreign_key.to_sym Proc.new do |attrs| if attrs.key?(foreign_key) attrs[foreign_key] = map_foreign_key(attrs[foreign_key], name, foreign_key, depends_on) else reporter.count_notice "#{name}.#{foreign_key} will not be mapped because it is not used" end end end |
#prepare_mappings! ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/abstract_importer/collection_importer.rb', line 57 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 << prepare_polymorphic_mapping!(association) else mappings << prepare_mapping!(association) end end mappings end |
#prepare_polymorphic_mapping!(association) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/abstract_importer/collection_importer.rb', line 93 def prepare_polymorphic_mapping!(association) foreign_key = association.foreign_key.to_sym foreign_type = association.foreign_key.gsub(/_id$/, "_type").to_sym Proc.new do |attrs| if attrs.key?(foreign_key) && attrs.key?(foreign_type) foreign_model = attrs[foreign_type] depends_on = foreign_model.tableize.to_sym if foreign_model attrs[foreign_key] = depends_on && map_foreign_key(attrs[foreign_key], name, foreign_key, depends_on) else reporter.count_notice "#{name}.#{foreign_key} will not be mapped because it is not used" end end end |
#redundant_record?(hash) ⇒ Boolean
124 125 126 127 128 129 130 131 132 |
# File 'lib/abstract_importer/collection_importer.rb', line 124 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
118 119 120 121 122 |
# File 'lib/abstract_importer/collection_importer.rb', line 118 def remap_foreign_keys!(hash) @mappings.each do |proc| proc.call(hash) end end |