sequel_deep_dup 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sequel_deep_dup.rb +18 -5
- data/lib/sequel_deep_dup/version.rb +1 -1
- data/spec/deep_dup_spec.rb +66 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f6f02dab9f3b6c333a4de5c5fd5a5079a8cbd0c2
|
4
|
+
data.tar.gz: 590e586fd31aa82f8066adee879bfe1932f13436
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9e8d9e9803f74368d9379c3b876e816ae8bb4dbfb8f2357e3cf95717d0dacd66e45e301bfbbaecb7e6e1e517e13abf122fc9e5d72834a8d09bdf65fa117c4766
|
7
|
+
data.tar.gz: 318fa95ea899418e543bf103031bed282c5bd692b43e2f419c039dbc3ea29483490d8552441b174d245b019383de2738ee5356d1898a3f78625bd15bb2ada85c
|
data/lib/sequel_deep_dup.rb
CHANGED
@@ -28,19 +28,21 @@ module Sequel
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def dup_associations instance, copy, includes = nil
|
31
|
-
includes &&= includes
|
31
|
+
includes &&= normalize_graph(includes)
|
32
32
|
associations = instance.class.associations
|
33
33
|
|
34
|
-
|
35
|
-
|
34
|
+
if includes
|
35
|
+
(includes.keys - associations).each do |assoc|
|
36
|
+
raise(Error, "no association named #{assoc} for #{instance}")
|
37
|
+
end
|
36
38
|
end
|
37
39
|
|
38
40
|
associations.each do |name|
|
39
41
|
next unless refl = instance.class.association_reflection(name)
|
40
42
|
[*instance.send(name)].compact.each do |rec|
|
41
43
|
if includes
|
42
|
-
next unless
|
43
|
-
instantiate_associated
|
44
|
+
next unless includes.has_key?( refl_name = refl[:name] )
|
45
|
+
instantiate_associated copy, refl, rec, includes[refl_name]
|
44
46
|
else
|
45
47
|
next copy.values.delete(refl[:key]) if refl[:type] == :many_to_one
|
46
48
|
instantiate_associated(copy, refl, rec, nil)
|
@@ -49,6 +51,17 @@ module Sequel
|
|
49
51
|
end
|
50
52
|
end
|
51
53
|
|
54
|
+
def normalize_graph(*enum)
|
55
|
+
enum.inject({}) do |hash, assoc|
|
56
|
+
case assoc
|
57
|
+
when Symbol then hash[assoc] = {}
|
58
|
+
when Hash then assoc.each { |k, v| hash[k] = normalize_graph(v) }
|
59
|
+
else hash.merge!(normalize_graph(*assoc) || next)
|
60
|
+
end
|
61
|
+
hash
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
52
65
|
private
|
53
66
|
def instantiate_associated copy, reflection, record, associations
|
54
67
|
return if omit_records.detect { |to_omit| record.pk == to_omit.pk && record.class == to_omit.class }
|
data/spec/deep_dup_spec.rb
CHANGED
@@ -18,12 +18,12 @@ describe Sequel::Plugins::DeepDup do
|
|
18
18
|
describe 'duplication plain record' do
|
19
19
|
context 'with regular pk' do
|
20
20
|
it { program_copy.name.should == 'CS' }
|
21
|
-
it { program_copy.pk.should be_nil }
|
21
|
+
it { program_copy.pk.should be_nil }
|
22
22
|
end
|
23
23
|
|
24
24
|
context 'with composite pks' do
|
25
|
-
it { program_copy.should be_new }
|
26
|
-
it { enrollment_copy.pk.should == [nil, nil] }
|
25
|
+
it { program_copy.should be_new }
|
26
|
+
it { enrollment_copy.pk.should == [nil, nil] }
|
27
27
|
it { enrollment_copy.should be_new }
|
28
28
|
end
|
29
29
|
end
|
@@ -119,10 +119,12 @@ describe Sequel::Plugins::DeepDup do
|
|
119
119
|
describe 'restrictions' do
|
120
120
|
before do
|
121
121
|
Program.plugin :deep_dup
|
122
|
+
Course.plugin :deep_dup
|
122
123
|
end
|
123
124
|
|
124
125
|
let!(:program) { create :program, :with_graph }
|
125
126
|
|
127
|
+
|
126
128
|
describe 'validate graph' do
|
127
129
|
it { Course.count.should be 3 }
|
128
130
|
it { Assignment.count.should be 9 }
|
@@ -137,7 +139,7 @@ describe Sequel::Plugins::DeepDup do
|
|
137
139
|
let(:program_copy) { program.deep_dup :courses }
|
138
140
|
it { expect { program_copy.save }.to change{ Course.count }.by(3) }
|
139
141
|
it { expect { program_copy.save }.not_to change{ Enrollment.count } }
|
140
|
-
end
|
142
|
+
end
|
141
143
|
|
142
144
|
describe 'restricts to children of children' do
|
143
145
|
let(:program_copy) { program.deep_dup :courses => :assignments }
|
@@ -158,8 +160,67 @@ describe Sequel::Plugins::DeepDup do
|
|
158
160
|
it { expect { program_copy.save }.not_to change { Category.count } }
|
159
161
|
end
|
160
162
|
|
163
|
+
describe 'allows different graphs for same record with different graph format' do
|
164
|
+
let(:course) { program.courses.first }
|
165
|
+
|
166
|
+
let(:course_copy) { course.deep_dup({:categories => [], :enrollments => {:student => [:profile, :account]}}, :assignments) }
|
167
|
+
it { expect { course_copy.save }.to change { Course.count }.by(1) }
|
168
|
+
it { expect { course_copy.save }.to change { Assignment.count }.by(3) }
|
169
|
+
it { expect { course_copy.save }.to change { Enrollment.count }.by(3) }
|
170
|
+
it { expect { course_copy.save }.to change { Student.count }.by(3) }
|
171
|
+
it { expect { course_copy.save }.to change { Account.count }.by(3) }
|
172
|
+
it { expect { course_copy.save }.to change { Profile.count }.by(3) }
|
173
|
+
it { expect { course_copy.save }.to change { DB[:course_categories].count }.by(3) }
|
174
|
+
it { expect { course_copy.save }.not_to change { Category.count } }
|
175
|
+
end
|
176
|
+
|
177
|
+
describe 'normalizing graph' do
|
178
|
+
let(:dupper) { Sequel::Plugins::DeepDup::DeepDupper.new(nil) }
|
179
|
+
|
180
|
+
it 'maps symbols' do
|
181
|
+
parsed = dupper.normalize_graph( [:course, :assignments, :categories] )
|
182
|
+
parsed.should == {:course => {}, :assignments => {}, :categories => {}}
|
183
|
+
end
|
184
|
+
|
185
|
+
it 'maps symbols followed by hash' do
|
186
|
+
parsed = dupper.normalize_graph( [:course, {:assignments => :categories}] )
|
187
|
+
parsed.should == {:course => {}, :assignments => {:categories => {}}}
|
188
|
+
end
|
189
|
+
|
190
|
+
it 'omits processing a hash' do
|
191
|
+
parsed = dupper.normalize_graph( :assignments => [], :categories => [], :enrollments => [:student] )
|
192
|
+
parsed.should == { :assignments => {}, :categories => {}, :enrollments => {:student => {}} }
|
193
|
+
end
|
194
|
+
|
195
|
+
it 'maps array of symbol and hashes' do
|
196
|
+
parsed = dupper.normalize_graph( [:assignments, {:categories => [], :enrollments => [:student]}] )
|
197
|
+
parsed.should == {:assignments => {}, :categories => {}, :enrollments => {:student => {}}}
|
198
|
+
end
|
199
|
+
|
200
|
+
it 'maps array of symbol and hashes with nested assoc array' do
|
201
|
+
parsed = dupper.normalize_graph( [:assignments, {:enrollments => [:student, :course]}, :tags] )
|
202
|
+
parsed.should == {:assignments => {}, :enrollments => {:student => {}, :course => {}}, :tags => {}}
|
203
|
+
end
|
204
|
+
|
205
|
+
it 'maps nested hashes' do
|
206
|
+
parsed = dupper.normalize_graph([{:courses => [:assignments, {:enrollments => {:student => [:profile, :account]}}, :categories]}])
|
207
|
+
parsed.should == {
|
208
|
+
:courses => {
|
209
|
+
:assignments => {},
|
210
|
+
:enrollments => {
|
211
|
+
:student => {
|
212
|
+
:profile => {},
|
213
|
+
:account => {}
|
214
|
+
}
|
215
|
+
},
|
216
|
+
:categories => {}
|
217
|
+
}
|
218
|
+
}
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
161
222
|
it 'raises exception when association present in graph is not defined in model' do
|
162
|
-
expect { program.deep_dup :potatoes }.to raise_error(Sequel::Error)
|
223
|
+
expect { program.deep_dup :potatoes }.to raise_error(Sequel::Error)
|
163
224
|
end
|
164
225
|
end
|
165
226
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sequel_deep_dup
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- macario
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|