cache_crispies 0.1.1 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: af451e5837f96664e141a147df118fbfffb7ee4fedc71cd5e6c34438b9069461
4
- data.tar.gz: ffe23f3217c50b5c6bbbd183127a2274b6c4afb59ad93e50a271c880d20968f4
3
+ metadata.gz: 76074205a976b800720f4573f27c03e60e0d9aaa6467c20b2e2ad70fe8b2df61
4
+ data.tar.gz: 8c26453bad074e191ce415009917ffb8b99a4d7b1273f15b9be774d8bc42c686
5
5
  SHA512:
6
- metadata.gz: e8208c9ef13c4f66e9d64c7b8a5394e7d2907b894ff4ca6617357ca785ede999048d3ef4ffc00d900a30e5ac284401a5b145fb2271764f878d16603017ac08d1
7
- data.tar.gz: 167cf43b28151ec4d6c65c0badfcbe73a871f7a102bc916f04eb0e889069843e518db1a608da31c854096a60843bd50a69ab99a2b4b784a89ce1d5ecd5e549fe
6
+ metadata.gz: 361709a032d7b2e8c1d4600945fc2897021c8665f6b51b5e82a5d1edff3003afc547d7905b2679569f945f3a98066b256defea701835bf9317990e896ada65c0
7
+ data.tar.gz: 5562570b2b96d1c18b336c28229749a9d98a9ecddf27772f586c7ea95b21134c5862574a37577499f80c901ec21cd298b2b8698d2b4d7a4747cdf59f4e2c18a1
@@ -100,8 +100,19 @@ module CacheCrispies
100
100
  #
101
101
  # @return [String] a cache key for the class
102
102
  def self.cache_key_base
103
- # TODO: we may need to get a cache key from nested serializers as well :(
104
- @cache_key_base ||= "#{self}-#{file_hash}"
103
+ @cache_key_base ||= "#{self}-#{file_hashes.join(CACHE_KEY_SEPARATOR)}"
104
+ end
105
+
106
+ # Return an array of cache key string for this serializer and all nested
107
+ # and deeply nested serializers. The purpose of grabbing all this data is
108
+ # to be able to construct a cache key that will be busted if any of the
109
+ # nested serializers, no matter how deep, change at all.
110
+ #
111
+ # @return [Array<String>] an array of uniq, sorted serializer file hashes
112
+ def self.file_hashes
113
+ @file_hashes ||= (
114
+ [file_hash] + nested_serializers.flat_map(&:file_hashes)
115
+ ).uniq.sort
105
116
  end
106
117
 
107
118
  private
@@ -139,6 +150,11 @@ module CacheCrispies
139
150
  end
140
151
  private_class_method :show_if
141
152
 
153
+ def self.nested_serializers
154
+ attributes.map(&:serializer).compact
155
+ end
156
+ private_class_method :nested_serializers
157
+
142
158
  def self.serialize(*attribute_names, from: nil, with: nil, to: nil)
143
159
  attribute_names.flatten.map { |att| att&.to_sym }.map do |attrib|
144
160
  current_nesting = Array(@nesting).dup
@@ -16,8 +16,11 @@ 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 options [Hash] any hash of custom options that should be passed
20
- # to the serializer instance
19
+ # @param [Hash] options any optional values from the serializer instance
20
+ # @option options [Symbol] :key the name of the root key to nest the JSON
21
+ # data under
22
+ # @option options [Boolean] :collection whether to render the data as a
23
+ # collection/array or a single object
21
24
  # @return [void]
22
25
  def cache_render(serializer, cacheable, options = {})
23
26
  plan = CacheCrispies::Plan.new(serializer, cacheable, options)
@@ -11,19 +11,28 @@ module CacheCrispies
11
11
  # CacheCrispies::Base
12
12
  # @param cacheable [Object] typically ActiveRecord::Base or an enumerable
13
13
  # containing instances of ActiveRecord::Base, but could be anything
14
- # @param options [Hash] any optional values from the serializer instance
14
+ # @param [Hash] options any optional values from the serializer instance
15
+ # @option options [Symbol] :key the name of the root key to nest the JSON
16
+ # data under
17
+ # @option options [Boolean] :collection whether to render the data as a
18
+ # collection/array or a single object
15
19
  def initialize(serializer, cacheable, options = {})
16
20
  @serializer = serializer
17
21
  @cacheable = cacheable
18
- @key = options.delete(:key)
19
- @options = options
22
+
23
+ opts = options.dup
24
+ @key = opts.delete(:key)
25
+ @collection = opts.delete(:collection)
26
+ @options = opts
20
27
  end
21
28
 
22
29
  # Whether or not the cacheable should be treated like a collection
23
30
  #
24
31
  # @return [Boolean] true if cacheable is a collection
25
32
  def collection?
26
- cacheable.respond_to?(:each)
33
+ return @collection unless @collection.nil?
34
+
35
+ @collection = cacheable.respond_to?(:each)
27
36
  end
28
37
 
29
38
  # Returns the cache_key in a format suitable for an ETag header
@@ -2,5 +2,5 @@
2
2
 
3
3
  module CacheCrispies
4
4
  # The version of the gem
5
- VERSION = '0.1.1'
5
+ VERSION = '0.2.0'
6
6
  end
@@ -90,7 +90,40 @@ describe CacheCrispies::Base do
90
90
  end
91
91
 
92
92
  describe '.cache_key_base' do
93
- # TODO
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
94
127
  end
95
128
 
96
129
  describe '.attributes' do
@@ -150,4 +183,4 @@ describe CacheCrispies::Base do
150
183
  expect(subject[5].conditions).to eq []
151
184
  end
152
185
  end
153
- end
186
+ end
@@ -32,7 +32,7 @@ describe CacheCrispies::HashBuilder do
32
32
  end
33
33
  end
34
34
 
35
- show_if ->(model, options) { options[:be_trendy] } do
35
+ show_if ->(_model, options) { options[:be_trendy] } do
36
36
  nest_in :health do
37
37
  serialize :organic
38
38
 
@@ -142,4 +142,4 @@ describe CacheCrispies::HashBuilder do
142
142
  end
143
143
  end
144
144
  end
145
- end
145
+ end
@@ -37,6 +37,14 @@ describe CacheCrispies::Plan do
37
37
  it 'returns false' do
38
38
  expect(subject.collection?).to be false
39
39
  end
40
+
41
+ context 'when the :collection option is true' do
42
+ let(:options) { { collection: true } }
43
+
44
+ it 'returns true' do
45
+ expect(subject.collection?).to be true
46
+ end
47
+ end
40
48
  end
41
49
 
42
50
  context 'when a collection' do
@@ -45,6 +53,14 @@ describe CacheCrispies::Plan do
45
53
  it 'returns false' do
46
54
  expect(subject.collection?).to be true
47
55
  end
56
+
57
+ context 'when the :collection option is false' do
58
+ let(:options) { { collection: false } }
59
+
60
+ it 'returns false' do
61
+ expect(subject.collection?).to be false
62
+ end
63
+ end
48
64
  end
49
65
  end
50
66
 
@@ -166,4 +182,4 @@ describe CacheCrispies::Plan do
166
182
  end
167
183
  end
168
184
  end
169
- end
185
+ end
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: 0.1.1
4
+ version: 0.2.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: 2019-07-12 00:00:00.000000000 Z
11
+ date: 2019-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj