thermos 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/thermos/beverage.rb +31 -4
- data/lib/thermos/dependency.rb +5 -6
- data/lib/thermos/version.rb +1 -1
- data/test/thermos_test.rb +24 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 87ceed3d0f1b54b2cd9e9b0afde15e90252fcbc9f575c5bf11a66e414045c5d5
|
4
|
+
data.tar.gz: dc3bc76cd808f474b80676c5f2d1f2e50681c6028c09412e12eb565b4494cfc4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d86ada3f6a3b07ed8bbf35c481e73ac4563a0aced184f6e47a986847287f0bf4a3c02c42e5d7600db1f5d8c2358f5ba4975fd20c4ccceee8fc3ec0cfcaae7811
|
7
|
+
data.tar.gz: 51f90cb754d381d81293e433261e09cb4e184dc1dcddd05fbfcbf7baacb577330f6a4cc6a0621cc8cd9964d7677e499554a8dd533d65762adb421d56cef58458
|
data/lib/thermos/beverage.rb
CHANGED
@@ -9,9 +9,7 @@ module Thermos
|
|
9
9
|
@model = model
|
10
10
|
@lookup_key = lookup_key || :id
|
11
11
|
@filter = filter || nil
|
12
|
-
@deps = deps
|
13
|
-
Dependency.new(model: model, association: dep)
|
14
|
-
end
|
12
|
+
@deps = generate_deps(model, deps)
|
15
13
|
@action = action
|
16
14
|
|
17
15
|
set_observers
|
@@ -21,7 +19,7 @@ module Thermos
|
|
21
19
|
@deps.select do |dep|
|
22
20
|
dep.klass == dep_model.class
|
23
21
|
end.flat_map do |dep|
|
24
|
-
@model.joins(dep.
|
22
|
+
@model.joins(dep.path)
|
25
23
|
.where(dep.table => { id: dep_model.id })
|
26
24
|
.pluck(@lookup_key)
|
27
25
|
end.uniq
|
@@ -41,5 +39,34 @@ module Thermos
|
|
41
39
|
def observe(model)
|
42
40
|
model.include(Notifier) unless model.included_modules.include?(Notifier)
|
43
41
|
end
|
42
|
+
|
43
|
+
def generate_deps(model, deps, root = nil, path = nil)
|
44
|
+
deps.reduce([]) do |acc, dep|
|
45
|
+
if dep.is_a? Symbol
|
46
|
+
acc << Dependency.new(
|
47
|
+
model: root || model,
|
48
|
+
ref: model.reflect_on_association(dep),
|
49
|
+
path: path || dep)
|
50
|
+
elsif dep.is_a? Array
|
51
|
+
dep.each do |d|
|
52
|
+
acc << Dependency.new(
|
53
|
+
model: root || model,
|
54
|
+
ref: model.reflect_on_association(d),
|
55
|
+
path: path || d)
|
56
|
+
end
|
57
|
+
elsif dep.is_a? Hash
|
58
|
+
dep.each do |k,v|
|
59
|
+
ref = model.reflect_on_association(k)
|
60
|
+
acc << Dependency.new(
|
61
|
+
model: root || model,
|
62
|
+
ref: ref,
|
63
|
+
path: path || k
|
64
|
+
)
|
65
|
+
acc.concat(generate_deps(ref.class_name.constantize, v, model, dep))
|
66
|
+
end
|
67
|
+
end
|
68
|
+
acc
|
69
|
+
end
|
70
|
+
end
|
44
71
|
end
|
45
72
|
end
|
data/lib/thermos/dependency.rb
CHANGED
@@ -2,14 +2,13 @@
|
|
2
2
|
|
3
3
|
module Thermos
|
4
4
|
class Dependency
|
5
|
-
attr_reader :model, :
|
5
|
+
attr_reader :model, :path, :klass, :table
|
6
6
|
|
7
|
-
def initialize(model:,
|
7
|
+
def initialize(model:, ref:, path: nil)
|
8
8
|
@model = model
|
9
|
-
@
|
10
|
-
|
11
|
-
@
|
12
|
-
@klass = reflection.class_name.constantize
|
9
|
+
@path = path
|
10
|
+
@table = ref.table_name
|
11
|
+
@klass = ref.class_name.constantize
|
13
12
|
end
|
14
13
|
end
|
15
14
|
end
|
data/lib/thermos/version.rb
CHANGED
data/test/thermos_test.rb
CHANGED
@@ -331,6 +331,30 @@ class ThermosTest < ActiveSupport::TestCase
|
|
331
331
|
assert_raises(MockExpectationError) { mock.verify }
|
332
332
|
end
|
333
333
|
|
334
|
+
test "handles indirect associations" do
|
335
|
+
mock = Minitest::Mock.new
|
336
|
+
category = categories(:baseball)
|
337
|
+
store = category.store
|
338
|
+
|
339
|
+
Thermos.fill(key: "key", model: Store, deps: [categories: [:products]]) do |id|
|
340
|
+
mock.call(id)
|
341
|
+
end
|
342
|
+
|
343
|
+
mock.expect(:call, 1, [store.id])
|
344
|
+
category.update!(name: "foo")
|
345
|
+
mock.verify
|
346
|
+
|
347
|
+
mock.expect(:call, 2, [store.id])
|
348
|
+
assert_equal 1, Thermos.drink(key: "key", id: store.id)
|
349
|
+
assert_raises(MockExpectationError) { mock.verify }
|
350
|
+
Product.create!(categories: [category])
|
351
|
+
mock.verify
|
352
|
+
|
353
|
+
mock.expect(:call, 3, [store.id])
|
354
|
+
assert_equal 2, Thermos.drink(key: "key", id: store.id)
|
355
|
+
assert_raises(MockExpectationError) { mock.verify }
|
356
|
+
end
|
357
|
+
|
334
358
|
test "only rebuilds cache for stated dependencies, even if another cache has an associated model of the primary" do
|
335
359
|
category_mock = Minitest::Mock.new
|
336
360
|
product_mock = Minitest::Mock.new
|