cache_crispies 1.3.2 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cache_crispies/base.rb +16 -1
- data/lib/cache_crispies/controller.rb +1 -1
- data/lib/cache_crispies/plan.rb +2 -2
- data/lib/cache_crispies/version.rb +1 -1
- data/lib/cache_crispies.rb +3 -0
- data/spec/cache_crispies/base_spec.rb +43 -0
- data/spec/cache_crispies/controller_spec.rb +30 -0
- data/spec/fixtures/engine_test_serializer.rb +3 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0b0cd37045a506df4c5268133eef6566c7ec269fc48eaec6721fcfa74e133416
|
4
|
+
data.tar.gz: b4985e62682465523ac4caec43b7539c676353d1ef5a2003e06b3060093007ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb6b86cda738d0fc7116fa8df04888951d37f89c0017f393df7645e681fb9b15d9acf4bbca80306f56c5f26bf6d08d8915e72cf737bd1bc28423d152d9ce57db
|
7
|
+
data.tar.gz: 83efc432240566b128620748fcda6eb63aaee98fcf4773c072facbda4feb3b93159a19376fb0ef069c86d99afdd10f66ff2c34c27e87e73366f0f60c2b5e6d62
|
data/lib/cache_crispies/base.rb
CHANGED
@@ -64,6 +64,21 @@ module CacheCrispies
|
|
64
64
|
alias do_caching? do_caching
|
65
65
|
end
|
66
66
|
|
67
|
+
# Get or set root path of Rails application or engine.
|
68
|
+
# It uses Rails by default, but can be overriden with your Rails Engine class.
|
69
|
+
# Calling the method with an argument will set the value, calling it without
|
70
|
+
# any arguments will get the value.
|
71
|
+
def self.engine(value = nil)
|
72
|
+
@engine ||= superclass.try(:engine)
|
73
|
+
@engine ||= Rails
|
74
|
+
|
75
|
+
# method called with no args so act as a getter
|
76
|
+
return @engine if value.nil?
|
77
|
+
|
78
|
+
# method called with args so act as a setter
|
79
|
+
@engine = value
|
80
|
+
end
|
81
|
+
|
67
82
|
# Get or set a JSON key to use as a root key on a non-collection
|
68
83
|
# serializable. By default it's the name of the class without the
|
69
84
|
# "Serializer" part. But it can be overridden in a subclass to be anything.
|
@@ -190,7 +205,7 @@ module CacheCrispies
|
|
190
205
|
parts = %w[app serializers]
|
191
206
|
parts += to_s.deconstantize.split('::').map(&:underscore)
|
192
207
|
parts << "#{to_s.demodulize.underscore}.rb"
|
193
|
-
|
208
|
+
engine.root.join(*parts)
|
194
209
|
end
|
195
210
|
end
|
196
211
|
private_class_method :path
|
data/lib/cache_crispies/plan.rb
CHANGED
@@ -16,7 +16,7 @@ module CacheCrispies
|
|
16
16
|
# data under
|
17
17
|
# @option options [Boolean] :collection whether to render the data as a
|
18
18
|
# collection/array or a single object
|
19
|
-
def initialize(serializer, cacheable, key:
|
19
|
+
def initialize(serializer, cacheable, key: UNDEFINED, collection: nil, **options)
|
20
20
|
@serializer = serializer
|
21
21
|
@cacheable = cacheable
|
22
22
|
|
@@ -91,7 +91,7 @@ module CacheCrispies
|
|
91
91
|
private
|
92
92
|
|
93
93
|
def key
|
94
|
-
return @key unless @key
|
94
|
+
return @key unless @key == UNDEFINED
|
95
95
|
|
96
96
|
(collection? ? serializer.collection_key : serializer.key)
|
97
97
|
end
|
data/lib/cache_crispies.rb
CHANGED
@@ -12,6 +12,9 @@ module CacheCrispies
|
|
12
12
|
# The string to use to join parts of the cache keys together
|
13
13
|
CACHE_KEY_SEPARATOR = '+'
|
14
14
|
|
15
|
+
# Magic value for undefined arguments
|
16
|
+
UNDEFINED = Object.new.freeze
|
17
|
+
|
15
18
|
require 'cache_crispies/version'
|
16
19
|
|
17
20
|
# Use autoload for better Rails development
|
@@ -39,6 +39,15 @@ describe CacheCrispies::Base do
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
+
class MyEngine
|
43
|
+
def self.root
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class CacheCrispiesEngineTestSerializer < CacheCrispiesTestSerializer
|
48
|
+
engine MyEngine
|
49
|
+
end
|
50
|
+
|
42
51
|
let(:model) do
|
43
52
|
OpenStruct.new(
|
44
53
|
id: 42,
|
@@ -52,6 +61,7 @@ describe CacheCrispies::Base do
|
|
52
61
|
end
|
53
62
|
|
54
63
|
let(:serializer) { CacheCrispiesTestSerializer }
|
64
|
+
let(:engine_serializer) { CacheCrispiesEngineTestSerializer }
|
55
65
|
let(:instance) { serializer.new(model) }
|
56
66
|
subject { instance }
|
57
67
|
|
@@ -161,6 +171,24 @@ describe CacheCrispies::Base do
|
|
161
171
|
end
|
162
172
|
end
|
163
173
|
|
174
|
+
describe '.engine' do
|
175
|
+
it 'defaults to Rails' do
|
176
|
+
expect(serializer.engine).to eq Rails
|
177
|
+
end
|
178
|
+
|
179
|
+
context 'when called with an argument' do
|
180
|
+
after { serializer.remove_instance_variable :@engine }
|
181
|
+
|
182
|
+
it 'sets and returns a value' do
|
183
|
+
expect {
|
184
|
+
serializer.engine MyEngine
|
185
|
+
}.to change {
|
186
|
+
serializer.engine
|
187
|
+
}.from(Rails).to MyEngine
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
164
192
|
describe '.cache_key_addons' do
|
165
193
|
it 'returns an empty array by default' do
|
166
194
|
expect(serializer.cache_key_addons).to eq []
|
@@ -203,6 +231,12 @@ describe CacheCrispies::Base do
|
|
203
231
|
let(:serializer_file_digest) {
|
204
232
|
Digest::MD5.file(serializer_file_path).to_s
|
205
233
|
}
|
234
|
+
let(:engine_serializer_file_path) {
|
235
|
+
File.expand_path('../fixtures/engine_test_serializer.rb', __dir__)
|
236
|
+
}
|
237
|
+
let(:engine_serializer_file_digest) {
|
238
|
+
Digest::MD5.file(engine_serializer_file_path).to_s
|
239
|
+
}
|
206
240
|
|
207
241
|
before do
|
208
242
|
allow(NutritionSerializer).to receive(:file_hash).and_return(
|
@@ -211,6 +245,9 @@ describe CacheCrispies::Base do
|
|
211
245
|
allow(Rails).to receive_message_chain(:root, :join).and_return(
|
212
246
|
serializer_file_path
|
213
247
|
)
|
248
|
+
allow(MyEngine).to receive_message_chain(:root, :join).and_return(
|
249
|
+
engine_serializer_file_path
|
250
|
+
)
|
214
251
|
end
|
215
252
|
|
216
253
|
it 'includes the file name' do
|
@@ -230,6 +267,12 @@ describe CacheCrispies::Base do
|
|
230
267
|
"#{serializer}-#{serializer_file_digest}+#{nested_serializer_digest}"
|
231
268
|
)
|
232
269
|
end
|
270
|
+
|
271
|
+
context 'when an engine is set' do
|
272
|
+
it "includes a digest of the serializer class file's contents" do
|
273
|
+
expect(engine_serializer.cache_key_base).to include engine_serializer_file_digest
|
274
|
+
end
|
275
|
+
end
|
233
276
|
end
|
234
277
|
|
235
278
|
describe '.attributes' do
|
@@ -73,6 +73,36 @@ describe CacheCrispies::Controller do
|
|
73
73
|
subject.cache_render CerealSerializerForController, collection.first
|
74
74
|
end
|
75
75
|
|
76
|
+
context 'with a key: option'do
|
77
|
+
context 'overriding the key in the serializer' do
|
78
|
+
it 'renders a json collection' do
|
79
|
+
expect(subject).to receive(:render).with json: { serials: cereal_names.map { |name| { name: name } } }.to_json
|
80
|
+
|
81
|
+
subject.cache_render CerealSerializerForController, collection, key: "serials"
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'renders a single json object' do
|
85
|
+
expect(subject).to receive(:render).with json: { serial: { name: cereal_names.first} }.to_json
|
86
|
+
|
87
|
+
subject.cache_render CerealSerializerForController, collection.first, key: "serial"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
context 'set to nil' do
|
92
|
+
it 'renders a json collection' do
|
93
|
+
expect(subject).to receive(:render).with json: cereal_names.map { |name| { name: name } }.to_json
|
94
|
+
|
95
|
+
subject.cache_render CerealSerializerForController, collection, key: nil
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'renders a single json object' do
|
99
|
+
expect(subject).to receive(:render).with json: { name: cereal_names.first }.to_json
|
100
|
+
|
101
|
+
subject.cache_render CerealSerializerForController, collection.first, key: nil
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
76
106
|
context 'with a status: option' do
|
77
107
|
it 'passes the status option to the Rails render call' do
|
78
108
|
expect(subject).to receive(:render).with(
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cache_crispies
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Crownoble
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -177,6 +177,7 @@ files:
|
|
177
177
|
- spec/cache_crispies/memoizer_spec.rb
|
178
178
|
- spec/cache_crispies/plan_spec.rb
|
179
179
|
- spec/cache_crispies_spec.rb
|
180
|
+
- spec/fixtures/engine_test_serializer.rb
|
180
181
|
- spec/fixtures/test_serializer.rb
|
181
182
|
- spec/spec_helper.rb
|
182
183
|
homepage: https://github.com/codenoble/cache-crispies
|
@@ -213,5 +214,6 @@ test_files:
|
|
213
214
|
- spec/cache_crispies/memoizer_spec.rb
|
214
215
|
- spec/cache_crispies/plan_spec.rb
|
215
216
|
- spec/cache_crispies_spec.rb
|
217
|
+
- spec/fixtures/engine_test_serializer.rb
|
216
218
|
- spec/fixtures/test_serializer.rb
|
217
219
|
- spec/spec_helper.rb
|