mongoid-cached-json 1.5.3 → 1.7.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.
@@ -6,7 +6,7 @@ describe Mongoid::CachedJson do
6
6
  expect(Mongoid::CachedJson::VERSION.to_f).to be > 0
7
7
  end
8
8
 
9
- [:dalli_store, :memory_store].each do |cache_store|
9
+ [MEMCACHE_STORE, :memory_store].each do |cache_store|
10
10
  context "#{cache_store}" do
11
11
  before :each do
12
12
  @cache = Mongoid::CachedJson::Config.cache
@@ -88,14 +88,14 @@ describe Mongoid::CachedJson do
88
88
  end
89
89
  context 'many-to-one relationships' do
90
90
  it 'uses the correct properties on the base object and passes :short or :all as appropriate' do
91
- manager = JsonManager.create(name: 'Boss')
92
- peon = manager.json_employees.create(name: 'Peon')
93
- manager.json_employees.create(name: 'Indentured servant')
94
- manager.json_employees.create(name: 'Serf', nickname: 'Vince')
91
+ manager = JsonManager.create!(name: 'Boss')
92
+ peon = manager.json_employees.create!(name: 'Peon')
93
+ manager.json_employees.create!(name: 'Indentured servant')
94
+ manager.json_employees.create!(name: 'Serf', nickname: 'Vince')
95
95
  3.times do
96
96
  3.times do
97
97
  manager_short_json = manager.as_json(properties: :short)
98
- expect(manager_short_json.length).to eq(2)
98
+ expect(manager_short_json.length).to eq(3)
99
99
  expect(manager_short_json[:name]).to eq('Boss')
100
100
  expect(manager_short_json[:employees].member?(name: 'Peon')).to be_truthy
101
101
  expect(manager_short_json[:employees].member?(name: 'Indentured servant')).to be_truthy
@@ -104,7 +104,7 @@ describe Mongoid::CachedJson do
104
104
  end
105
105
  3.times do
106
106
  manager_public_json = manager.as_json(properties: :public)
107
- expect(manager_public_json.length).to eq(2)
107
+ expect(manager_public_json.length).to eq(3)
108
108
  expect(manager_public_json[:name]).to eq('Boss')
109
109
  expect(manager_public_json[:employees].member?(name: 'Peon')).to be_truthy
110
110
  expect(manager_public_json[:employees].member?(name: 'Indentured servant')).to be_truthy
@@ -113,7 +113,7 @@ describe Mongoid::CachedJson do
113
113
  end
114
114
  3.times do
115
115
  manager_all_json = manager.as_json(properties: :all)
116
- expect(manager_all_json.length).to eq(3)
116
+ expect(manager_all_json.length).to eq(4)
117
117
  expect(manager_all_json[:name]).to eq('Boss')
118
118
  expect(manager_all_json[:ssn]).to eq('123-45-6789')
119
119
  expect(manager_all_json[:employees].member?(name: 'Peon', nickname: 'My Favorite')).to be_truthy
@@ -129,18 +129,18 @@ describe Mongoid::CachedJson do
129
129
  end
130
130
  end
131
131
  it 'correctly updates fields when either the parent or child class changes' do
132
- manager = JsonManager.create(name: 'JsonManager')
133
- employee = manager.json_employees.create(name: 'JsonEmployee')
132
+ manager = JsonManager.create!(name: 'JsonManager')
133
+ employee = manager.json_employees.create!(name: 'JsonEmployee')
134
134
  3.times do
135
- expect(manager.as_json(properties: :short)).to eq(name: 'JsonManager', employees: [{ name: 'JsonEmployee' }])
135
+ expect(manager.as_json(properties: :short)).to eq(name: 'JsonManager', employees: [{ name: 'JsonEmployee' }], has_left_handed_employee: false)
136
136
  expect(employee.as_json(properties: :short)).to eq(name: 'JsonEmployee')
137
137
  end
138
138
  manager.name = 'New JsonManager'
139
139
  manager.save
140
- 3.times { expect(manager.as_json(properties: :short)).to eq(name: 'New JsonManager', employees: [{ name: 'JsonEmployee' }]) }
140
+ 3.times { expect(manager.as_json(properties: :short)).to eq(name: 'New JsonManager', employees: [{ name: 'JsonEmployee' }], has_left_handed_employee: false) }
141
141
  employee.name = 'New JsonEmployee'
142
142
  employee.save
