mongoid-cached-json 1.7.0 → 1.7.1

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.
data/README.md CHANGED
@@ -1,9 +1,6 @@
1
- Mongoid::CachedJson
2
- ===================
1
+ # Mongoid::CachedJson
3
2
 
4
- [![Gem Version](https://badge.fury.io/rb/mongoid-cached-json.svg)](http://badge.fury.io/rb/mongoid-cached-json)
5
- [![Test](https://github.com/mongoid/mongoid-cached-json/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/mongoid/mongoid-cached-json/actions/workflows/test.yml?query=branch%3Amaster)
6
- [![Coverage Status](https://coveralls.io/repos/github/mongoid/mongoid-cached-json/badge.svg?branch=master)](https://coveralls.io/github/mongoid/mongoid-cached-json?branch=master)
3
+ [![Gem Version](https://badge.fury.io/rb/mongoid-cached-json.svg)](https://badge.fury.io/rb/mongoid-cached-json) [![Test](https://github.com/mongoid/mongoid-cached-json/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/mongoid/mongoid-cached-json/actions/workflows/test.yml?query=branch%3Amaster) [![Coverage Status](https://coveralls.io/repos/github/mongoid/mongoid-cached-json/badge.svg?branch=master)](https://coveralls.io/github/mongoid/mongoid-cached-json?branch=master)
7
4
 
8
5
  The Ruby 4.0 CI job measures library line coverage with SimpleCov and submits `coverage/.resultset.json` to Coveralls using GitHub's automatic token. Other matrix jobs do not submit duplicate reports. Run `COVERAGE=true bundle install` followed by `COVERAGE=true bundle exec rake spec` for a local report. The default-branch badge requires Coveralls to accept an upstream `master` build; configuring this workflow does not activate the external service.
9
6
 
@@ -13,15 +10,21 @@ Typical `as_json` definitions may involve lots of database point queries and met
13
10
 
14
11
  Using `Mongoid::CachedJson` we were able to cut our JSON API average response time by about a factor of 10.
15
12
 
16
- Resources
17
- ---------
13
+ ## Resources
18
14
 
19
- * [Need Help? Google Group](http://groups.google.com/group/mongoid-cached-json)
20
- * [Source Code](http://github.com/mongoid/mongoid-cached-json)
15
+ * [Need Help? Google Group](https://groups.google.com/g/mongoid-cached-json)
16
+ * [Source Code](https://github.com/mongoid/mongoid-cached-json)
21
17
  * [Upgrading](UPGRADING.md)
22
18
 
23
- Quickstart
24
- ----------
19
+ ## Installation
20
+
21
+ Add the gem to your Gemfile and run `bundle install`.
22
+
23
+ ```ruby
24
+ gem 'mongoid-cached-json'
25
+ ```
26
+
27
+ ## Quickstart
25
28
 
26
29
  Add `mongoid-cached-json` to your Gemfile.
27
30
 
@@ -29,7 +32,7 @@ Add `mongoid-cached-json` to your Gemfile.
29
32
 
30
33
  Include `Mongoid::CachedJson` in your models.
31
34
 
32
- ``` ruby
35
+ ```ruby
33
36
  class Gadget
34
37
  include Mongoid::CachedJson
35
38
 
@@ -41,7 +44,6 @@ class Gadget
41
44
  json_fields \
42
45
  name: {},
43
46
  extras: { properties: :public }
44
-
45
47
  end
46
48
 
47
49
  class Widget
@@ -53,13 +55,12 @@ class Widget
53
55
  json_fields \
54
56
  name: {},
55
57
  gadgets: { type: :reference, properties: :public }
56
-
57
58
  end
58
59
  ```
59
60
 
60
61
  Invoke `as_json`.
61
62
 
62
- ``` ruby
63
+ ```ruby
63
64
  widget = Widget.first
64
65
 
65
66
  # the `:short` version of the JSON, `gadgets` not included
@@ -75,12 +76,11 @@ widget.as_json(properties: :public)
75
76
  widget.as_json(properties: :all)
76
77
  ```
77
78
 
78
- Configuration
79
- -------------
79
+ ## Configuration
80
80
 
81
81
  By default `Mongoid::CachedJson` will use an instance of `ActiveSupport::Cache::MemoryStore` in a non-Rails and `Rails.cache` in a Rails environment. You can configure it to use any other cache store.
82
82
 
83
- ``` ruby
83
+ ```ruby
84
84
  Mongoid::CachedJson.configure do |config|
85
85
  config.cache = ActiveSupport::Cache::FileStore.new
86
86
  end
@@ -88,14 +88,13 @@ end
88
88
 
89
89
  The default JSON version returned from `as_json` is `:unspecified`. If you wish to redefine this, set `Mongoid::CachedJson.config.default_version`.
90
90
 
91
- ``` ruby
91
+ ```ruby
92
92
  Mongoid::CachedJson.configure do |config|
93
93
  config.default_version = :v2
94
94
  end
95
95
  ```
96
96
 
97
- Defining Fields
98
- ---------------
97
+ ## Defining Fields
99
98
 
100
99
  `Mongoid::CachedJson` supports the following options:
101
100
 
@@ -111,12 +110,11 @@ Defining Fields
111
110
  * `:cache` defaults to `true`; set it to `false` to recompute a field on every serialization (see below)
112
111
  * `:reference_properties` can be one of `:short`, `:public`, `:all`, default will select the reference properties format dynamically (see below)
113
112
 
114
- Per-Field Caching
115
- -----------------
113
+ ## Per-Field Caching
116
114
 
117
115
  Use `cache: false` for a field whose value should be evaluated on every `as_json` call, even when the rest of the object's JSON comes from the cache. The field is excluded from the cached fragment and merged into the response after that fragment is fetched. Its `definition`, property/version filters, and configured transformations still apply.
118
116
 
119
- ``` ruby
117
+ ```ruby
120
118
  class Product
121
119
  include Mongoid::Document
122
120
  include Mongoid::CachedJson
@@ -142,7 +140,7 @@ The cached `name` stays unchanged until its cache entry expires or is invalidate
142
140
 
143
141
  For a `type: :reference` field, `cache: false` bypasses reads and writes of the referenced object's own JSON fragment through that relationship. The parent's other fields remain cached:
144
142
 
145
- ``` ruby
143
+ ```ruby
146
144
  class Order
147
145
  include Mongoid::Document
148
146
  include Mongoid::CachedJson
@@ -161,15 +159,13 @@ order.as_json # => { number: '123', product: { name: 'Renamed widget', stock: 9
161
159
 
162
160
  The `product` reference is recomputed even if the product has a cached JSON fragment from another serialization. This does not disable caching on the `Product` model itself: `product.as_json` and other relationships to that product still use their normal cache policies. References nested inside the product also retain their own cache policies; the bypass is not recursive.
163
161
 
164
- All Stored Fields
165
- -----------------
162
+ ## All Stored Fields
166
163
 
167
164
  Use `json_fields :all, display_name: { definition: :display_name }` to expand every Mongoid field declared so far (including `_id`) into an ordinary JSON field, then merge explicit definitions. An explicit definition overrides the generated definition for the same symbol key; for example, `json_fields :all, secret: { properties: :all }` restricts that field to the `:all` representation.
168
165
 
169
166
  Declare Mongoid fields before `json_fields :all`. Later declarations, dynamic attributes, and associations are not added automatically; declare references explicitly with `type: :reference`. This is opt-in and may expose sensitive stored data or foreign keys, so prefer an explicit allowlist for public APIs. Runtime `as_json(methods: ...)` selection remains unsupported: declare computed methods in `json_fields`.
170
167
 
171
- Reference Properties
172
- --------------------
168
+ ## Reference Properties
173
169
 
174
170
  When calling `as_json` on a model that contains references to other models the value of the `:properties` option passed into the `as_json` call will be chosen as follows:
175
171
 
@@ -182,7 +178,7 @@ The dynamic selection where `:public` generates `:short` references allows to re
182
178
 
183
179
  To override this behavior and always return the `:short` JSON for a child reference, use `:reference_properties`. In the following example we would want `Person.as_json(properties: :all)` to return the social security number for that person, but not for all their friends.
184
180
 
185
- ``` ruby
181
+ ```ruby
186
182
  class Person
187
183
  include Mongoid::Document
188
184
  include Mongoid::CachedJson
@@ -195,16 +191,14 @@ class Person
195
191
  name: {},
196
192
  ssn: { properties: :all },
197
193
  friends: { properties: :public, reference_properties: :short }
198
-
199
194
  end
200
195
  ```
201
196
 
202
- Versioning
203
- ----------
197
+ ## Versioning
204
198
 
205
199
  You can set an optional `version` or `versions` attribute on JSON fields. Consider the following definition where the first version defined `:name`, then split it into `:first`, `:middle` and `:last` in version `:v2` and introduced a date of birth in `:v3`.
206
200
 
207
- ``` ruby
201
+ ```ruby
208
202
  class Person
209
203
  include Mongoid::Document
210
204
  include Mongoid::CachedJson
@@ -213,39 +207,37 @@ class Person
213
207
  field :last
214
208
 
215
209
  def name
216
- [ first, middle, last ].compact.join(' ')
210
+ [first, middle, last].compact.join(' ')
217
211
  end
218
212
 
219
213
  json_fields \
220
- first: { versions: [ :v2, :v3 ] },
221
- last: { versions: [ :v2, :v3 ] },
222
- middle: { versions: [ :v2, :v3 ] },
214
+ first: { versions: %i[v2 v3] },
215
+ last: { versions: %i[v2 v3] },
216
+ middle: { versions: %i[v2 v3] },
223
217
  born: { versions: :v3 },
224
218
  name: { definition: :name }
225
-
226
219
  end
227
220
  ```
228
221
 
229
- ``` ruby
222
+ ```ruby
230
223
  person = Person.create(first: 'John', middle: 'F.', last: 'Kennedy', born: 'May 29, 1917')
231
224
  person.as_json # { name: 'John F. Kennedy' }
232
225
  person.as_json(version: :v2) # { first: 'John', middle: 'F.', last: 'Kennedy', name: 'John F. Kennedy' }
233
- person.as_json(version: :v3) # { first: 'John', middle: 'F.', last: 'Kennedy', name: 'John F. Kennedy', born: 'May 29, 1917' }
226
+ person.as_json(version: :v3)
227
+ # { first: 'John', middle: 'F.', last: 'Kennedy', name: 'John F. Kennedy', born: 'May 29, 1917' }
234
228
  ```
235
229
 
236
- Schema Cache Versions
237
- ---------------------
230
+ ## Schema Cache Versions
238
231
 
239
232
  Declare `json_fields cache_version: 2, name: {}` to add a model-specific schema namespace to its cache keys. Increment it when changing fields, computed values, or transformations in a deployment. This is independent of the API `version:` passed to `as_json`; callers need not change. Omitting `cache_version` preserves existing keys.
240
233
 
241
234
  New code stops reading old fragments, including referenced fragments of that model. Old entries are not deleted and remain until eviction or a cache-store TTL. The gem does not set a default TTL; configure `expires_in` on your cache store if needed. Updates expire only the current namespace. For rolling deployments or rollbacks, do not reuse a namespace containing stale values: coordinate invalidation or choose another version.
242
235
 
243
- Transformations
244
- ---------------
236
+ ## Transformations
245
237
 
246
238
  You can define global transformations on all JSON values with `Mongoid::CachedJson.config.transform`. Each transformation must return a value. In the following example we extend the JSON definition with an application-specific `:trusted` field and encode any content that is not trusted.
247
239
 
248
- ``` ruby
240
+ ```ruby
249
241
  class Widget
250
242
  include Mongoid::Document
251
243
  include Mongoid::CachedJson
@@ -259,38 +251,34 @@ class Widget
259
251
  end
260
252
  ```
261
253
 
262
- ``` ruby
263
- Mongoid::CachedJson.config.transform do |field, definition, value|
264
- trusted = !!definition[:trusted]
265
- trusted ? value : CGI.escapeHTML(value)
266
- end
254
+ ```ruby
255
+ Mongoid::CachedJson.config.transform do |field, definition, value|
256
+ definition[:trusted] ? value : CGI.escapeHTML(value)
257
+ end
267
258
  ```
268
259
 
269
- Mixing with Standard as_json
270
- ----------------------------
260
+ ## Mixing with Standard `as_json`
271
261
 
272
262
  Taking part in the `Mongoid::CachedJson` `json_fields` scheme is optional: you can still write `as_json` methods where it makes sense.
273
263
 
274
- Turning Caching Off
275
- -------------------
264
+ ## Turning Caching Off
276
265
 
277
266
  You can set `Mongoid::CachedJson.config.disable_caching = true`. It may be a good idea to set it to `ENV['DISABLE_JSON_CACHING']`, in case this turns out not to be The Solution To All Of Your Performance Problems (TM).
278
267
 
279
268
  To bypass shared-cache reads and writes for one model while retaining its JSON fields, versions, and property hierarchy, declare `json_fields disable_caching: true, name: {}`. This applies when the model is serialized directly, in collections, or as a reference. Other models, including its parent and referenced children, keep their own caching policy. Changes to an uncached child are visible even when its parent's scalar fields are cached.
280
269
 
281
- Cache Invalidation Boundaries
282
- -----------------------------
270
+ ## Cache Invalidation Boundaries
283
271
 
284
272
  Normal model updates and destroys expire that document's JSON fragments through Mongoid callbacks. Bulk or raw collection updates that bypass model callbacks cannot invalidate the cache automatically. Explicitly expire each affected document after such a write:
285
273
 
286
- ``` ruby
274
+ ```ruby
287
275
  User.where(id: user_id).update_all(status: 'active')
288
276
  User.find(user_id).expire_cached_json
289
277
  ```
290
278
 
291
279
  Concurrent writes have a separate boundary: an older in-memory instance can repopulate a fragment after another instance has saved newer values and expired the cache. Reload stale instances before serializing them, and expire their fragments if a stale writer may already have repopulated the cache:
292
280
 
293
- ``` ruby
281
+ ```ruby
294
282
  user.reload
295
283
  user.expire_cached_json
296
284
  user.as_json
@@ -298,12 +286,11 @@ user.as_json
298
286
 
299
287
  Reloading and expiring are not an atomic cross-process operation. This gem does not provide a distributed revision or compare-and-set protocol for cache stores. Applications requiring strict consistency across concurrent writers should coordinate persistence and cache invalidation themselves or disable caching for the affected data.
300
288
 
301
- Testing JSON
302
- ------------
289
+ ## Testing JSON
303
290
 
304
291
  This library overrides `as_json`, hence testing JSON results can be done at model level.
305
292
 
306
- ``` ruby
293
+ ```ruby
307
294
  describe 'as_json' do
308
295
  before :each do
309
296
  @person = Person.create!(first: 'John', last: 'Doe')
@@ -316,7 +303,7 @@ end
316
303
 
317
304
  It's also common to test the results of the API using the [Pathy](https://github.com/twoism/pathy) library.
318
305
 
319
- ``` ruby
306
+ ```ruby
320
307
  describe 'as_json' do
321
308
  before :each do
322
309
  person = Person.create!(first: 'John', last: 'Doe')
@@ -328,14 +315,13 @@ describe 'as_json' do
328
315
  end
329
316
  ```
330
317
 
331
- Testing Cache Invalidation
332
- --------------------------
318
+ ## Testing Cache Invalidation
333
319
 
334
320
  Cache is invalidated by calling `:expire_cached_json` on an instance.
335
321
 
336
- ``` ruby
322
+ ```ruby
337
323
  describe 'updating a person' do
338
- before :each
324
+ before :each do
339
325
  @person = Person.create!(name: 'John Doe')
340
326
  end
341
327
  it 'invalidates cache' do
@@ -344,6 +330,7 @@ describe 'updating a person' do
344
330
  end
345
331
  end
346
332
  ```
333
+
347
334
  You may also want to use [this RSpec matcher](spec/support/matchers/invalidate.rb).
348
335
 
349
336
  ```ruby
@@ -356,12 +343,11 @@ describe 'updating a person' do
356
343
  end
357
344
  ```
358
345
 
359
- Performance
360
- -----------
346
+ ## Performance
361
347
 
362
348
  This gem implements two interesting optimizations.
363
349
 
364
- ### Bulk Reference Resolving w/ Local Store
350
+ ### Bulk Reference Resolving with a Local Store
365
351
 
366
352
  Consider an array of Mongoid instances, each with numerous references to other objects. It's typical to see such instances reference the same object. `Mongoid::CachedJson` first collects all JSON references, then resolves them after suppressing duplicates. This significantly reduces the number of cache queries.
367
353
 
@@ -369,14 +355,12 @@ Consider an array of Mongoid instances, each with numerous references to other o
369
355
 
370
356
  Various cache stores, including Memcached, support bulk read operations. The [Dalli](https://github.com/mperham/dalli) gem exposes this via the `read_multi` method. `Mongoid::CachedJson` will always invoke `read_multi` where available, which significantly reduces the number of network roundtrips to the cache servers.
371
357
 
372
- Contributing
373
- ------------
358
+ ## Contributing
374
359
 
375
- See [CONTRIBUTING](CONTRIBUTING.md).
360
+ See [CONTRIBUTING.md](CONTRIBUTING.md), [CHANGELOG.md](CHANGELOG.md), and [UPGRADING.md](UPGRADING.md).
376
361
 
377
- Copyright and License
378
- ---------------------
362
+ ## Copyright and License
379
363
 
380
- MIT License, see [LICENSE](https://github.com/mongoid/mongoid-cached-json/blob/master/LICENSE.md) for details.
364
+ MIT License, see [LICENSE.md](LICENSE.md) for details.
381
365
 
382
366
  (c) 2012-2014 [Artsy](https://artsy.net) and [Contributors](CHANGELOG.md)
data/RELEASING.md ADDED
@@ -0,0 +1,35 @@
1
+ # Releasing Mongoid::CachedJson
2
+
3
+ Releases use [ruby-gem-release](https://github.com/dblock/ruby-gem-release) 0.9.0 through a manually dispatched [Release workflow](.github/workflows/release.yml). Keep the `ruby-gem-release` gem in [Gemfile](Gemfile) and the reusable workflow in `release.yml` pinned to the same version. The workflow publishes through RubyGems Trusted Publishing, so no RubyGems API key or one-time password is needed. It only runs from `master` in `mongoid/mongoid-cached-json`. The local Bundler `rake release`, `release:rubygem_push`, and `release:source_control_push` tasks are disabled.
4
+
5
+ ## One-Time Setup
6
+
7
+ Before the first automated release, a maintainer must:
8
+
9
+ 1. Create the GitHub environment `release` in `mongoid/mongoid-cached-json`, for example with `gh api --method PUT repos/mongoid/mongoid-cached-json/environments/release`, and restrict its deployment branches to `master`. Add required reviewers to require maintainer approval before publication.
10
+ 2. At [mongoid-cached-json's trusted publishers](https://rubygems.org/gems/mongoid-cached-json/trusted_publishers), add a GitHub Actions publisher with repository owner `mongoid`, repository `mongoid-cached-json`, workflow filename `release.yml`, environment `release`, and the separate workflow repository owner `dblock` and repository `ruby-gem-release`.
11
+ 3. Allow GitHub Actions to push release commits and `v*` tags to `master`. See the upstream [setup and permissions guidance](https://github.com/dblock/ruby-gem-release/blob/v0.9.0/RELEASING.md#one-time-setup).
12
+
13
+ Verify the setup without publishing with a [dry run](https://github.com/dblock/ruby-gem-release/blob/v0.9.0/docs/actions/release.md#dry-run), which never edits files, builds, pushes, or publishes:
14
+
15
+ ```sh
16
+ gh workflow run release.yml --repo mongoid/mongoid-cached-json --ref master -f dry-run=true
17
+ ```
18
+
19
+ ## Prepare
20
+
21
+ Start with a clean, up-to-date `master` and verify that the latest [CI checks](https://github.com/mongoid/mongoid-cached-json/actions) pass. Follow semantic versioning: patch for fixes, minor for backward-compatible features, major for breaking changes. Check `VERSION` in [lib/mongoid-cached-json/version.rb](lib/mongoid-cached-json/version.rb) and the matching `(Next)` heading in [CHANGELOG.md](CHANGELOG.md), and change both in a pull request if needed, or pass a `version` override when dispatching. The workflow dates the `(Next)` heading using `YYYY/MM/DD` and removes the `* Your contribution here.` placeholder; a section without notes cannot be released.
22
+
23
+ Release automation requires MRI Ruby 3.3 or newer; the release dependency and tasks are skipped on older Ruby versions and JRuby. Release specs in [spec/release](spec/release) run the actual release tasks against a local fixture with the [ruby-gem-release RSpec helpers](https://github.com/dblock/ruby-gem-release/blob/v0.9.0/docs/testing.md), without contacting RubyGems or GitHub.
24
+
25
+ ## Publish
26
+
27
+ ```sh
28
+ gh workflow run release.yml --repo mongoid/mongoid-cached-json --ref master
29
+ ```
30
+
31
+ Pass `-f version=x.y.z` to release a different version. Dispatching starts a real release; do not merge into `master` while it runs. The workflow prepares the release commit, starts MongoDB 8.0, runs lint and specs on Ruby 3.3, builds the gem, preserves recovery artifacts, publishes to RubyGems, pushes the release commit and `v` tag, and creates a GitHub Release with the changelog notes and the gem attached. It then increments the patch version, adds a new `(Next)` section, and pushes the next development commit. Verify the published version on [RubyGems](https://rubygems.org/gems/mongoid-cached-json), the tag, the [GitHub Release](https://github.com/mongoid/mongoid-cached-json/releases), and the next development commit.
32
+
33
+ ## Recover a Partial Release
34
+
35
+ Inspect the failed run, RubyGems, remote commits and tags, and the GitHub Release before retrying. Publication may have succeeded even if a later step failed. Never move a published tag or republish a version with different contents. Use the run's retained recovery artifacts and the upstream [recovery instructions](https://github.com/dblock/ruby-gem-release/blob/v0.9.0/docs/actions/release.md#recovery-artifacts).
data/Rakefile CHANGED
@@ -6,6 +6,18 @@ require File.expand_path('lib/mongoid-cached-json/version', __dir__)
6
6
  Bundler.setup(:default, :development)
7
7
  Bundler::GemHelper.install_tasks
8
8
 
9
+ if RUBY_ENGINE == 'ruby' && Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.3') &&
10
+ Gem.loaded_specs.key?('ruby-gem-release')
11
+ require_relative 'script/release'
12
+ MongoidCachedJsonRelease.workflow.install_tasks
13
+
14
+ %w[release release:rubygem_push release:source_control_push].each do |name|
15
+ Rake::Task[name].clear
16
+ desc 'Disabled; releases run through GitHub Actions, see RELEASING.md'
17
+ task(name) { abort "#{name} is disabled; releases run through GitHub Actions, see RELEASING.md" }
18
+ end
19
+ end
20
+
9
21
  require 'rake'
10
22
 
11
23
  require 'rspec/core'
@@ -28,7 +40,14 @@ Rake::RDocTask.new do |rdoc|
28
40
  rdoc.rdoc_files.include('lib/**/*.rb')
29
41
  end
30
42
 
31
- require 'rubocop/rake_task'
32
- RuboCop::RakeTask.new
43
+ lint = RUBY_ENGINE == 'ruby' && Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.3') &&
44
+ Gem.loaded_specs.key?('rubocop')
45
+ if lint
46
+ require 'rubocop/rake_task'
47
+ RuboCop::RakeTask.new(:rubocop)
48
+
49
+ desc 'Lint Ruby, Markdown, YAML, and spelling'
50
+ task lint: :rubocop
51
+ end
33
52
 
34
- task default: [:rubocop, :spec]
53
+ task default: lint ? %i[rubocop spec] : %i[spec]
data/UPGRADING.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Upgrading
2
2
 
3
- ## Upgrading to 1.7.0
3
+ ## Upgrading To 1.7.0
4
4
 
5
- ### Hidden child references
5
+ ### Hidden Child References
6
6
 
7
7
  [#35](https://github.com/mongoid/mongoid-cached-json/pull/35) changes how a single child reference is serialized when the child's `hide_as_child_json_when` predicate returns `true`. Previously, an existing but hidden child and a missing child both appeared as `child: nil` (`"child": null` in JSON), so clients could not distinguish them. Now, an existing but hidden child's key is omitted; a missing child still appears as `child: nil`.
8
8
 
@@ -30,7 +30,7 @@ class SometimesSecret
30
30
  belongs_to :secret_parent
31
31
 
32
32
  json_fields \
33
- hide_as_child_json_when: lambda { |child| !child.should_tell_secret },
33
+ hide_as_child_json_when: ->(child) { !child.should_tell_secret },
34
34
  secret: {}
35
35
  end
36
36
 
@@ -294,7 +294,7 @@ module Mongoid
294
294
  # config.cache = Rails.cache
295
295
  # end
296
296
  #
297
- # @return [ Config ] The configuration obejct.
297
+ # @return [ Config ] The configuration object.
298
298
  def configure
299
299
  block_given? ? yield(Mongoid::CachedJson::Config) : Mongoid::CachedJson::Config
300
300
  end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module CachedJson
3
- VERSION = '1.7.0'
3
+ VERSION = '1.7.1'
4
4
  end
5
5
  end
@@ -1,5 +1,4 @@
1
- $LOAD_PATH.push File.expand_path('lib', __dir__)
2
- require 'mongoid-cached-json/version'
1
+ require_relative 'lib/mongoid-cached-json/version'
3
2
 
4
3
  Gem::Specification.new do |s|
5
4
  s.name = 'mongoid-cached-json'
data/script/release.rb ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruby-gem-release/changelog'
4
+ require 'ruby-gem-release/workflow/ruby_gem'
5
+
6
+ # Builds the ruby-gem-release workflow used by the Rakefile, the Release GitHub Actions workflow, and spec/release.
7
+ module MongoidCachedJsonRelease
8
+ def self.workflow(**options)
9
+ RubyGemRelease::Workflow::RubyGem.new(
10
+ version: RubyGemRelease::Version::RubyConstant.new(path: 'lib/mongoid-cached-json/version.rb',
11
+ constant: 'VERSION'),
12
+ changelog: RubyGemRelease::Changelog::Intridea.new(placeholder: '* Your contribution here.'),
13
+ **options
14
+ )
15
+ end
16
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Cached JSON boundary contracts' do
4
+ before do
5
+ stub_const('PlainCoverageDocument', Class.new)
6
+ PlainCoverageDocument.class_eval do
7
+ include Mongoid::Document
8
+ field :name, type: String
9
+ end
10
+ end
11
+
12
+ after do
13
+ PlainCoverageDocument.delete_all
14
+ end
15
+
16
+ it 'serializes criteria for models that do not include CachedJson' do
17
+ document = PlainCoverageDocument.create!(name: 'plain')
18
+ json = PlainCoverageDocument.where(id: document.id).as_json
19
+ expect(json.first['name']).to eq('plain')
20
+ end
21
+
22
+ it 'applies configured transforms to uncached fields' do
23
+ stub_const('TransformCoverageDocument', Class.new)
24
+ TransformCoverageDocument.class_eval do
25
+ include Mongoid::Document
26
+ include Mongoid::CachedJson
27
+ field :value
28
+ json_fields value: { cache: false }
29
+ end
30
+ begin
31
+ Mongoid::CachedJson.config.transform << ->(_field, _definition, value) { value.upcase }
32
+ document = TransformCoverageDocument.create!(value: 'fresh')
33
+ expect(document.as_json).to eq(value: 'FRESH')
34
+ ensure
35
+ TransformCoverageDocument.delete_all
36
+ end
37
+ end
38
+ end
39
+
40
+ describe Mongoid::CachedJson do
41
+ it 'defines a semantic version' do
42
+ expect(described_class::VERSION).to match(/\A\d+\.\d+\.\d+\z/)
43
+ end
44
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ if RUBY_ENGINE == 'ruby' && Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.3') &&
4
+ Gem.loaded_specs.key?('ruby-gem-release')
5
+ require_relative '../../script/release'
6
+ require 'ruby-gem-release/rspec'
7
+
8
+ RSpec.describe MongoidCachedJsonRelease do
9
+ include RubyGemRelease::RSpec
10
+
11
+ let(:root) { File.expand_path('../..', __dir__) }
12
+ let(:version_path) { 'lib/mongoid-cached-json/version.rb' }
13
+ let(:fixture_files) do
14
+ %w[
15
+ mongoid-cached-json.gemspec script/release.rb lib/**/*.rb CHANGELOG.md
16
+ CONTRIBUTING.md LICENSE.md README.md UPGRADING.md
17
+ ]
18
+ end
19
+ let(:placeholder) { '* Your contribution here.' }
20
+ let(:note) { '* [#1](https://github.com/mongoid/mongoid-cached-json/pull/1): Added a feature.' }
21
+
22
+ def changelog(notes)
23
+ [
24
+ '### 1.2.0 (Next)', '', notes, placeholder, '',
25
+ '### 1.1.0 (2026/01/01)', '', '* Previous release.', ''
26
+ ].join("\n")
27
+ end
28
+
29
+ def write_version(project, version)
30
+ source = File.read(File.join(root, version_path))
31
+ project.write(version_path, source.sub(/VERSION = (['"])[^'"]+\1/, "VERSION = '#{version}'"))
32
+ end
33
+
34
+ def with_project(notes)
35
+ with_release_project do |project|
36
+ project.environment.merge!(ENV.to_h.slice('MONGOID_VERSION'))
37
+ project.copy_files(path: root, files: fixture_files)
38
+ project.configure_workflow(require: 'script/release', builder: 'MongoidCachedJsonRelease.workflow')
39
+ project.write('CHANGELOG.md', changelog(notes))
40
+ write_version(project, '1.2.0')
41
+ project.commit!
42
+ yield project
43
+ end
44
+ end
45
+
46
+ it 'builds the standard ruby-gem-release workflow' do
47
+ expect(described_class.workflow).to be_a(RubyGemRelease::Workflow::RubyGem)
48
+ end
49
+
50
+ it 'dates the release and prepares the next patch version' do
51
+ with_project(note) do |project|
52
+ project.release!
53
+
54
+ released = project.remote_file('v1.2.0', 'CHANGELOG.md')
55
+ expect(released).to match(%r{\A### 1\.2\.0 \(\d{4}/\d{2}/\d{2}\)\n\n})
56
+ expect(released).to include(note)
57
+ expect(released).not_to include('(Next)', placeholder)
58
+ expect(project.remote_file('v1.2.0', version_path)).to include("VERSION = '1.2.0'")
59
+ pending = project.remote_file('master', 'CHANGELOG.md')
60
+ expect(pending).to start_with("### 1.2.1 (Next)\n\n#{placeholder}\n\n### 1.2.0 (")
61
+ expect(project.remote_file('master', version_path)).to include("VERSION = '1.2.1'")
62
+ expect(project.git('status', '--porcelain')).to be_empty
63
+ end
64
+ end
65
+
66
+ it 'rejects placeholder-only notes without changing files or remote refs' do
67
+ with_project('') do |project|
68
+ original = project.git('rev-parse', 'origin/master')
69
+ project.run_task('release:workflow:capture')
70
+
71
+ expect { project.run_task('release:validate') }.to raise_error(RuntimeError, /no pending changelog notes/)
72
+ expect(project.git('status', '--porcelain')).to be_empty
73
+ expect(project.git('rev-parse', 'origin/master')).to eq(original)
74
+ end
75
+ end
76
+ end
77
+ end