abstract_importer 1.5.0 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 75b8682cc7102c4700ff606c17d5761a3bd93bfe
4
- data.tar.gz: a79b16c14e77b902400ce9a6149a4629d5b3d2d0
3
+ metadata.gz: 31e3cc0171a843a214b22c3e6e3ae23cbedcd5e3
4
+ data.tar.gz: 1a59ba701e8554d5961241e523ed9a6e4ef86cc8
5
5
  SHA512:
6
- metadata.gz: 260320a2d92c89d560866290b1871499ef2bbb83a64ec669cd1fd6b6e0d686b08813b785214c4f3bdbd246aa34d81c190a2f957f79ed53904aa3d9cdda000477
7
- data.tar.gz: 11d5dbffe9b22088cdaa785ceb2c764055c465beb6afc87ddbf7163143ad083bb17672cef4b54d0a563669e4ef1ceedd4b4884a8c872d2a586c3aab154e85c70
6
+ metadata.gz: aa2eb7be342b3a8852019174770cae2eb20cbb71fd22ae06f2fb5f61fe0787558ab2799e368767f286430c8479ea62cb90ef233e2735bf90e0131a69291d7071
7
+ data.tar.gz: 55ddab55d457e31cc62f6e17cc47e253a05c2237e37a1915b37b97feefd24aeccc27841c8e2617a5d016e324158ea10a7d0625dbb159c7b6ba9fd098397455f4
@@ -95,10 +95,10 @@ module AbstractImporter
95
95
  {}.tap do |results|
96
96
  reporter.start_all(self)
97
97
 
98
- ms = Benchmark.ms do
98
+ setup_ms = Benchmark.ms do
99
99
  setup
100
100
  end
101
- reporter.finish_setup(self, ms)
101
+ reporter.finish_setup(self, setup_ms)
102
102
 
103
103
  ms = Benchmark.ms do
104
104
  with_transaction do
@@ -108,12 +108,12 @@ module AbstractImporter
108
108
  end
109
109
  end
110
110
 
111
- ms = Benchmark.ms do
111
+ teardown_ms = Benchmark.ms do
112
112
  teardown
113
113
  end
114
- reporter.finish_teardown(self, ms)
114
+ reporter.finish_teardown(self, teardown_ms)
115
115
 
116
- reporter.finish_all(self, ms)
116
+ reporter.finish_all(self, setup_ms + ms + teardown_ms)
117
117
  end
118
118
  end
119
119
 
@@ -128,10 +128,10 @@ module AbstractImporter
128
128
 
129
129
  def collection_counts
130
130
  @collection_counts ||= Hash.new do |counts, collection_name|
131
- counts[collection_name] = if self.source.respond_to?(:"#{collection_name}_count")
132
- self.source.public_send(:"#{collection_name}_count")
131
+ counts[collection_name] = if source.respond_to?(:"#{collection_name}_count")
132
+ source.public_send(:"#{collection_name}_count")
133
133
  else
134
- self.source.public_send(collection_name).count
134
+ source.public_send(collection_name).count
135
135
  end
136
136
  end
137
137
  end
@@ -1,4 +1,6 @@
1
1
  require "abstract_importer/strategies"
2
+ require "abstract_importer/mapping"
3
+ require "abstract_importer/polymorphic_mapping"
2
4
 
3
5
  module AbstractImporter
4
6
  class CollectionImporter
@@ -27,7 +29,6 @@ module AbstractImporter
27
29
  :use_id_map_for?,
28
30
  :remap_foreign_key?,
29
31
  :id_map,
30
- :map_foreign_key,
31
32
  :generate_id,
32
33
  :to => :importer
33
34
 
@@ -72,40 +73,16 @@ module AbstractImporter
72
73
  next unless remap_foreign_key?(name, foreign_key)
73
74
 
74
75
  if association.options[:polymorphic]
75
- mappings << prepare_polymorphic_mapping!(association)
76
+ mappings << AbstractImporter::PolymorphicMapping.new(self, association)
76
77
  else
77
- mappings << prepare_mapping!(association)
78
+ mappings << AbstractImporter::Mapping.new(self, association)
78
79
  end
79
80
  end
80
81
  mappings
81
82
  end
82
83
 
83
- def prepare_mapping!(association)
84
- depends_on = association.table_name.to_sym
85
- foreign_key = association.foreign_key.to_sym
86
-
87
- Proc.new do |attrs|
88
- if attrs.key?(foreign_key)
89
- attrs[foreign_key] = map_foreign_key(attrs[foreign_key], name, foreign_key, depends_on)
90
- else
91
- reporter.count_notice "#{name}.#{foreign_key} will not be mapped because it is not used"
92
- end
93
- end
94
- end
95
-
96
- def prepare_polymorphic_mapping!(association)
97
- foreign_key = association.foreign_key.to_sym
98
- foreign_type = association.foreign_key.gsub(/_id$/, "_type").to_sym
99
-
100
- Proc.new do |attrs|
101
- if attrs.key?(foreign_key) && attrs.key?(foreign_type)
102
- foreign_model = attrs[foreign_type]
103
- depends_on = foreign_model.constantize.table_name.to_sym if foreign_model
104
- attrs[foreign_key] = depends_on && map_foreign_key(attrs[foreign_key], name, foreign_key, depends_on)
105
- else
106
- reporter.count_notice "#{name}.#{foreign_key} will not be mapped because it is not used"
107
- end
108
- end
84
+ def map_foreign_key(legacy_id, foreign_key, depends_on)
85
+ importer.map_foreign_key(legacy_id, name, foreign_key, depends_on)
109
86
  end
