deltaconveyor 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6b6a21e28386560cc9f41bf2b56e67e850b8a028
4
- data.tar.gz: 90ff8165dc997c5226cf3572db6b448983725d4c
3
+ metadata.gz: 712223865ad3f96330a2693b159d4d1b07606059
4
+ data.tar.gz: c6ca40ef25658c369f609fd479cfc0c5706c0412
5
5
  SHA512:
6
- metadata.gz: 43ae49ae35561b099105b638332c6732ae9537ae2450025242929112a919ccd18401ebbe6f36038840d09da010bd9974bee6bafd7f25d382168b504754d442e7
7
- data.tar.gz: 802b72c51ddb79545dcfc1dabfd6890b4aedb2613717c1421450391029186f9065ad17bb889373da22915d7d1424fa459910f0cf4de91bccfe2942396a5c765d
6
+ metadata.gz: f0c5743616da52a2ac2881d7e1191cfab80a52e4e5c2813dea2aa681cdedd974660ccc291c566d8da6a9f8aab54e7da8b836e545923db77600dcaf24e944b1e6
7
+ data.tar.gz: 7d6c519fcaa0f1d337c10067a1edeb6f65338d42959a5fe7d39b2d025d229d80d75ffbbedec0b5815c583bb59d3229adc447bf40f338177d7f7f1642ae5f77ad
@@ -14,10 +14,6 @@ module Deltaconveyor
14
14
  raise 'You must implement #save! method in class extends Row.'
15
15
  end
16
16
 
17
- def key
18
- :id
19
- end
20
-
21
17
  def valid?
22
18
  raise 'You must implement #valid? method in class extends Row.'
23
19
  end
@@ -1,3 +1,3 @@
1
1
  module Deltaconveyor
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/deltaconveyor.rb CHANGED
@@ -7,120 +7,95 @@ module Deltaconveyor
7
7
  class InvalidRowError < StandardError; end
8
8
  class RemovingOriginalError < StandardError; end
9
9
  def self.import(data, option, container, force_remove: false)
10
- originals = container.originals
10
+ Importer.new(option, container, force_remove: force_remove).import(data)
11
+ end
11
12
 
12
- passes_container = option.row_class.method(:from_json).parameters.size == 2
13
- rows = data.map do |d|
14
- passes_container ? option.row_class.from_json(d, container) : option.row_class.from_json(d)
15
- end
13
+ class Importer
16
14
 
17
- invalids = rows.reject(&:valid?)
18
- if invalids.size > 0
19
- option.logger.error 'Error: Some data has errors.'
20
- invalids.each do |invalid|
21
- option.logger.error invalid.errors.messages.to_s
22
- end
23
- raise InvalidRowError, 'Not Imported.'
15
+ def initialize(option, container, force_remove: false)
16
+ @option = option
17
+ @container = container
18
+ @force_remove = force_remove
24
19
  end
25
20
 
26
- option_keys = Array(option.key)
27
- original_hash = index_by_keys(originals, option_keys)
28
- new_hash = index_by_keys(rows, option_keys)
21
+ attr_reader :option, :container, :force_remove
29
22
 
30
- original_keys = original_hash.keys
31
- new_keys = new_hash.keys
23
+ def import(data)
24
+ originals = container.originals
32
25
 
33
- intersection_keys = original_keys & new_keys
34
- remove_keys = original_keys - intersection_keys
35
- adding_keys = new_keys - intersection_keys
36
-
37
- if remove_keys.size > 0 && !force_remove
38
- option.logger.error 'Error: Some data is facing to remove but `force_remove` flag is false'
39
- option.logger.error 'so import is stopped.'
40
- option.logger.error 'Removing keys:'
41
- option.logger.error remove_keys
42
- raise RemovingOriginalError, 'Not Imported.'
43
- end
26
+ passes_container = option.row_class.method(:from_json).parameters.size == 2
27
+ rows = data.map do |d|
28
+ passes_container ? option.row_class.from_json(d, container) : option.row_class.from_json(d)
29
+ end
44
30
 
45
- remove_keys.each do |key|
46
- original_data = original_hash[key]
47
- raise 'Bug.' unless original_data
48
- option.logger.info "Remove #{key}"
49
- # TODO: rethink
50
- original_data.destroy!
51
- end
52
- option.logger.info 'Removing Phase is finished.'
31
+ invalids = rows.reject(&:valid?)
32
+ if invalids.size > 0
33
+ option.logger.error 'Error: Some data has errors.'
34
+ invalids.each do |invalid|
35
+ option.logger.error invalid.errors.messages.to_s
36
+ end
37
+ raise InvalidRowError, 'Not Imported.'
38
+ end
53
39
 