143
- 3.times { expect(manager.as_json(properties: :short)).to eq(name: 'New JsonManager', employees: [{ name: 'New JsonEmployee' }]) }
143
+ 3.times { expect(manager.as_json(properties: :short)).to eq(name: 'New JsonManager', employees: [{ name: 'New JsonEmployee' }], has_left_handed_employee: false) }
144
144
  end
145
145
  context 'reference_properties' do
146
146
  it 'limits the json fields of a child relationship' do
@@ -220,6 +220,7 @@ describe Mongoid::CachedJson do
220
220
  end
221
221
  it 'should yield JSON without an instance of a child' do
222
222
  p = SecretParent.create(name: 'Parent')
223
+ expect(p.as_json(properties: :short)).to eq(name: 'Parent', child: nil)
223
224
  expect(p.as_json(properties: :all)[:child]).to be_nil
224
225
  end
225
226
  it 'should yield child JSON when as_json is called on the parent and hide_as_child_json_when returns false on an instance' do
@@ -230,8 +231,8 @@ describe Mongoid::CachedJson do
230
231
  it 'should not yield child JSON when as_json is called on the parent and hide_as_child_json_when returns true on an instance' do
231
232
  p = SecretParent.create(name: 'Parent')
232
233
  p.create_sometimes_secret(should_tell_secret: false)
233
- expect(p.as_json(properties: :short)).to eq(name: 'Parent', child: nil)
234
- expect(p.as_json(properties: :short)[:child]).to be_nil
234
+ expect(p.as_json(properties: :short)).to eq(name: 'Parent')
235
+ expect(p.as_json(properties: :short)).not_to have_key(:child)
235
236
  end
236
237
  end
237
238
  context 'relationships with a multi-level hierarchy' do
@@ -353,9 +354,9 @@ describe Mongoid::CachedJson do
353
354
  example = JsonFoobar.create(foo: 'FOO', baz: 'BAZ', bar: 'BAR')
354
355
  key = "as_json/unspecified/JsonFoobar/#{example.id}/short/true"
355
356
  case cache_store
356
- when :memory_store then
357
+ when :memory_store
357
358
  expect(Mongoid::CachedJson.config.cache).to receive(:fetch).with(key, force: true).twice
358
- when :dalli_store then
359
+ when :dalli_store, :mem_cache_store
359
360
  expect(Mongoid::CachedJson.config.cache).not_to receive(:write)
360
361
  else
361
362
  fail ArgumentError, "invalid cache store: #{cache_store}"
@@ -496,6 +497,14 @@ describe Mongoid::CachedJson do
496
497
  expect(@image.as_json(properties: :all)).to eq(name: 'Image', urls: [])
497
498
  end
498
499
  end
500
+ context 'evaluating a false-y value in a reference type' do
501
+ it 'correctly returns false instead of nil' do
502
+ manager = JsonManager.create!(name: 'Boss')
503
+ manager.json_employees.create!(name: 'Matt')
504
+ manager_short_json = manager.as_json(properties: :short)
505
+ expect(manager_short_json[:has_left_handed_employee]).to eq(false)
506
+ end
507
+ end
499
508
  end
500
509
  end
501
510
  end
data/spec/dalli_spec.rb CHANGED
@@ -1,18 +1,17 @@
1
1
  require 'spec_helper'
2
- require 'active_support/cache/dalli_store'
3
2
 
4
- describe ActiveSupport::Cache::DalliStore do
3
+ describe MEMCACHE_STORE_CLASS do
5
4
  before :each do
6
5
  @cache = Mongoid::CachedJson::Config.cache
7
6
  Mongoid::CachedJson.configure do |config|
8
- config.cache = ActiveSupport::Cache.lookup_store(:dalli_store)
7
+ config.cache = ActiveSupport::Cache.lookup_store(MEMCACHE_STORE)
9
8
  end
10
9
  end
11
10
  after :each do
12
11
  Mongoid::CachedJson::Config.cache = @cache
13
12
  end
14
13
  it 'uses dalli_store' do
15
- expect(Mongoid::CachedJson.config.cache).to be_a ActiveSupport::Cache::DalliStore
14
+ expect(Mongoid::CachedJson.config.cache).to be_a MEMCACHE_STORE_CLASS
16
15
  end
17
16
  context 'read_multi' do