110
87
 
111
88
 
@@ -119,8 +96,12 @@ module AbstractImporter
119
96
 
120
97
 
121
98
  def remap_foreign_keys!(hash)
122
- @mappings.each do |proc|
123
- proc.call(hash)
99
+ @mappings.each_with_index do |mapping, i|
100
+ if mapping.applicable?(hash)
101
+ mapping.apply!(hash)
102
+ else
103
+ reporter.count_notice "#{mapping} will not be mapped because it is not used"
104
+ end
124
105
  end
125
106
  end
126
107
 
@@ -0,0 +1,32 @@
1
+ module AbstractImporter
2
+ class Mapping
3
+ attr_reader :collection, :foreign_key, :depends_on
4
+
5
+ def initialize(collection, association)
6
+ @collection = collection
7
+ @depends_on = association.table_name.to_sym
8
+ @foreign_key = association.foreign_key.to_sym
9
+ end
10
+
11
+ def applicable?(attrs)
12
+ attrs.key?(foreign_key)
13
+ end
14
+
15
+ def apply!(attrs)
16
+ attrs[foreign_key] = apply(attrs)
17
+ end
18
+
19
+ def apply(attrs)
20
+ collection.map_foreign_key(attrs[foreign_key], foreign_key, depends_on)
21
+ end
22
+
23
+ def inspect
24
+ "#<#{self.class.name} #{self}>"
25
+ end
26
+
27
+ def to_s
28
+ "#{collection.name}.#{foreign_key}"
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,40 @@
1
+ module AbstractImporter
2
+ class PolymorphicMapping < AbstractImporter::Mapping
3
+ attr_reader :collection, :foreign_key, :foreign_type
4
+
5
+ def initialize(collection, association)
6
+ @collection = collection
7
+ @foreign_key = association.foreign_key.to_sym
8
+ @foreign_type = association.foreign_key.gsub(/_id$/, "_type").to_sym
9
+ @table_name_by_foreign_model = Hash.new do |map, foreign_model|
10
+ map[foreign_model] = foreign_model && foreign_model.constantize.table_name.to_sym
11
+ end
12
+ end
13
+
14
+ def applicable?(attrs)
15
+ attrs.key?(foreign_key) && attrs.key?(foreign_type)
16
+ end
17
+ alias applies_to? applicable?
18
+
19
+ def foreign_model_for(attrs)
20
+ attrs[foreign_type]
21
+ end
22
+
23
+ def foreign_table_for(attrs)
24
+ table_name_for(foreign_model_for(attrs))
25
+ end
26
+
27
+ def apply(attrs)
28
+ depends_on = foreign_table_for(attrs)
29
+ collection.map_foreign_key(attrs[foreign_key], foreign_key, depends_on) if depends_on
30
+ end
31
+ alias [] apply
32
+
33
+ private
34
+
35
+ def table_name_for(foreign_model)
36
+ @table_name_by_foreign_model[foreign_model]
37
+ end
38
+
39
+ end
40
+ end
@@ -41,7 +41,9 @@ module AbstractImporter
41
41
 
42
42
  if remap_ids?
43
43
  hash = hash.merge(legacy_id: hash.delete(:id))
44
- hash[:id] = generate_id.call if generate_id
44
+ if generate_id
45
+ hash[:id] = generate_id.arity.zero? ? generate_id.call : generate_id.call(hash)
46
+ end
45
47
  end
46
48
 
47
49
  hash.merge(association_attrs)
@@ -1,3 +1,3 @@
1
1
  module AbstractImporter
2
- VERSION = "1.5.0"
2
+ VERSION = "1.5.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: abstract_importer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bob Lail
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-07 00:00:00.000000000 Z
11
+ date: 2017-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -242,6 +242,8 @@ files:
242
242
  - lib/abstract_importer/id_map.rb
243
243
  - lib/abstract_importer/import_options.rb
244
244
  - lib/abstract_importer/import_plan.rb
245
+ - lib/abstract_importer/mapping.rb
246
+ - lib/abstract_importer/polymorphic_mapping.rb
245
247
  - lib/abstract_importer/reporters.rb
246
248
  - lib/abstract_importer/reporters/base_reporter.rb
247
249
  - lib/abstract_importer/reporters/debug_reporter.rb