54
- intersection_keys.each do |key|
55
- original_data = original_hash[key]
56
- new_data = new_hash[key]
57
- raise 'Bug.' unless original_data && new_data
40
+ option_keys = Array(option.key)
41
+ original_hash = index_by_keys(originals, option_keys)
42
+ new_hash = index_by_keys(rows, option_keys)
58
43
 
59
- new_data.update!(original_data)
60
- end
61
- option.logger.info 'Updating Phase is finished.'
44
+ original_keys = original_hash.keys
45
+ new_keys = new_hash.keys
62
46
 
63
- adding_keys.each do |key|
64
- new_data = new_hash[key]
65
- raise 'Bug.' unless new_data
47
+ intersection_keys = original_keys & new_keys
48
+ remove_keys = original_keys - intersection_keys
49
+ adding_keys = new_keys - intersection_keys
66
50
 
67
- option.logger.info "Insert #{key}"
68
- new_data.save!
69
- end
70
- option.logger.info 'Adding Phase is finished.'
71
- nil
72
- end
73
-
74
- def self.index_by_keys(arr, keys)
75
- hash = {}
76
- arr.each do |elem|
77
- pk = keys.map { |key| elem.send(key) }
78
- unless hash[pk].nil?
79
- option.logger.error "not unique key #{pk}"
80
- raise 'Not Imported.'
51
+ if remove_keys.size > 0 && !force_remove
52
+ option.logger.error 'Error: Some data is facing to remove but `force_remove` flag is false'
53
+ option.logger.error 'so import is stopped.'
54
+ option.logger.error 'Removing keys:'
55
+ option.logger.error remove_keys
56
+ raise RemovingOriginalError, 'Not Imported.'
81
57
  end
82
- hash[pk] = elem
83
- end
84
- hash
85
58
 
86
- remove_keys.each do |key|
87
- original_data = original_hash[key]
88
- raise 'Bug.' unless original_data
89
- option.logger.info "Remove #{key}"
90
- original_data.destroy!
91
- end
92
- option.logger.info 'Removing Phase is finished.'
59
+ remove_keys.each do |key|
60
+ original_data = original_hash[key]
61
+ raise 'Bug.' unless original_data
62
+ option.logger.info "Remove #{key}"
63
+ # TODO: rethink
64
+ original_data.destroy!
65
+ end
66
+ option.logger.info 'Removing Phase is finished.'
93
67
 
94
- intersection_keys.each do |key|
95
- original_data = original_hash[key]
96
- new_data = new_hash[key]
97
- raise 'Bug.' unless original_data && new_data
68
+ intersection_keys.each do |key|
69
+ original_data = original_hash[key]
70
+ new_data = new_hash[key]
71
+ raise 'Bug.' unless original_data && new_data
98
72
 
99
- new_data.update!(original_data)
100
- end
101
- option.logger.info 'Updating Phase is finished.'
73
+ new_data.update!(original_data)
74
+ end
75
+ option.logger.info 'Updating Phase is finished.'
102
76
 
103
- adding_keys.each do |key|
104
- new_data = new_hash[key]
105
- raise 'Bug.' unless new_data
77
+ adding_keys.each do |key|
78
+ new_data = new_hash[key]
79
+ raise 'Bug.' unless new_data
106
80
 
107
- option.logger.info "Insert #{key}"
108
- new_data.save!
81
+ option.logger.info "Insert #{key}"
82
+ new_data.save!
83
+ end
84
+ option.logger.info 'Adding Phase is finished.'
85
+ nil
109
86
  end
110
- option.logger.info 'Adding Phase is finished.'
111
- nil
112
- end
113
87
 
114
- def self.index_by_keys(arr, keys)
115
- hash = {}
116
- arr.each do |elem|
117
- pk = keys.map { |key| elem.send(key) }
118
- unless hash[pk].nil?
119
- option.logger.error "Error: not unique key #{pk}"
120
- raise 'Not Imported.'
88
+ def index_by_keys(arr, keys)
89
+ hash = {}
90
+ arr.each do |elem|
91
+ pk = keys.map { |key| elem.send(key) }
92
+ unless hash[pk].nil?
93
+ option.logger.error "Error: not unique key #{pk}"
94
+ raise 'Not Imported.'
95
+ end
96
+ hash[pk] = elem
121
97
  end
122
- hash[pk] = elem
98
+ hash
123
99
  end
124
- hash
125
100
  end
126
101
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deltaconveyor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - qsona
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-30 00:00:00.000000000 Z
11
+ date: 2018-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler