cache_crispies 1.0.2 → 1.1.4

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: dde77903d49df53ae283abc8d1c0b93e9f8d849b8d1696d39fced6a664b005dd
4
- data.tar.gz: e656b7ce6ce5e74076380b67fb5d86b6fe731a4df80edbb953c5e63694ce40de
3
+ metadata.gz: dd2682e40adfbb9511562cc2b406cd5fa9731134977f1072c5e42684f88590b9
4
+ data.tar.gz: 12c00d8ddd71b23daf0834db565334e0a1d71a28e37809b82d52717e940f16ec
5
5
  SHA512:
6
- metadata.gz: fb83071875231fa5ec31244d47e2594de335787515dafe75fd9ce433060e151b38469c7b06e2a5428c6e1746855ced29ca6cf25970bd874c3dae056936347478
7
- data.tar.gz: be8908410852bc16ac94efe31b9972d384b82932fb791aad2e9e98df646386131fc260317e3b1d7d3c94e19f20ffc5e3c5951d0a87be67d68da88c7d8eb41cc4
6
+ metadata.gz: 71ea2e90153a90ca9f1ae3d9d6b5e229a7f547f0fe271a6cbe8ec01bde10ec23e2736ef7a14898c7c9c48d2c17393b5ebe8bd7787b0123fd0bfc1868095007b1
7
+ data.tar.gz: c029d5cd5a5b35f22494eeeed2f4b9a3bcffd64989c36b6cd8be2c8731eb032e34926f79b3f3e8f69ab9a84c57170bfc22cc4c06f5757774d6b37eb7ffc5f329
@@ -223,7 +223,8 @@ module CacheCrispies
223
223
  from: nil, with: nil, through: nil, to: nil, collection: nil,
224
224
  &block
225
225
  )
226
- attribute_names.flatten.map { |att| att&.to_sym }.map do |attrib|
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
 
@@ -16,16 +16,24 @@ 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 [Hash] options any optional values from the serializer instance
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
- # @option options [Boolean] :collection whether to render the data as a
21
+ # @param collection [Boolean] whether to render the data as a
23
22
  # collection/array or a single object
24
- # @option options [Integer, Symbol] :status the HTTP response status code
25
- # or Rails-supported symbol. See
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(serializer, cacheable, key: nil, collection: nil, status: nil, **options)
30
+ def cache_render(
31
+ serializer,
32
+ cacheable,
33
+ key: nil, collection: nil, status: nil,
34
+ meta: {}, meta_key: :meta,
35
+ **options
36
+ )
29
37
  plan = CacheCrispies::Plan.new(
30
38
  serializer,
31
39
  cacheable,
@@ -48,7 +56,10 @@ module CacheCrispies
48
56
  plan.cache { serializer.new(cacheable, options).as_json }
49
57
  end
50
58
 
51
- render_hash = { json: Oj.dump(plan.wrap(serializer_json), mode: OJ_MODE) }
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) }
52
63
  render_hash[:status] = status if status
53
64
 
54
65
  render render_hash
@@ -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
@@ -2,5 +2,5 @@
2
2
 
3
3
  module CacheCrispies
4
4
  # The version of the gem
5
- VERSION = '1.0.2'
5
+ VERSION = '1.1.4'
6
6
  end
@@ -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(:single_json) { { cereal: { name: cereal_names.first } }.to_json }
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
@@ -85,8 +85,7 @@ describe CacheCrispies::HashBuilder do
85
85
  { name: 'Other Kind of Sugar' },
86
86
  ]
87
87
  }
88
- },
89
- health: {}
88
+ }
90
89
  })
91
90
  end
92
91
 
@@ -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.0.2
4
+ version: 1.1.4
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: 2020-01-10 00:00:00.000000000 Z
11
+ date: 2020-12-12 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.1'
22
+ version: '6.2'
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.1'
32
+ version: '6.2'
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.1'
56
+ version: '6.2'
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.1'
66
+ version: '6.2'
67
67
  - !ruby/object:Gem::Dependency
68
68
  name: appraisal
69
69
  requirement: !ruby/object:Gem::Requirement
@@ -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
@@ -183,8 +183,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
183
183
  - !ruby/object:Gem::Version
184
184
  version: '0'
185
185
  requirements: []
186
- rubygems_version: 3.0.3
187
- signing_key:
186
+ rubygems_version: 3.1.2
187
+ signing_key:
188
188
  specification_version: 4
189
189
  summary: Fast Rails serializer with built-in caching
190
190
  test_files: