data_broker 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/data_broker/mapper.rb +20 -10
- data/lib/data_broker/version.rb +1 -1
- data/spec/mapper_spec.rb +41 -0
- 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: efa47ef957de906755498b6ae0f153e142364672
|
4
|
+
data.tar.gz: 3d18d6b223a291474a1a75375dca874112cf927f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c0bd2f321438ba7e89a3b87f6f96ee23c63f347e5f186129744ec9177376b42ce6ded419f2296a2c0315194116266f3c9c514f00b28b271c0da987d11a39fbac
|
7
|
+
data.tar.gz: f4d8e9924a6d49cf3138b34b2c41b2a072cd8d7415154504c8aea0d72a25f00398427016e703bcab311c59c16ed5ecf3e2d2aef2dfc5e4d22b8a4a68a44f4923
|
data/lib/data_broker/mapper.rb
CHANGED
@@ -37,33 +37,43 @@ module DataBroker
|
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
-
def all
|
41
|
-
records_to_objects(self.record.all)
|
40
|
+
def all(eager_load: false)
|
41
|
+
records_to_objects(self.record.all, :eager_load => eager_load)
|
42
42
|
end
|
43
43
|
|
44
|
-
def find(id)
|
44
|
+
def find(id, eager_load: true)
|
45
45
|
record = self.eager_record.find_by(:id => id)
|
46
|
-
record.nil?
|
46
|
+
if record.nil?
|
47
|
+
nil
|
48
|
+
else
|
49
|
+
records_to_objects(record, :eager_load => eager_load).first
|
50
|
+
end
|
47
51
|
end
|
48
52
|
|
49
|
-
def where(conds)
|
53
|
+
def where(eager_load: true, **conds)
|
50
54
|
conds = Hash[conds.map { |key, value| [self.mapping[key], value] }]
|
51
|
-
records_to_objects(
|
55
|
+
records_to_objects(
|
56
|
+
self.eager_record.where(conds), :eager_load => eager_load
|
57
|
+
)
|
52
58
|
end
|
53
59
|
|
54
|
-
def save(model)
|
60
|
+
def save(model, eager_load: true)
|
55
61
|
record = self.eager_record.find_by(:id => model.id) || self.record.new
|
56
62
|
attributes = Hash[self.mapping.map { |key, value| [value, model[key]] }]
|
57
63
|
attributes.delete(:id)
|
58
64
|
record.attributes = attributes
|
59
65
|
|
60
66
|
record.save!
|
61
|
-
records_to_objects(record.reload, :eager_load =>
|
67
|
+
records_to_objects(record.reload, :eager_load => eager_load).first
|
62
68
|
end
|
63
69
|
|
64
|
-
def destroy(model)
|
70
|
+
def destroy(model, eager_load: false)
|
65
71
|
record = self.record.find_by(:id => model.id)
|
66
|
-
record.nil?
|
72
|
+
if record.nil?
|
73
|
+
nil
|
74
|
+
else
|
75
|
+
records_to_objects(record.destroy, :eager_load => false).first
|
76
|
+
end
|
67
77
|
end
|
68
78
|
|
69
79
|
def records_to_objects(records, eager_load: false)
|
data/lib/data_broker/version.rb
CHANGED
data/spec/mapper_spec.rb
CHANGED
@@ -88,6 +88,14 @@ describe DataBroker::Mapper do
|
|
88
88
|
|
89
89
|
expect(obj).to be_a(mapper.model)
|
90
90
|
end
|
91
|
+
|
92
|
+
it "eager loads if requested" do
|
93
|
+
3.times { mapper.record.create }
|
94
|
+
expect(mapper).to receive(:load_parents!).exactly(3).times
|
95
|
+
expect(mapper).to receive(:load_children!).once
|
96
|
+
|
97
|
+
mapper.all(:eager_load => true)
|
98
|
+
end
|
91
99
|
end
|
92
100
|
|
93
101
|
describe "#find" do
|
@@ -130,6 +138,17 @@ describe DataBroker::Mapper do
|
|
130
138
|
ids = obj.instance_variable_get(:@child_ids)
|
131
139
|
expect(ids).to match_array([id])
|
132
140
|
end
|
141
|
+
|
142
|
+
it "doesn't eager load if requested" do
|
143
|
+
expect(parent_mapper).to_not receive(:load_children!)
|
144
|
+
expect(parent_mapper).to_not receive(:load_parents!)
|
145
|
+
|
146
|
+
parent_id = parent_mapper.record.create.id
|
147
|
+
mapper.record.create(
|
148
|
+
:child_name => "Bobby", :parent_id => parent_id
|
149
|
+
).id
|
150
|
+
parent_mapper.find(parent_id, :eager_load => false)
|
151
|
+
end
|
133
152
|
end
|
134
153
|
|
135
154
|
describe "#where" do
|
@@ -186,6 +205,16 @@ describe DataBroker::Mapper do
|
|
186
205
|
ids = obj.instance_variable_get(:@child_ids)
|
187
206
|
expect(ids).to match_array([id])
|
188
207
|
end
|
208
|
+
|
209
|
+
it "doesn't eager load if requested" do
|
210
|
+
expect(parent_mapper).to_not receive(:load_children!)
|
211
|
+
expect(parent_mapper).to_not receive(:load_parents!)
|
212
|
+
parent_id = parent_mapper.record.create.id
|
213
|
+
mapper.record.create(
|
214
|
+
:child_name => "Bobby", :parent_id => parent_id
|
215
|
+
).id
|
216
|
+
parent_mapper.where(:id => parent_id, :eager_load => false).first
|
217
|
+
end
|
189
218
|
end
|
190
219
|
|
191
220
|
describe "#save" do
|
@@ -210,6 +239,12 @@ describe DataBroker::Mapper do
|
|
210
239
|
|
211
240
|
expect(rt.id).to eq(model.id)
|
212
241
|
end
|
242
|
+
|
243
|
+
it "doesn't eager load if requested" do
|
244
|
+
expect(mapper).to_not receive(:load_children!)
|
245
|
+
expect(mapper).to_not receive(:load_parents!)
|
246
|
+
mapper.save(mapper.model.new, :eager_load => false)
|
247
|
+
end
|
213
248
|
end
|
214
249
|
|
215
250
|
describe "#destroy" do
|
@@ -231,5 +266,11 @@ describe DataBroker::Mapper do
|
|
231
266
|
|
232
267
|
expect(mapper.find(model.id)).to be_nil
|
233
268
|
end
|
269
|
+
|
270
|
+
it "eager loads if requests" do
|
271
|
+
expect(mapper).to receive(:load_children!).once
|
272
|
+
model = mapper.save(mapper.model.new)
|
273
|
+
mapper.destroy(model, :eager_load => true)
|
274
|
+
end
|
234
275
|
end
|
235
276
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_broker
|
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
|
- Shane Emmons
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: virtus
|