18
17
  context 'array' do
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Per-field cache policy' do
4
+ before do
5
+ class FieldCacheDocument
6
+ include Mongoid::Document
7
+ include Mongoid::CachedJson
8
+
9
+ field :stored
10
+ field :live
11
+
12
+ json_fields stored: {}, live: { cache: false }
13
+ end
14
+
15
+ class FieldCacheChild
16
+ include Mongoid::Document
17
+ include Mongoid::CachedJson
18
+
19
+ field :value
20
+
21
+ json_fields value: {}
22
+ end
23
+
24
+ class FieldCacheParent
25
+ include Mongoid::Document
26
+ include Mongoid::CachedJson
27
+
28
+ field :name
29
+ attr_accessor :child
30
+
31
+ json_fields name: {}, child: { type: :reference, cache: false }
32
+ end
33
+ end
34
+
35
+ after do
36
+ [FieldCacheDocument, FieldCacheChild, FieldCacheParent].each(&:delete_all)
37
+ [:FieldCacheDocument, :FieldCacheChild, :FieldCacheParent].each { |name| Object.send(:remove_const, name) }
38
+ end
39
+
40
+ [true, false].each do |bulk|
41
+ context "with bulk reads #{bulk}" do
42
+ before do
43
+ @store = ActiveSupport::Cache::MemoryStore.new
44
+ @store.singleton_class.send(:undef_method, :read_multi) unless bulk
45
+ Mongoid::CachedJson.config.cache = @store
46
+ end
47
+
48
+ it 'recomputes an individual callable field without refreshing cached siblings' do
49
+ document = FieldCacheDocument.create!(stored: 'stored', live: 'first')
50
+ expect(document.as_json).to eq(stored: 'stored', live: 'first')
51
+
52
+ document.stored = 'unsaved'
53
+ document.live = 'second'
54
+ expect(document.as_json).to eq(stored: 'stored', live: 'second')
55
+ end
56
+
57
+ it 'bypasses the referenced model cache only for this field' do
58
+ parent = FieldCacheParent.create!(name: 'parent')
59
+ child = FieldCacheChild.create!(value: 'first')
60
+ parent.child = child
61
+ options = { properties: :short, version: :unspecified, is_top_level_json: false }
62
+ key = FieldCacheChild.cached_json_key(options, FieldCacheChild, child.id)
63
+
64
+ expect(parent.as_json).to eq(name: 'parent', child: { value: 'first' })
65
+ expect(@store.read(key)).to be_nil
66
+
67
+ child.value = 'second'
68
+ expect(parent.as_json).to eq(name: 'parent', child: { value: 'second' })
69
+ expect(@store.read(key)).to be_nil
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Per-model cache policy' do
4
+ before do
5
+ class UncachedDocument
6
+ include Mongoid::Document
7
+ include Mongoid::CachedJson
8
+ field :value
9
+ json_fields disable_caching: true, value: {}
10
+ end
11
+
12
+ class CachedContainer
13
+ include Mongoid::Document
14
+ include Mongoid::CachedJson
15
+ field :value
16
+ attr_accessor :child
17
+
18
+ json_fields value: {}, child: { type: :reference }
19
+ end
20
+ end
21
+
22
+ after do
23
+ [UncachedDocument, CachedContainer].each(&:delete_all)
24
+ [:UncachedDocument, :CachedContainer].each { |name| Object.send(:remove_const, name) }
25
+ end
26
+
27
+ [true, false].each do |bulk|
28
+ context "with bulk reads #{bulk}" do
29
+ before do
30
+ @store = ActiveSupport::Cache::MemoryStore.new
31
+ @store.singleton_class.send(:undef_method, :read_multi) unless bulk
32
+ Mongoid::CachedJson.config.cache = @store
33
+ end
34
+
35
+ it 'does not read or write shared cache for a disabled model' do
36
+ document = UncachedDocument.create!(value: 'first')
37
+ expect(@store).not_to receive(:read_multi)
38
+ expect(@store).not_to receive(:fetch)
39
+ expect(@store).not_to receive(:write)
40
+ expect(document.as_json).to eq(value: 'first')
41
+ document.value = 'second'
42
+ expect([document].as_json).to eq([{ value: 'second' }])
43
+ end
44
+
45
+ it 'keeps the parent cached while recomputing a disabled child' do
46
+ parent = CachedContainer.create!(value: 'parent')
47
+ child = UncachedDocument.create!(value: 'first')
48
+ parent.child = child
49
+ expect(parent.as_json).to eq(value: 'parent', child: { value: 'first' })
50
+ parent.value = 'not saved'
51
+ child.value = 'second'
52
+ expect(parent.as_json).to eq(value: 'parent', child: { value: 'second' })
53
+ end
54
+
55
+ it 'does not disable caching on other models' do
56
+ expect(CachedContainer.cached_json_disable_caching).to be_falsey
57
+ document = CachedContainer.create!(value: 'first')
58
+ document.as_json
59
+ document.value = 'second'
60
+ expect(document.as_json[:value]).to eq('first')
61
+ end
62
+ end
63
+ end
64
+ end
@@ -68,7 +68,8 @@ describe Mongoid::Criteria do
68
68
  )
