cache_crispies 1.2.0 → 1.3.2

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: 144a9a90f1bceced6d4197add1047b29352f7cba8b89c19dd92e57411672d7c4
4
- data.tar.gz: d6ad1a658628441972010ff169f56fe779b0edc2a8990522b4f49ff765b6f4ee
3
+ metadata.gz: 1a93c5ab1a386306488b41307cb945164dcf1a0d0646cd5b02f828b87b2836d8
4
+ data.tar.gz: dadb16037bb994b1a17157cc7568afbd962d446a007838ba003a0fea29e7745f
5
5
  SHA512:
6
- metadata.gz: f35f5dd51753129083e2ea3c6664709d1977e0255bd60d0a316d275ca6197de1b43cad1fd79bff13ffa9b7183a445f5de4955c45d66ff93a5bd8e30f26f8d37c
7
- data.tar.gz: f29c05d0fd636e7648b6d4f14bbdd597fc91519669eb841797a1934e667653fa8706b276e3b592ea66217d7bf0c93d8294a259003d060de1d4b15f15f090878a
6
+ metadata.gz: '078816fce3ff0dc88fd45dd378dec482843229bdc28b83c188dd0ae04e984b18b14e81ab1d62f5930e26d3724133df3b6aac2033fbd647acad278409ce77c47b'
7
+ data.tar.gz: 26ec8aa1c7f4a55c1e58b76606ca0cafb1384ce2579b0131d3f9a2fa09391da8c0882524a379420c92c13eda3e0173b2c4fbad3356bdd529eb35c1beb1348e18
@@ -15,6 +15,8 @@ module CacheCrispies
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
17
  # @param collection [Boolean] force rendering as single or collection
18
+ # @param optional [Boolean] render only if included in the option's
19
+ # `included` array
18
20
  # @param nesting [Array<Symbol>] the JSON keys this attribute will be
19
21
  # nested inside
20
22
  # @param conditions [Array<CacheCrispies::Condition>] the show_if condition
@@ -24,6 +26,7 @@ module CacheCrispies
24
26
  def initialize(
25
27
  key,
26
28
  from: nil, with: nil, through: nil, to: nil, collection: nil,
29
+ optional: nil,
27
30
  nesting: [], conditions: [],
28
31
  &block
29
32
  )
@@ -36,6 +39,8 @@ module CacheCrispies
36
39
  @nesting = Array(nesting)
37
40
  @conditions = Array(conditions)
38
41
  @block = block
42
+
43
+ @conditions << Optional.new(key) if optional
39
44
  end
40
45
 
