cache_crispies 1.3.1 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d6f64a24131b351a6748e8640f8745ade2ae3d84d14403fdd83e765a252adfe6
4
- data.tar.gz: 5ba42a9d345bdd03c5849d2af1d050b9561a83d3e4b2ce658e101ab3ada9b1a5
3
+ metadata.gz: 0b0cd37045a506df4c5268133eef6566c7ec269fc48eaec6721fcfa74e133416
4
+ data.tar.gz: b4985e62682465523ac4caec43b7539c676353d1ef5a2003e06b3060093007ce
5
5
  SHA512:
6
- metadata.gz: fbd76029802fab2b03950f05ef4d778c50c854ced9c974dd15d8458627d8e157af34fc61bb38898817a4c593d2a9c19d7206b6991e9d7766a3414c4a9296b88e
7
- data.tar.gz: e9e61f6afbf49b8cbd189812041b5648f8a9a02c373a11acceea0b206b0d0a91eea13b5cbfb5bcde0909ab24b2a8f3c5e94a12767297b58d02e6caa76c1ce65b
6
+ metadata.gz: cb6b86cda738d0fc7116fa8df04888951d37f89c0017f393df7645e681fb9b15d9acf4bbca80306f56c5f26bf6d08d8915e72cf737bd1bc28423d152d9ce57db
7
+ data.tar.gz: 83efc432240566b128620748fcda6eb63aaee98fcf4773c072facbda4feb3b93159a19376fb0ef069c86d99afdd10f66ff2c34c27e87e73366f0f60c2b5e6d62
@@ -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
- Rails.root.join(*parts)
208
+ engine.root.join(*parts)
194
209
  end
195
210
  end
196
211
  private_class_method :path
@@ -41,7 +41,7 @@ module CacheCrispies
41
41
 
42
42
  def cached_json
43
43
  models_by_cache_key = collection.each_with_object({}) do |model, hash|
44
- plan = Plan.new(serializer, model, options)
44
+ plan = Plan.new(serializer, model, **options)
45
45
 
46
46
  hash[plan.cache_key] = model
47
47
  end
@@ -30,7 +30,7 @@ module CacheCrispies
30
30
  def cache_render(
31
31
  serializer,
32
32
  cacheable,
33
- key: nil, collection: nil, status: nil,
33
+ key: UNDEFINED, collection: nil, status: nil,
34
34
  meta: {}, meta_key: :meta,
35
35
  **options
36
36
  )
@@ -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: nil, collection: nil, **options)
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.nil?
94
+ return @key unless @key == UNDEFINED
95
95
 
96
96
  (collection? ? serializer.collection_key : serializer.key)
97
97
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module CacheCrispies
4
4
  # The version of the gem
5
- VERSION = '1.3.1'
5
+ VERSION = '1.4.0'
6
6
  end
@@ -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
@@ -58,11 +58,11 @@ describe CacheCrispies::Collection do
58
58
 
59
59
  it 'fetches the cache for each object in the collection' do
60
60
  expect(CacheCrispies::Plan).to receive(:new).with(
61
- serializer, model1, options
61
+ serializer, model1
62
62
  ).and_return double('plan-dbl-1', cache_key: 'cereal-key-1')
63
63
 
64
64
  expect(CacheCrispies::Plan).to receive(:new).with(
65
- serializer, model2, options
65
+ serializer, model2
66
66
  ).and_return double('plan-dbl-2', cache_key: 'cereal-key-2')
67
67
 
68
68
  expect(CacheCrispies).to receive_message_chain(
@@ -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(
@@ -0,0 +1,3 @@
1
+ # Nothing has to actually be in this file.
2
+ # It's just here so test can MD5 sum it's content.
3
+ # This is an alternative serializer file.
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.3.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: 2021-07-02 00:00:00.000000000 Z
11
+ date: 2022-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: 5.0.0
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '7.0'
22
+ version: '8.0'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: 5.0.0
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '7.0'
32
+ version: '8.0'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: oj
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -53,7 +53,7 @@ dependencies:
53
53
  version: 5.0.0
54
54
  - - "<"
55
55
  - !ruby/object:Gem::Version
56
- version: '7.0'
56
+ version: '8.0'
57
57
  type: :development
58
58
  prerelease: false
59
59
  version_requirements: !ruby/object:Gem::Requirement
@@ -63,7 +63,7 @@ dependencies:
63
63
  version: 5.0.0
64
64
  - - "<"
65
65
  - !ruby/object:Gem::Version
66
- version: '7.0'
66
+ version: '8.0'
67
67
  - !ruby/object:Gem::Dependency
68
68
  name: appraisal
69
69
  requirement: !ruby/object:Gem::Requirement
@@ -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
@@ -198,7 +199,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
198
199
  - !ruby/object:Gem::Version
199
200
  version: '0'
200
201
  requirements: []
201
- rubygems_version: 3.2.15
202
+ rubygems_version: 3.3.7
202
203
  signing_key:
203
204
  specification_version: 4
204
205
  summary: Fast Rails serializer with built-in caching
@@ -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