69
69
  # read_multi returned only 2 of 3 things, don't call fetch, just store the third value
70
70
  expect(Mongoid::CachedJson.config.cache).not_to receive(:fetch)
71
- expect(Mongoid::CachedJson.config.cache).to receive(:write).with("as_json/unspecified/Tool/#{tool2.id}/all/true", name: 'screwdriver')
71
+ expected_json = { name: 'screwdriver' }
72
+ expect(Mongoid::CachedJson.config.cache).to receive(:write).with("as_json/unspecified/Tool/#{tool2.id}/all/true", expected_json)
72
73
  expect(tool_box.tools.as_json(properties: :all)).to eq([
73
74
  { tool_box: { x: :y }, x: :y },
74
75
  { tool_box: { x: :y }, name: 'screwdriver' }
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,11 @@
1
+ if ENV['COVERAGE'] == 'true'
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ track_files 'lib/**/*.rb'
5
+ add_filter '/spec/'
6
+ end
7
+ end
8
+
1
9
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
10
  $LOAD_PATH.unshift(File.dirname(__FILE__))
3
11
 
@@ -11,8 +19,22 @@ end
11
19
 
12
20
  Mongoid.configure do |config|
13
21
  config.connect_to('cached_json_test')
22
+ if Mongoid::VERSION.to_i >= 5
23
+ config.clients['default']['hosts'] = [ENV.fetch('MONGODB_HOST', 'localhost:27017')]
24
+ else
25
+ config.sessions['default']['hosts'] = [ENV.fetch('MONGODB_HOST', 'localhost:27017')]
26
+ end
14
27
  end
15
28
 
29
+ if ActiveSupport::VERSION::MAJOR >= 6
30
+ require 'active_support/cache/mem_cache_store'
31
+ else
32
+ require 'active_support/cache/dalli_store'
33
+ end
34
+
35
+ MEMCACHE_STORE = ActiveSupport::VERSION::MAJOR >= 6 ? :mem_cache_store : :dalli_store
36
+ MEMCACHE_STORE_CLASS = ActiveSupport::Cache.lookup_store(MEMCACHE_STORE).class
37
+
16
38
  RSpec.configure do |config|
17
39
  config.raise_errors_for_deprecations!
18
40
  config.after :each do
@@ -3,7 +3,11 @@ class FastJsonImage
3
3
  include Mongoid::CachedJson
4
4
 
5
5
  field :name, default: 'Image'
6
- belongs_to :fast_json_artwork
6
+ if Mongoid::Compatibility::Version.mongoid5_or_older?
7
+ belongs_to :fast_json_artwork
8
+ else
9
+ belongs_to :fast_json_artwork, required: false
10
+ end
7
11
  has_and_belongs_to_many :fast_json_urls
8
12
 
9
13
  json_fields \
@@ -4,6 +4,7 @@ class JsonEmployee
4
4
 
5
5
  field :name
6
6
  field :nickname, default: 'My Favorite'
7
+ field :is_left_handed, type: Boolean, default: false
7
8
  belongs_to :json_manager
8
9
 
9
10
  json_fields \
@@ -5,10 +5,20 @@ class JsonManager
5
5
  field :name
6
6
  field :ssn, default: '123-45-6789'
7
7
  has_many :json_employees
8
- belongs_to :supervisor, class_name: 'JsonSupervisor'
8
+
9
+ if Mongoid::Compatibility::Version.mongoid5_or_older?
10
+ belongs_to :supervisor, class_name: 'JsonSupervisor'
11
+ else
12
+ belongs_to :supervisor, class_name: 'JsonSupervisor', required: false
13
+ end
14
+
15
+ def has_left_handed_employee?
16
+ json_employees.any? { |e| e.is_left_handed? }
17
+ end
9
18
 
10
19
  json_fields \
11
20
  name: {},
12
21
  ssn: { properties: :all },
13
- employees: { type: :reference, definition: :json_employees }
22
+ employees: { type: :reference, definition: :json_employees },
23
+ has_left_handed_employee: { type: :reference, definition: :has_left_handed_employee? }
14
24
  end
data/spec/support/tool.rb CHANGED
@@ -3,7 +3,12 @@ class Tool
3
3
  include Mongoid::CachedJson
4
4
 
5
5
  field :name
6
- belongs_to :tool_box
6
+
7
+ if Mongoid::Compatibility::Version.mongoid5_or_older?
8
+ belongs_to :tool_box
9
+ else
10
+ belongs_to :tool_box, required: false
11
+ end
7
12
 
8
13
  json_fields \
9
14
  name: {},
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-cached-json
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.3
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Windsor
8
8
  - Daniel Doubrovkine
9
9
  - Frank Macreery
10
- autorequire:
10
+ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-09-17 00:00:00.000000000 Z
13
+ date: 2026-10-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: mongoid
@@ -26,23 +26,27 @@ dependencies:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
28
  version: '3.0'
29
- description:
29
+ description:
30
30
  email: dblock@dblock.org
31
31
  executables: []
32
32
  extensions: []
33
33
  extra_rdoc_files: []
34
34
  files:
35
+ - ".github/workflows/danger-comment.yml"
36
+ - ".github/workflows/danger.yml"
37
+ - ".github/workflows/test.yml"
35
38
  - ".gitignore"
36
39
  - ".rspec"
37
40
  - ".rubocop.yml"
38
41
  - ".rubocop_todo.yml"
39
- - ".travis.yml"
40
42
  - CHANGELOG.md
41
43
  - CONTRIBUTING.md
44
+ - Dangerfile
42
45
  - Gemfile
43
46
  - LICENSE.md
44
47
  - README.md
45
48
  - Rakefile
49
+ - UPGRADING.md
46
50
  - lib/mongoid-cached-json.rb
47
51
  - lib/mongoid-cached-json/array.rb
48
52
  - lib/mongoid-cached-json/cached_json.rb
@@ -52,12 +56,16 @@ files:
52
56
  - lib/mongoid-cached-json/mongoid_criteria.rb
53
57
  - lib/mongoid-cached-json/version.rb
54
58
  - mongoid-cached-json.gemspec
59
+ - spec/all_fields_spec.rb
55
60
  - spec/array_spec.rb
56
61
  - spec/benchmark_spec.rb
62
+ - spec/cache_version_spec.rb
57
63
  - spec/cached_json_spec.rb
58
64
  - spec/config_spec.rb
59
65
  - spec/dalli_spec.rb
66
+ - spec/field_cache_spec.rb
60
67
  - spec/hash_spec.rb
68
+ - spec/model_cache_spec.rb
61
69
  - spec/mongoid_criteria_spec.rb
62
70
  - spec/spec_helper.rb
63
71
  - spec/support/awesome_artwork.rb
@@ -87,11 +95,11 @@ files:
87
95
  - spec/support/sometimes_secret.rb
88
96
  - spec/support/tool.rb
89
97
  - spec/support/tool_box.rb
90
- homepage: http://github.com/dblock/mongoid-cached-json
98
+ homepage: http://github.com/mongoid/mongoid-cached-json
91
99
  licenses:
92
100
  - MIT
93
101
  metadata: {}
94
- post_install_message:
102
+ post_install_message:
95
103
  rdoc_options: []
96
104
  require_paths:
97
105
  - lib
@@ -106,10 +114,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
114
  - !ruby/object:Gem::Version
107
115
  version: 1.3.6
108
116
  requirements: []
109
- rubyforge_project:
110
- rubygems_version: 2.2.2
111
- signing_key:
117
+ rubygems_version: 3.5.16
118
+ signing_key:
112
119
  specification_version: 4
113
120
  summary: Cached-json is a DSL for describing JSON representations of Mongoid models.
114
121
  test_files: []
115
- has_rdoc:
data/.travis.yml DELETED
@@ -1,17 +0,0 @@
1
- sudo: false
2
-
3
- services:
4
- - mongodb
5
- - memcache
6
-
7
- rvm:
8
- - 1.9.3
9
- - 2.0.0
10
- - 2.2.0
11
-
12
- env:
13
- - MONGOID_VERSION=3
14
- - MONGOID_VERSION=4
15
- - MONGOID_VERSION=5
16
-
17
- cache: bundler