41
46
  attr_reader(
@@ -221,6 +221,7 @@ module CacheCrispies
221
221
  def self.serialize(
222
222
  *attribute_names,
223
223
  from: nil, with: nil, through: nil, to: nil, collection: nil,
224
+ optional: nil,
224
225
  &block
225
226
  )
226
227
  attribute_names.flat_map do |attrib|
@@ -236,6 +237,7 @@ module CacheCrispies
236
237
  through: through,
237
238
  to: to,
238
239
  collection: collection,
240
+ optional: optional,
239
241
  nesting: current_nesting,
240
242
  conditions: current_conditions,
241
243
  &block
@@ -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
@@ -19,24 +19,29 @@ module CacheCrispies
19
19
  block.object_id
20
20
  end
21
21
 
22
- # Test the truthiness of the condition against a model and options
22
+ # Test the truthiness of the condition against the serializer instance
23
23
  #
24
- # @param model [Object] typically ActiveRecord::Base, but could be anything
25
- # @param options [Hash] any optional values from the serializer instance
24
+ # @param serializer [Object] CacheCrispies::Base serializer instance
26
25
  # @return [Boolean] the condition's truthiness
27
- def true_for?(model, options = {})
28
- !!case block.arity
29
- when 0
30
- block.call
31
- when 1
32
- block.call(model)
33
- else
34
- block.call(model, options)
35
- end
26
+ def true_for?(serializer)
27
+ return !!serializer.public_send(block) if block.is_a?(Symbol)
28
+
29
+ !!execute_block(serializer.model, serializer.options)
36
30
  end
37
31
 
38
32
  private
39
33
 
40
34
  attr_reader :block
35
+
36
+ def execute_block(model, options)
37
+ case block.arity
38
+ when 0
39
+ block.call
40
+ when 1
41
+ block.call(model)
42
+ else
43
+ block.call(model, options)
44
+ end
45
+ end
41
46
  end
42
47
  end
@@ -51,7 +51,7 @@ module CacheCrispies
51
51
  # show_if block
52
52
  attribute.conditions.all? do |cond|
53
53
  condition_results.fetch(cond.uid) do
54
- cond.true_for?(serializer.model, serializer.options)
54
+ cond.true_for?(serializer)
55
55
  end
56
56
  end
57
57
  end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CacheCrispies
4
+ # Represents an optional condition
5
+ class Optional
6
+ # Returns a new instance of Optional
7
+ #
8
+ # @param block [Proc] the key of the attribute to include
9
+ def initialize(key)
10
+ @key = key
11
+ end
12
+
13
+ # A system-wide unique ID used for memoizaiton
14
+ #
15
+ # @eturn [Integer] the unique ID for this condition
16
+ def uid
17
+ # Just reusing the key seems to make sense
18
+ key
19
+ end
20
+
21
+ # Test the truthiness of the optional condition against a model and options
22
+ #
23
+ # @param model [Object] typically ActiveRecord::Base, but could be anything
24
+ # @param options [Hash] any optional values from the serializer instance
25
+ # @return [Boolean] the condition's truthiness
26
+ def true_for?(serializer)
27
+ included = Array(serializer.options.fetch(:include, [])).map(&:to_sym)
28
+
29
+ included.include?(key) || included.include?(:*)
30
+ end
31
+
32
+ private
33
+
34
+ attr_reader :key
35
+ end
36
+ end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module CacheCrispies
4
4
  # The version of the gem
5
- VERSION = '1.2.0'
5
+ VERSION = '1.3.2'
6
6
  end
@@ -19,6 +19,7 @@ module CacheCrispies
19
19
  autoload :Base, 'cache_crispies/base'
20
20
  autoload :Collection, 'cache_crispies/collection'
21
21
  autoload :Condition, 'cache_crispies/condition'
22
+ autoload :Optional, 'cache_crispies/optional'
22
23
  autoload :Configuration, 'cache_crispies/configuration'
23
24
  autoload :HashBuilder, 'cache_crispies/hash_builder'
24
25
  autoload :Memoizer, 'cache_crispies/memoizer'
@@ -11,7 +11,7 @@ describe CacheCrispies::Base do
11
11
 
12
12
  show_if -> { true } do
13
13
  show_if -> { true } do
14
- show_if -> { true } do
14
+ show_if :visible? do
15
15
  serialize :name, from: :brand
16
16
  end
17
17
  end
@@ -33,6 +33,10 @@ describe CacheCrispies::Base do
33
33
  def id
34
34
  model.id.to_s
35
35
  end
36
+
37
+ def visible?
38
+ true
39
+ end
36
40
  end
37
41
 
38
42
  let(:model) 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(
@@ -1,7 +1,18 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe CacheCrispies::Condition do
4
+ class TestSerializer < CacheCrispies::Base
5
+ show_if :boolean_method? do
6
+ serialize :name
7
+ end
8
+
9
+ def boolean_method?
10
+ end
11
+ end
12
+
4
13
  let(:block) { -> {} }
14
+ let(:model) { OpenStruct.new(name: 'Name') }
15
+ let(:serializer) { TestSerializer.new(model) }
5
16
  subject { described_class.new(block) }
6
17
 
7
18
  describe '#uid' do
@@ -17,7 +28,7 @@ describe CacheCrispies::Condition do
17
28
 
18
29
  it 'calls the block with model and options arguments' do
19
30
  expect(block).to receive(:call).with(model, options)
20
- subject.true_for? model, options
31
+ subject.true_for? serializer
21
32
  end
22
33
 
23
34
  context 'when the block has one argument' do
@@ -25,7 +36,7 @@ describe CacheCrispies::Condition do
25
36
 
26
37
  it 'calls the block with the model only' do
27
38
  expect(block).to receive(:call).with(model)
28
- subject.true_for? model, options
39
+ subject.true_for? serializer
29
40
  end
30
41
  end
31
42
 
@@ -34,12 +45,21 @@ describe CacheCrispies::Condition do
34
45
 
35
46
  it 'calls the block with no arguments' do
36
47
  expect(block).to receive(:call).with(no_args)
37
- subject.true_for? model, options
48
+ subject.true_for? serializer
49
+ end
50
+ end
51
+
52
+ context 'when the block is a symbol' do
53
+ let(:block) { :boolean_method? }
54
+
55
+ it 'calls the method on serializer instance' do
56
+ expect(serializer).to receive(block).with(no_args)
57
+ subject.true_for? serializer
38
58
  end
39
59
  end
40
60
 
41
61
  it 'returns a boolean' do
42
- expect(subject.true_for?(model, options)).to be true
62
+ expect(subject.true_for?(serializer)).to be true
43
63
  end
44
64
  end
45
65
  end
@@ -8,6 +8,10 @@ describe CacheCrispies::HashBuilder do
8
8
  serialize :name
9
9
  end
10
10
 
11
+ class AllergySerializer < CacheCrispies::Base
12
+ serialize :name
13
+ end
14
+
11
15
  class MarketingBsSerializer < CacheCrispies::Base
12
16
  serialize :tagline, :small_print
13
17
 
@@ -29,6 +33,8 @@ describe CacheCrispies::HashBuilder do
29
33
  nest_in :nutritional_information do
30
34
  serialize :calories
31
35
  serialize :ingredients, with: IngredientSerializer
36
+
37
+ serialize :allergies, with: AllergySerializer, optional: true
32
38
  end
33
39
  end
34
40
 
@@ -54,6 +60,12 @@ describe CacheCrispies::HashBuilder do
54
60
  OpenStruct.new(name: 'Other Kind of Sugar')
55
61
  ]
56
62
  }
