cache_crispies 1.0.1 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/cache_crispies/attribute.rb +8 -4
- data/lib/cache_crispies/base.rb +4 -2
- data/lib/cache_crispies/controller.rb +25 -9
- data/lib/cache_crispies/hash_builder.rb +4 -2
- data/lib/cache_crispies/plan.rb +4 -5
- data/lib/cache_crispies/version.rb +1 -1
- data/spec/cache_crispies/base_spec.rb +20 -0
- data/spec/cache_crispies/controller_spec.rb +31 -1
- data/spec/cache_crispies/hash_builder_spec.rb +1 -2
- data/spec/cache_crispies/plan_spec.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 35d549e631ed6401a8d401b28e77f192ffc0661ed7ad4afd5f6e70b7e85770a0
|
4
|
+
data.tar.gz: 2b118175b85d5d5be47c43c8c9359be7dd496502c056ec5694fb2cbad7778d04
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a3279adc9b9e20800159b2cdcf0aaa173d58d030d22027dcb62818b3fafa12aa2ed5776f824261daf184d8a83cb011e74f617d71bd2d36d5e16aa46ce7446ed0
|
7
|
+
data.tar.gz: '0030582a6e0bc880adff6e350a0f38412dc04f40e04a902ef0aeb5b93d2f6eb5206e6d0960ed414e978d3c3604a789c0d199a93d95cf3da85f4fcc463ba70029'
|
@@ -14,6 +14,7 @@ module CacheCrispies
|
|
14
14
|
# @param from [Symbol] the method on the model to call to get the value
|
15
15
|
# @param with [CacheCrispies::Base] a serializer to use to serialize the
|
16
16
|
# @param to [Class, Symbol] the data type to coerce the value into
|
17
|
+
# @param collection [Boolean] force rendering as single or collection
|
17
18
|
# @param nesting [Array<Symbol>] the JSON keys this attribute will be
|
18
19
|
# nested inside
|
19
20
|
# @param conditions [Array<CacheCrispies::Condition>] the show_if condition
|
@@ -22,7 +23,8 @@ module CacheCrispies
|
|
22
23
|
# argument's value
|
23
24
|
def initialize(
|
24
25
|
key,
|
25
|
-
from: nil, with: nil, through: nil, to: nil,
|
26
|
+
from: nil, with: nil, through: nil, to: nil, collection: nil,
|
27
|
+
nesting: [], conditions: [],
|
26
28
|
&block
|
27
29
|
)
|
28
30
|
@key = key
|
@@ -30,6 +32,7 @@ module CacheCrispies
|
|
30
32
|
@serializer = with
|
31
33
|
@through = through
|
32
34
|
@coerce_to = to
|
35
|
+
@collection = collection
|
33
36
|
@nesting = Array(nesting)
|
34
37
|
@conditions = Array(conditions)
|
35
38
|
@block = block
|
@@ -41,6 +44,7 @@ module CacheCrispies
|
|
41
44
|
:serializer,
|
42
45
|
:through,
|
43
46
|
:coerce_to,
|
47
|
+
:collection,
|
44
48
|
:nesting,
|
45
49
|
:conditions,
|
46
50
|
:block
|
@@ -66,8 +70,6 @@ module CacheCrispies
|
|
66
70
|
serializer ? serialize(value, options) : coerce(value)
|
67
71
|
end
|
68
72
|
|
69
|
-
|
70
|
-
|
71
73
|
private
|
72
74
|
|
73
75
|
def through?
|
@@ -81,7 +83,9 @@ module CacheCrispies
|
|
81
83
|
# Here we'll render the attribute with a given serializer and attempt to
|
82
84
|
# cache the results for better cache reusability
|
83
85
|
def serialize(value, options)
|
84
|
-
plan = CacheCrispies::Plan.new(
|
86
|
+
plan = CacheCrispies::Plan.new(
|
87
|
+
serializer, value, collection: collection, **options
|
88
|
+
)
|
85
89
|
|
86
90
|
if plan.collection?
|
87
91
|
plan.cache { Collection.new(value, serializer, options).as_json }
|
data/lib/cache_crispies/base.rb
CHANGED
@@ -220,10 +220,11 @@ module CacheCrispies
|
|
220
220
|
|
221
221
|
def self.serialize(
|
222
222
|
*attribute_names,
|
223
|
-
from: nil, with: nil, through: nil, to: nil,
|
223
|
+
from: nil, with: nil, through: nil, to: nil, collection: nil,
|
224
224
|
&block
|
225
225
|
)
|
226
|
-
attribute_names.
|
226
|
+
attribute_names.flat_map do |attrib|
|
227
|
+
attrib = attrib&.to_sym
|
227
228
|
current_nesting = Array(@nesting).dup
|
228
229
|
current_conditions = Array(@conditions).dup
|
229
230
|
|
@@ -234,6 +235,7 @@ module CacheCrispies
|
|
234
235
|
with: with,
|
235
236
|
through: through,
|
236
237
|
to: to,
|
238
|
+
collection: collection,
|
237
239
|
nesting: current_nesting,
|
238
240
|
conditions: current_conditions,
|
239
241
|
&block
|
@@ -16,17 +16,30 @@ module CacheCrispies
|
|
16
16
|
# CacheCrispies::Base
|
17
17
|
# @param cacheable [Object] can be any object. But is typically a Rails
|
18
18
|
# model inheriting from ActiveRecord::Base
|
19
|
-
# @param [
|
20
|
-
# @option options [Symbol] :key the name of the root key to nest the JSON
|
19
|
+
# @param key [Symbol] the name of the root key to nest the JSON
|
21
20
|
# data under
|
22
|
-
# @
|
21
|
+
# @param collection [Boolean] whether to render the data as a
|
23
22
|
# collection/array or a single object
|
24
|
-
# @
|
25
|
-
#
|
23
|
+
# @param status [Integer, Symbol] the HTTP response status code or
|
24
|
+
# Rails-supported symbol. See
|
26
25
|
# https://guides.rubyonrails.org/layouts_and_rendering.html#the-status-option
|
26
|
+
# @param meta [Hash] data to include as metadata under a root key
|
27
|
+
# @param meta_key [Symbol] they key to store the metadata under
|
28
|
+
# @param [Hash] options any optional values from the serializer instance
|
27
29
|
# @return [void]
|
28
|
-
def cache_render(
|
29
|
-
|
30
|
+
def cache_render(
|
31
|
+
serializer,
|
32
|
+
cacheable,
|
33
|
+
key: nil, collection: nil, status: nil,
|
34
|
+
meta: {}, meta_key: :meta,
|
35
|
+
**options
|
36
|
+
)
|
37
|
+
plan = CacheCrispies::Plan.new(
|
38
|
+
serializer,
|
39
|
+
cacheable,
|
40
|
+
key: key, collection: collection,
|
41
|
+
**options
|
42
|
+
)
|
30
43
|
|
31
44
|
if CacheCrispies.config.etags?
|
32
45
|
response.weak_etag = plan.etag
|
@@ -43,8 +56,11 @@ module CacheCrispies
|
|
43
56
|
plan.cache { serializer.new(cacheable, options).as_json }
|
44
57
|
end
|
45
58
|
|
46
|
-
|
47
|
-
|
59
|
+
json_hash = plan.wrap(serializer_json)
|
60
|
+
json_hash[meta_key] = meta if meta.present?
|
61
|
+
|
62
|
+
render_hash = { json: Oj.dump(json_hash, mode: OJ_MODE) }
|
63
|
+
render_hash[:status] = status if status
|
48
64
|
|
49
65
|
render render_hash
|
50
66
|
end
|
@@ -16,18 +16,20 @@ module CacheCrispies
|
|
16
16
|
#
|
17
17
|
# @return [Hash]
|
18
18
|
def call
|
19
|
+
return unless @serializer.model
|
20
|
+
|
19
21
|
hash = {}
|
20
22
|
|
21
23
|
serializer.attributes.each do |attrib|
|
22
24
|
deepest_hash = hash
|
23
25
|
|
26
|
+
next unless show?(attrib)
|
27
|
+
|
24
28
|
attrib.nesting.each do |key|
|
25
29
|
deepest_hash[key] ||= {}
|
26
30
|
deepest_hash = deepest_hash[key]
|
27
31
|
end
|
28
32
|
|
29
|
-
next unless show?(attrib)
|
30
|
-
|
31
33
|
value = value_for(attrib)
|
32
34
|
|
33
35
|
if attrib.key
|
data/lib/cache_crispies/plan.rb
CHANGED
@@ -16,14 +16,13 @@ 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,
|
19
|
+
def initialize(serializer, cacheable, key: nil, collection: nil, **options)
|
20
20
|
@serializer = serializer
|
21
21
|
@cacheable = cacheable
|
22
22
|
|
23
|
-
|
24
|
-
@
|
25
|
-
@
|
26
|
-
@options = opts
|
23
|
+
@key = key
|
24
|
+
@collection = collection
|
25
|
+
@options = options
|
27
26
|
end
|
28
27
|
|
29
28
|
# Whether or not the cacheable should be treated like a collection
|
@@ -69,6 +69,26 @@ describe CacheCrispies::Base do
|
|
69
69
|
parent_company: 'Disney probably'
|
70
70
|
)
|
71
71
|
end
|
72
|
+
|
73
|
+
context 'when nutrition_info is nil' do
|
74
|
+
before { model.nutrition_info = nil }
|
75
|
+
|
76
|
+
it 'serializes to a hash' do
|
77
|
+
expect(subject.as_json).to eq(
|
78
|
+
id: '42',
|
79
|
+
name: 'Cookie Crisp',
|
80
|
+
company: 'General Mills',
|
81
|
+
nested: {
|
82
|
+
nested_again: {
|
83
|
+
deeply_nested: 'TRUE'
|
84
|
+
}
|
85
|
+
},
|
86
|
+
nutrition_info: nil,
|
87
|
+
organic: true,
|
88
|
+
parent_company: 'Disney probably'
|
89
|
+
)
|
90
|
+
end
|
91
|
+
end
|
72
92
|
end
|
73
93
|
|
74
94
|
describe '.key' do
|
@@ -24,7 +24,8 @@ describe CacheCrispies::Controller do
|
|
24
24
|
|
25
25
|
describe '#cache_render' do
|
26
26
|
let(:etags) { false }
|
27
|
-
let(:
|
27
|
+
let(:single_hash) { { cereal: { name: cereal_names.first } } }
|
28
|
+
let(:single_json) { single_hash.to_json }
|
28
29
|
let(:collection_json) {
|
29
30
|
{ cereals: cereal_names.map { |name| { name: name } } }.to_json
|
30
31
|
}
|
@@ -86,5 +87,34 @@ describe CacheCrispies::Controller do
|
|
86
87
|
)
|
87
88
|
end
|
88
89
|
end
|
90
|
+
|
91
|
+
context 'with a meta: option' do
|
92
|
+
it 'adds a meta data hash to the JSON' do
|
93
|
+
expect(subject).to receive(:render).with(
|
94
|
+
json: single_hash.merge(meta: { page: 42 }).to_json
|
95
|
+
)
|
96
|
+
|
97
|
+
subject.cache_render(
|
98
|
+
CerealSerializerForController,
|
99
|
+
collection.first,
|
100
|
+
meta: { page: 42 }
|
101
|
+
)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
context 'with a meta_key: option' do
|
106
|
+
it 'adds a meta data hash to the JSON with the provided key' do
|
107
|
+
expect(subject).to receive(:render).with(
|
108
|
+
json: single_hash.merge(test_meta_data: { page: 42 }).to_json
|
109
|
+
)
|
110
|
+
|
111
|
+
subject.cache_render(
|
112
|
+
CerealSerializerForController,
|
113
|
+
collection.first,
|
114
|
+
meta: { page: 42 },
|
115
|
+
meta_key: :test_meta_data
|
116
|
+
)
|
117
|
+
end
|
118
|
+
end
|
89
119
|
end
|
90
120
|
end
|
@@ -20,7 +20,7 @@ describe CacheCrispies::Plan do
|
|
20
20
|
let(:model) { OpenStruct.new(name: 'Sugar Smacks', cache_key: model_cache_key) }
|
21
21
|
let(:cacheable) { model }
|
22
22
|
let(:options) { {} }
|
23
|
-
let(:instance) { described_class.new(serializer, cacheable, options) }
|
23
|
+
let(:instance) { described_class.new(serializer, cacheable, **options) }
|
24
24
|
subject { instance }
|
25
25
|
|
26
26
|
before do
|
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.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Crownoble
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-10-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -134,7 +134,7 @@ dependencies:
|
|
134
134
|
- - "~>"
|
135
135
|
- !ruby/object:Gem::Version
|
136
136
|
version: '0.17'
|
137
|
-
description:
|
137
|
+
description:
|
138
138
|
email: adam@codenoble.com
|
139
139
|
executables: []
|
140
140
|
extensions: []
|
@@ -168,7 +168,7 @@ homepage: https://github.com/codenoble/cache-crispies
|
|
168
168
|
licenses:
|
169
169
|
- MIT
|
170
170
|
metadata: {}
|
171
|
-
post_install_message:
|
171
|
+
post_install_message:
|
172
172
|
rdoc_options: []
|
173
173
|
require_paths:
|
174
174
|
- lib
|
@@ -184,7 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
184
184
|
version: '0'
|
185
185
|
requirements: []
|
186
186
|
rubygems_version: 3.0.3
|
187
|
-
signing_key:
|
187
|
+
signing_key:
|
188
188
|
specification_version: 4
|
189
189
|
summary: Fast Rails serializer with built-in caching
|
190
190
|
test_files:
|