cache_crispies 0.3.1 → 1.0.1
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.rb +21 -8
- data/lib/cache_crispies/attribute.rb +31 -7
- data/lib/cache_crispies/base.rb +100 -31
- data/lib/cache_crispies/collection.rb +2 -4
- data/lib/cache_crispies/configuration.rb +28 -0
- data/lib/cache_crispies/controller.rb +7 -5
- data/lib/cache_crispies/plan.rb +2 -1
- data/lib/cache_crispies/version.rb +1 -1
- data/spec/{attribute_spec.rb → cache_crispies/attribute_spec.rb} +29 -0
- data/spec/cache_crispies/base_spec.rb +289 -0
- data/spec/{collection_spec.rb → cache_crispies/collection_spec.rb} +31 -21
- data/spec/{condition_spec.rb → cache_crispies/condition_spec.rb} +0 -0
- data/spec/cache_crispies/configuration_spec.rb +42 -0
- data/spec/cache_crispies/controller_spec.rb +90 -0
- data/spec/{hash_builder_spec.rb → cache_crispies/hash_builder_spec.rb} +0 -0
- data/spec/{memoizer_spec.rb → cache_crispies/memoizer_spec.rb} +0 -0
- data/spec/{plan_spec.rb → cache_crispies/plan_spec.rb} +12 -7
- data/spec/cache_crispies_spec.rb +27 -0
- metadata +49 -24
- data/spec/base_spec.rb +0 -186
- data/spec/controller_spec.rb +0 -69
File without changes
|
File without changes
|
@@ -2,11 +2,10 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe CacheCrispies::Plan do
|
4
4
|
class CerealSerializerForPlan < CacheCrispies::Base
|
5
|
-
|
6
|
-
|
7
|
-
end
|
5
|
+
key :cereal
|
6
|
+
dependency_key :'v1-beta'
|
8
7
|
|
9
|
-
|
8
|
+
cache_key_addons do |options|
|
10
9
|
['addon1', options[:extra_addon]]
|
11
10
|
end
|
12
11
|
|
@@ -15,7 +14,7 @@ describe CacheCrispies::Plan do
|
|
15
14
|
|
16
15
|
let(:serializer) { CerealSerializerForPlan }
|
17
16
|
let(:serializer_file_path) {
|
18
|
-
File.expand_path('fixtures/test_serializer.rb', __dir__)
|
17
|
+
File.expand_path('../fixtures/test_serializer.rb', __dir__)
|
19
18
|
}
|
20
19
|
let(:model_cache_key) { 'model-cache-key' }
|
21
20
|
let(:model) { OpenStruct.new(name: 'Sugar Smacks', cache_key: model_cache_key) }
|
@@ -86,6 +85,10 @@ describe CacheCrispies::Plan do
|
|
86
85
|
expect(subject.cache_key).to include serializer.cache_key_base
|
87
86
|
end
|
88
87
|
|
88
|
+
it "includes the serializer's #dependency_key" do
|
89
|
+
expect(subject.cache_key).to include 'v1-beta'
|
90
|
+
end
|
91
|
+
|
89
92
|
it "includes the addons_key" do
|
90
93
|
expect(subject.cache_key).to include(
|
91
94
|
Digest::MD5.hexdigest('addon1|addon2')
|
@@ -104,6 +107,7 @@ describe CacheCrispies::Plan do
|
|
104
107
|
expect(subject.cache_key).to eq(
|
105
108
|
'cache-crispies' \
|
106
109
|
"+CerealSerializerForPlan-#{Digest::MD5.file(serializer_file_path)}" \
|
110
|
+
'+v1-beta' \
|
107
111
|
"+#{Digest::MD5.hexdigest('addon1|addon2')}" \
|
108
112
|
'+model-cache-key'
|
109
113
|
)
|
@@ -116,6 +120,7 @@ describe CacheCrispies::Plan do
|
|
116
120
|
expect(subject.cache_key).to eq(
|
117
121
|
'cache-crispies' \
|
118
122
|
"+CerealSerializerForPlan-#{Digest::MD5.file(serializer_file_path)}" \
|
123
|
+
'+v1-beta' \
|
119
124
|
'+model-cache-key'
|
120
125
|
)
|
121
126
|
end
|
@@ -125,7 +130,7 @@ describe CacheCrispies::Plan do
|
|
125
130
|
describe '#cache' do
|
126
131
|
context 'when the plan is not cacheable' do
|
127
132
|
it "doesn't cache the results" do
|
128
|
-
expect(
|
133
|
+
expect(CacheCrispies).to_not receive(:cache)
|
129
134
|
subject.cache {}
|
130
135
|
end
|
131
136
|
end
|
@@ -134,7 +139,7 @@ describe CacheCrispies::Plan do
|
|
134
139
|
it "doesn't cache the results" do
|
135
140
|
expect(subject).to receive(:cache?).and_return true
|
136
141
|
expect(subject).to receive(:cache_key).and_return 'bar'
|
137
|
-
expect(
|
142
|
+
expect(CacheCrispies).to receive_message_chain(:cache, :fetch).with('bar')
|
138
143
|
subject.cache {}
|
139
144
|
end
|
140
145
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CacheCrispies do
|
4
|
+
after { described_class.config.reset! }
|
5
|
+
|
6
|
+
describe '.configure and .config' do
|
7
|
+
it 'allows setting values' do
|
8
|
+
expect {
|
9
|
+
described_class.configure do |conf|
|
10
|
+
conf.etags = true
|
11
|
+
end
|
12
|
+
}.to change { described_class.config.etags }.from(false).to true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '.cache' do
|
17
|
+
it 'delegates to config.cache_store' do
|
18
|
+
cache_store = ActiveSupport::Cache::NullStore.new
|
19
|
+
|
20
|
+
expect {
|
21
|
+
described_class.configure do |conf|
|
22
|
+
conf.cache_store = cache_store
|
23
|
+
end
|
24
|
+
}.to change { described_class.cache }.to cache_store
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,35 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cache_crispies
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Crownoble
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-10-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: railties
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.0.0
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '6.1'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 5.0.0
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '6.1'
|
13
33
|
- !ruby/object:Gem::Dependency
|
14
34
|
name: oj
|
15
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -25,7 +45,7 @@ dependencies:
|
|
25
45
|
- !ruby/object:Gem::Version
|
26
46
|
version: '3.7'
|
27
47
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
48
|
+
name: activemodel
|
29
49
|
requirement: !ruby/object:Gem::Requirement
|
30
50
|
requirements:
|
31
51
|
- - ">="
|
@@ -34,7 +54,7 @@ dependencies:
|
|
34
54
|
- - "<"
|
35
55
|
- !ruby/object:Gem::Version
|
36
56
|
version: '6.1'
|
37
|
-
type: :
|
57
|
+
type: :development
|
38
58
|
prerelease: false
|
39
59
|
version_requirements: !ruby/object:Gem::Requirement
|
40
60
|
requirements:
|
@@ -48,16 +68,16 @@ dependencies:
|
|
48
68
|
name: appraisal
|
49
69
|
requirement: !ruby/object:Gem::Requirement
|
50
70
|
requirements:
|
51
|
-
- - "
|
71
|
+
- - "~>"
|
52
72
|
- !ruby/object:Gem::Version
|
53
|
-
version: '
|
73
|
+
version: '2.2'
|
54
74
|
type: :development
|
55
75
|
prerelease: false
|
56
76
|
version_requirements: !ruby/object:Gem::Requirement
|
57
77
|
requirements:
|
58
|
-
- - "
|
78
|
+
- - "~>"
|
59
79
|
- !ruby/object:Gem::Version
|
60
|
-
version: '
|
80
|
+
version: '2.2'
|
61
81
|
- !ruby/object:Gem::Dependency
|
62
82
|
name: bundler
|
63
83
|
requirement: !ruby/object:Gem::Requirement
|
@@ -126,20 +146,23 @@ files:
|
|
126
146
|
- lib/cache_crispies/base.rb
|
127
147
|
- lib/cache_crispies/collection.rb
|
128
148
|
- lib/cache_crispies/condition.rb
|
149
|
+
- lib/cache_crispies/configuration.rb
|
129
150
|
- lib/cache_crispies/controller.rb
|
130
151
|
- lib/cache_crispies/hash_builder.rb
|
131
152
|
- lib/cache_crispies/memoizer.rb
|
132
153
|
- lib/cache_crispies/plan.rb
|
133
154
|
- lib/cache_crispies/version.rb
|
134
|
-
- spec/attribute_spec.rb
|
135
|
-
- spec/base_spec.rb
|
136
|
-
- spec/collection_spec.rb
|
137
|
-
- spec/condition_spec.rb
|
138
|
-
- spec/
|
155
|
+
- spec/cache_crispies/attribute_spec.rb
|
156
|
+
- spec/cache_crispies/base_spec.rb
|
157
|
+
- spec/cache_crispies/collection_spec.rb
|
158
|
+
- spec/cache_crispies/condition_spec.rb
|
159
|
+
- spec/cache_crispies/configuration_spec.rb
|
160
|
+
- spec/cache_crispies/controller_spec.rb
|
161
|
+
- spec/cache_crispies/hash_builder_spec.rb
|
162
|
+
- spec/cache_crispies/memoizer_spec.rb
|
163
|
+
- spec/cache_crispies/plan_spec.rb
|
164
|
+
- spec/cache_crispies_spec.rb
|
139
165
|
- spec/fixtures/test_serializer.rb
|
140
|
-
- spec/hash_builder_spec.rb
|
141
|
-
- spec/memoizer_spec.rb
|
142
|
-
- spec/plan_spec.rb
|
143
166
|
- spec/spec_helper.rb
|
144
167
|
homepage: https://github.com/codenoble/cache-crispies
|
145
168
|
licenses:
|
@@ -165,13 +188,15 @@ signing_key:
|
|
165
188
|
specification_version: 4
|
166
189
|
summary: Fast Rails serializer with built-in caching
|
167
190
|
test_files:
|
168
|
-
- spec/attribute_spec.rb
|
169
|
-
- spec/base_spec.rb
|
170
|
-
- spec/collection_spec.rb
|
171
|
-
- spec/condition_spec.rb
|
172
|
-
- spec/
|
191
|
+
- spec/cache_crispies/attribute_spec.rb
|
192
|
+
- spec/cache_crispies/base_spec.rb
|
193
|
+
- spec/cache_crispies/collection_spec.rb
|
194
|
+
- spec/cache_crispies/condition_spec.rb
|
195
|
+
- spec/cache_crispies/configuration_spec.rb
|
196
|
+
- spec/cache_crispies/controller_spec.rb
|
197
|
+
- spec/cache_crispies/hash_builder_spec.rb
|
198
|
+
- spec/cache_crispies/memoizer_spec.rb
|
199
|
+
- spec/cache_crispies/plan_spec.rb
|
200
|
+
- spec/cache_crispies_spec.rb
|
173
201
|
- spec/fixtures/test_serializer.rb
|
174
|
-
- spec/hash_builder_spec.rb
|
175
|
-
- spec/memoizer_spec.rb
|
176
|
-
- spec/plan_spec.rb
|
177
202
|
- spec/spec_helper.rb
|
data/spec/base_spec.rb
DELETED
@@ -1,186 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'ostruct'
|
3
|
-
|
4
|
-
describe CacheCrispies::Base do
|
5
|
-
class NutritionSerializer < CacheCrispies::Base
|
6
|
-
serialize :calories
|
7
|
-
end
|
8
|
-
|
9
|
-
class CacheCrispiesTestSerializer < CacheCrispies::Base
|
10
|
-
serialize :id, :company, to: String
|
11
|
-
|
12
|
-
show_if -> { true } do
|
13
|
-
show_if -> { true } do
|
14
|
-
show_if -> { true } do
|
15
|
-
serialize :name, from: :brand
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
nest_in :nested do
|
21
|
-
nest_in :nested_again do
|
22
|
-
serialize :deeply_nested
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
serialize :nutrition_info, with: NutritionSerializer
|
27
|
-
|
28
|
-
serialize :organic, to: :bool
|
29
|
-
|
30
|
-
def id
|
31
|
-
model.id.to_s
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
let(:model) do
|
36
|
-
OpenStruct.new(
|
37
|
-
id: 42,
|
38
|
-
brand: 'Cookie Crisp',
|
39
|
-
company: 'General Mills',
|
40
|
-
deeply_nested: true,
|
41
|
-
nutrition_info: OpenStruct.new(calories: 1_000),
|
42
|
-
organic: 'true'
|
43
|
-
)
|
44
|
-
end
|
45
|
-
|
46
|
-
let(:instance) { CacheCrispiesTestSerializer.new(model) }
|
47
|
-
subject { instance }
|
48
|
-
|
49
|
-
describe '#as_json' do
|
50
|
-
it 'serializes to a hash' do
|
51
|
-
expect(subject.as_json).to eq(
|
52
|
-
id: '42',
|
53
|
-
name: 'Cookie Crisp',
|
54
|
-
company: 'General Mills',
|
55
|
-
nested: {
|
56
|
-
nested_again: {
|
57
|
-
deeply_nested: true
|
58
|
-
}
|
59
|
-
},
|
60
|
-
nutrition_info: {
|
61
|
-
calories: 1000
|
62
|
-
},
|
63
|
-
organic: true
|
64
|
-
)
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
describe '.key' do
|
69
|
-
it 'underscores the demodulized class name' do
|
70
|
-
expect(subject.class.key).to eq :cache_crispies_test
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
describe '.collection_key' do
|
75
|
-
it 'pluralizes the #key' do
|
76
|
-
expect(subject.class.collection_key).to eq :cache_crispies_tests
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
describe '.do_caching?' do
|
81
|
-
it 'is false by default' do
|
82
|
-
expect(subject.class.do_caching?).to be false
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
describe '.cache_key_addons' do
|
87
|
-
it 'returns an empty array by default' do
|
88
|
-
expect(subject.class.cache_key_addons).to eq []
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
describe '.cache_key_base' do
|
93
|
-
let(:nested_serializer_digest) { 'nutrition-serializer-digest' }
|
94
|
-
let(:serializer_file_path) {
|
95
|
-
File.expand_path('fixtures/test_serializer.rb', __dir__)
|
96
|
-
}
|
97
|
-
let(:serializer_file_digest) {
|
98
|
-
Digest::MD5.file(serializer_file_path).to_s
|
99
|
-
}
|
100
|
-
|
101
|
-
before do
|
102
|
-
allow(NutritionSerializer).to receive(:file_hash).and_return(
|
103
|
-
nested_serializer_digest
|
104
|
-
)
|
105
|
-
allow(Rails).to receive_message_chain(:root, :join).and_return(
|
106
|
-
serializer_file_path
|
107
|
-
)
|
108
|
-
end
|
109
|
-
|
110
|
-
it 'includes the file name' do
|
111
|
-
expect(subject.class.cache_key_base).to include subject.class.to_s
|
112
|
-
end
|
113
|
-
|
114
|
-
it "includes a digest of the serializer class file's contents" do
|
115
|
-
expect(subject.class.cache_key_base).to include serializer_file_digest
|
116
|
-
end
|
117
|
-
|
118
|
-
it "includes a digest of the nested serializer class file's contents" do
|
119
|
-
expect(subject.class.cache_key_base).to include nested_serializer_digest
|
120
|
-
end
|
121
|
-
|
122
|
-
it 'correctly formats the key' do
|
123
|
-
expect(subject.class.cache_key_base).to eq(
|
124
|
-
"#{subject.class}-#{serializer_file_digest}+#{nested_serializer_digest}"
|
125
|
-
)
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
describe '.attributes' do
|
130
|
-
subject { instance.class.attributes }
|
131
|
-
|
132
|
-
it 'contains all the attributes' do
|
133
|
-
expect(subject.length).to eq 6
|
134
|
-
end
|
135
|
-
|
136
|
-
it 'preserves the attribute order' do
|
137
|
-
expect(subject.map(&:key)).to eq(
|
138
|
-
%i[id company name deeply_nested nutrition_info organic]
|
139
|
-
)
|
140
|
-
end
|
141
|
-
|
142
|
-
it 'contains the correct attribute values' do
|
143
|
-
expect(subject[0].method_name).to eq :id
|
144
|
-
expect(subject[0].key).to eq :id
|
145
|
-
expect(subject[0].serializer).to be nil
|
146
|
-
expect(subject[0].coerce_to).to be String
|
147
|
-
expect(subject[0].nesting).to eq []
|
148
|
-
expect(subject[0].conditions).to eq []
|
149
|
-
|
150
|
-
expect(subject[1].method_name).to eq :company
|
151
|
-
expect(subject[1].key).to eq :company
|
152
|
-
expect(subject[1].serializer).to be nil
|
153
|
-
expect(subject[1].coerce_to).to be String
|
154
|
-
expect(subject[1].nesting).to eq []
|
155
|
-
expect(subject[1].conditions).to eq []
|
156
|
-
|
157
|
-
expect(subject[2].method_name).to eq :brand
|
158
|
-
expect(subject[2].key).to eq :name
|
159
|
-
expect(subject[2].serializer).to be nil
|
160
|
-
expect(subject[2].coerce_to).to be nil
|
161
|
-
expect(subject[2].nesting).to eq []
|
162
|
-
expect(subject[2].conditions.length).to be 3
|
163
|
-
|
164
|
-
expect(subject[3].method_name).to eq :deeply_nested
|
165
|
-
expect(subject[3].key).to eq :deeply_nested
|
166
|
-
expect(subject[3].serializer).to be nil
|
167
|
-
expect(subject[3].coerce_to).to be nil
|
168
|
-
expect(subject[3].nesting).to eq %i[nested nested_again]
|
169
|
-
expect(subject[3].conditions).to eq []
|
170
|
-
|
171
|
-
expect(subject[4].method_name).to eq :nutrition_info
|
172
|
-
expect(subject[4].key).to eq :nutrition_info
|
173
|
-
expect(subject[4].serializer).to be NutritionSerializer
|
174
|
-
expect(subject[4].coerce_to).to be nil
|
175
|
-
expect(subject[4].nesting).to eq []
|
176
|
-
expect(subject[4].conditions).to eq []
|
177
|
-
|
178
|
-
expect(subject[5].method_name).to eq :organic
|
179
|
-
expect(subject[5].key).to eq :organic
|
180
|
-
expect(subject[5].serializer).to be nil
|
181
|
-
expect(subject[5].coerce_to).to be :bool
|
182
|
-
expect(subject[5].nesting).to eq []
|
183
|
-
expect(subject[5].conditions).to eq []
|
184
|
-
end
|
185
|
-
end
|
186
|
-
end
|
data/spec/controller_spec.rb
DELETED
@@ -1,69 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'active_model'
|
3
|
-
require 'action_controller'
|
4
|
-
|
5
|
-
describe CacheCrispies::Controller do
|
6
|
-
class Cereal
|
7
|
-
include ActiveModel::Model
|
8
|
-
attr_accessor :name
|
9
|
-
end
|
10
|
-
|
11
|
-
class CerealController < ActionController::Base
|
12
|
-
include CacheCrispies::Controller
|
13
|
-
end
|
14
|
-
|
15
|
-
class CerealSerializerForController < CacheCrispies::Base
|
16
|
-
serialize :name
|
17
|
-
|
18
|
-
def self.key
|
19
|
-
'cereal'
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
let(:cereal_names) { ['Count Chocula', 'Eyeholes'] }
|
24
|
-
let(:collection) { cereal_names.map { |name| Cereal.new(name: name) } }
|
25
|
-
|
26
|
-
subject { CerealController.new }
|
27
|
-
|
28
|
-
before do
|
29
|
-
allow_any_instance_of(
|
30
|
-
CacheCrispies::Plan
|
31
|
-
).to receive(:etag).and_return 'test-etag'
|
32
|
-
allow(subject).to receive_message_chain(:response, :weak_etag=).with 'test-etag'
|
33
|
-
end
|
34
|
-
|
35
|
-
describe '#cache_render' do
|
36
|
-
it 'renders a json collection' do
|
37
|
-
expect(subject).to receive(:render).with(
|
38
|
-
json: {
|
39
|
-
cereals: cereal_names.map { |name| { name: name } }
|
40
|
-
}.to_json
|
41
|
-
)
|
42
|
-
|
43
|
-
subject.cache_render CerealSerializerForController, collection
|
44
|
-
end
|
45
|
-
|
46
|
-
it 'renders a single json object' do
|
47
|
-
expect(subject).to receive(:render).with(
|
48
|
-
json: { cereal: { name: cereal_names.first } }.to_json
|
49
|
-
)
|
50
|
-
|
51
|
-
subject.cache_render CerealSerializerForController, collection.first
|
52
|
-
end
|
53
|
-
|
54
|
-
context 'with a status: option' do
|
55
|
-
it 'passes the status option to the Rails render call' do
|
56
|
-
expect(subject).to receive(:render).with(
|
57
|
-
json: { cereal: { name: cereal_names.first } }.to_json,
|
58
|
-
status: 418
|
59
|
-
)
|
60
|
-
|
61
|
-
subject.cache_render(
|
62
|
-
CerealSerializerForController,
|
63
|
-
collection.first,
|
64
|
-
status: 418
|
65
|
-
)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|