63
+ let(:allergies) {
64
+ [
65
+ OpenStruct.new(name: 'Peanuts'),
66
+ OpenStruct.new(name: 'Lactose')
67
+ ]
68
+ }
57
69
  let(:model) {
58
70
  OpenStruct.new(
59
71
  id: 42,
@@ -62,7 +74,8 @@ describe CacheCrispies::HashBuilder do
62
74
  calories: 1_000,
63
75
  organic: organic,
64
76
  tagline: "Part of a balanced breakfast",
65
- ingredients: ingredients
77
+ ingredients: ingredients,
78
+ allergies: allergies
66
79
  )
67
80
  }
68
81
  let(:options) { { footnote_marker: '*' } }
@@ -141,5 +154,59 @@ describe CacheCrispies::HashBuilder do
141
154
  end
142
155
  end
143
156
  end
157
+
158
+ context 'when allergies are included' do
159
+ let(:options) { { footnote_marker: '*', include: :allergies } }
160
+
161
+ it 'includes the allergies' do
162
+ expect(subject.call).to eq ({
163
+ uid: '42',
164
+ name: 'Lucky Charms',
165
+ company: 'General Mills',
166
+ tagline: 'Part of a balanced breakfast*',
167
+ small_print: "*this doesn't mean jack-squat",
168
+ about: {
169
+ nutritional_information: {
170
+ calories: 1000,
171
+ ingredients: [
172
+ { name: 'Sugar' },
173
+ { name: 'Other Kind of Sugar' },
174
+ ],
175
+ allergies: [
176
+ { name: 'Peanuts' },
177
+ { name: 'Lactose' },
178
+ ]
179
+ }
180
+ }
181
+ })
182
+ end
183
+ end
184
+
185
+ context 'when everything is included' do
186
+ let(:options) { { footnote_marker: '*', include: '*' } }
187
+
188
+ it 'includes the allergies' do
189
+ expect(subject.call).to eq ({
190
+ uid: '42',
191
+ name: 'Lucky Charms',
192
+ company: 'General Mills',
193
+ tagline: 'Part of a balanced breakfast*',
194
+ small_print: "*this doesn't mean jack-squat",
195
+ about: {
196
+ nutritional_information: {
197
+ calories: 1000,
198
+ ingredients: [
199
+ { name: 'Sugar' },
200
+ { name: 'Other Kind of Sugar' },
201
+ ],
202
+ allergies: [
203
+ { name: 'Peanuts' },
204
+ { name: 'Lactose' },
205
+ ]
206
+ }
207
+ }
208
+ })
209
+ end
210
+ end
144
211
  end
145
212
  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: 1.2.0
4
+ version: 1.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Crownoble
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-12 00:00:00.000000000 Z
11
+ date: 2022-06-07 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: '6.2'
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: '6.2'
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: '6.2'
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: '6.2'
66
+ version: '8.0'
67
67
  - !ruby/object:Gem::Dependency
68
68
  name: appraisal
69
69
  requirement: !ruby/object:Gem::Requirement
@@ -164,6 +164,7 @@ files:
164
164
  - lib/cache_crispies/controller.rb
165
165
  - lib/cache_crispies/hash_builder.rb
166
166
  - lib/cache_crispies/memoizer.rb
167
+ - lib/cache_crispies/optional.rb
167
168
  - lib/cache_crispies/plan.rb
168
169
  - lib/cache_crispies/version.rb
169
170
  - spec/cache_crispies/attribute_spec.rb
@@ -197,7 +198,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
197
198
  - !ruby/object:Gem::Version
198
199
  version: '0'
199
200
  requirements: []
200
- rubygems_version: 3.2.0
201
+ rubygems_version: 3.3.7
201
202
  signing_key:
202
203
  specification_version: 4
203
204
  summary: Fast Rails serializer with built-in caching