mongoid-cached-json 1.5.2 → 1.5.3

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
  SHA1:
3
- metadata.gz: 0e18f9babf5b55e3d785541737870a25b29ca0c0
4
- data.tar.gz: 20d28efe0aa10fe48518bfff03015033fa9199ce
3
+ metadata.gz: 11dccfcc261da8a4c6b4027696ab55b327070041
4
+ data.tar.gz: e32ddf96914f8e82bbda3a1ea07c9852f6a80c30
5
5
  SHA512:
6
- metadata.gz: 750a96034f2430ade4c8ff7ce27f18b36fb17a2c1173f3bb583f0c82dbd54cb1bdfee27872876291b9dd3106d453dd4dad445eb6d9081177aa4ae5bcdeb9bbcc
7
- data.tar.gz: d06238d96ef027691d0de888dc76f205964d9b8434511a8c521a09a3ec7a7aac6411f4bb05a765d0652e28a2f0de0699f44b8d83cd191889e12326d3b048caaf
6
+ metadata.gz: a2f59a4a67f3ba6707b74bd1bba88a34208414b11cff5252994f7e680d4d70f7b135fe5945931473f7aaf420a511eeda6ef7ec7409738b693ac0e4cf56ec83f7
7
+ data.tar.gz: ebec52181980a9b345bcfb10fbfd78aa3f839d65f4f693813a46da608b4dfe0c9db49e0ff4508bd28386b5788db9c544f1ad52b690e8c50dff1cf67439e0b017
data/.rubocop_todo.yml CHANGED
@@ -1,5 +1,6 @@
1
- # This configuration was generated by `rubocop --auto-gen-config`
2
- # on 2014-12-29 16:04:26 -0500 using RuboCop version 0.28.0.
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2015-09-17 10:10:32 -0400 using RuboCop version 0.33.0.
3
4
  # The point is for the user to remove these configuration records
4
5
  # one by one as the offenses are removed from the code base.
5
6
  # Note that changes in the inspected code, or installation of new
@@ -8,15 +9,19 @@
8
9
  # Offense count: 1
9
10
  # Configuration parameters: AllowSafeAssignment.
10
11
  Lint/AssignmentInCondition:
11
- Enabled: false
12
+ Exclude:
13
+ - 'lib/mongoid-cached-json/cached_json.rb'
12
14
 
13
15
  # Offense count: 1
14
16
  Lint/UnderscorePrefixedVariableName:
15
- Enabled: false
17
+ Exclude:
18
+ - 'lib/mongoid-cached-json/cached_json.rb'
16
19
 
17
20
  # Offense count: 2
18
21
  Lint/UselessAssignment:
19
- Enabled: false
22
+ Exclude:
23
+ - 'lib/mongoid-cached-json/cached_json.rb'
24
+ - 'spec/cached_json_spec.rb'
20
25
 
21
26
  # Offense count: 5
22
27
  Metrics/AbcSize:
@@ -30,7 +35,7 @@ Metrics/BlockNesting:
30
35
  Metrics/CyclomaticComplexity:
31
36
  Max: 15
32
37
 
33
- # Offense count: 201
38
+ # Offense count: 200
34
39
  # Configuration parameters: AllowURI, URISchemes.
35
40
  Metrics/LineLength:
36
41
  Max: 202
@@ -40,6 +45,11 @@ Metrics/LineLength:
40
45
  Metrics/MethodLength:
41
46
  Max: 36
42
47
 
48
+ # Offense count: 1
49
+ # Configuration parameters: CountComments.
50
+ Metrics/ModuleLength:
51
+ Max: 119
52
+
43
53
  # Offense count: 4
44
54
  Metrics/PerceivedComplexity:
45
55
  Max: 16
@@ -50,24 +60,34 @@ Style/Documentation:
50
60
 
51
61
  # Offense count: 2
52
62
  Style/DoubleNegation:
53
- Enabled: false
63
+ Exclude:
64
+ - 'lib/mongoid-cached-json/cached_json.rb'
54
65
 
55
66
  # Offense count: 1
56
67
  Style/EachWithObject:
57
- Enabled: false
68
+ Exclude:
69
+ - 'lib/mongoid-cached-json/hash.rb'
58
70
 
59
71
  # Offense count: 1
60
72
  # Configuration parameters: Exclude.
61
73
  Style/FileName:
62
- Enabled: false
74
+ Exclude:
75
+ - 'lib/mongoid-cached-json.rb'
63
76
 
64
77
  # Offense count: 5
78
+ # Cop supports --auto-correct.
65
79
  Style/Lambda:
66
- Enabled: false
80
+ Exclude:
81
+ - 'lib/mongoid-cached-json/cached_json.rb'
82
+ - 'spec/support/json_foobar.rb'
83
+ - 'spec/support/poly_company.rb'
84
+ - 'spec/support/poly_person.rb'
85
+ - 'spec/support/sometimes_secret.rb'
67
86
 
68
87
  # Offense count: 1
69
88
  Style/ModuleFunction:
70
- Enabled: false
89
+ Exclude:
90
+ - 'lib/mongoid-cached-json/config.rb'
71
91
 
72
92
  # Offense count: 1
73
93
  # Configuration parameters: EnforcedStyle, SupportedStyles.
@@ -77,4 +97,5 @@ Style/RaiseArgs:
77
97
  # Offense count: 2
78
98
  # Configuration parameters: Methods.
79
99
  Style/SingleLineBlockParams:
80
- Enabled: false
100
+ Exclude:
101
+ - 'spec/benchmark_spec.rb'
data/.travis.yml CHANGED
@@ -12,5 +12,6 @@ rvm:
12
12
  env:
13
13
  - MONGOID_VERSION=3
14
14
  - MONGOID_VERSION=4
15
+ - MONGOID_VERSION=5
15
16
 
16
17
  cache: bundler
data/CHANGELOG.md CHANGED
@@ -1,4 +1,9 @@
1
- 1.5.2 (29/12/2014)
1
+ 1.5.3 (2015/09/17)
2
+ ------------------
3
+
4
+ * Compatibility with Mongoid 5 - [@dblock](http://github.com/dblock).
5
+
6
+ 1.5.2 (2014/29/12)
2
7
  ------------------
3
8
 
4
9
  * Fixed support for Ruby 2.2.0 - [@dblock](http://github.com/dblock).
@@ -6,77 +11,77 @@
6
11
  * Upgraded to RSpec 3.1 - [@dblock](http://github.com/dblock).
7
12
  * Removed Jeweler - [@dblock](http://github.com/dblock).
8
13
 
9
- 1.5.1 (05/07/2013)
14
+ 1.5.1 (2013/05/07)
10
15
  --------------------
11
16
 
12
17
  * Fixed `read_multi` calls so as to enable proper cache reading behavior in stores other than `ActiveSupport::Cache::DalliStore` - [@macreery](http://github.com/macreery).
13
18
 
14
- 1.5 (04/13/2013)
19
+ 1.5 (2013/04/13)
15
20
  ----------------
16
21
 
17
22
  * Added `:reference_properties` that disables dynamic selection of the type of JSON to return for a reference - [@dblock](https://github.com/dblock).
18
23
 
19
- 1.4.3 (01/25/2013)
24
+ 1.4.3 (2013/01/25)
20
25
  ------------------
21
26
 
22
27
  * For caches that support `read_multi`, do not attempt to fetch JSON a second time via `fetch`, write it directly to cache - [@dblock](https://github.com/dblock).
23
28
 
24
- 1.4.2 (01/24/2013)
29
+ 1.4.2 (2013/01/24)
25
30
  ------------------
26
31
 
27
32
  * Fix: calling `as_json` on a destroyed Mongoid 3.1 object with a HABTM relationship raises `undefined method 'map' for nil:NilClass` - [@dblock](http://github.com/dblock).
28
33
 
29
- 1.4.1 (01/22/2013)
34
+ 1.4.1 (2013/01/22)
30
35
  ------------------
31
36
 
32
37
  * Invalidate cache in `after_destroy` - [@dblock](http://github.com/dblock).
33
38
  * Do not invalidate cache when the document is created - [@dblock](http://github.com/dblock).
34
39
  * Invalidate cache in `after_update` instead of `before_update` - [@dblock](http://github.com/dblock).
35
40
 
36
- 1.4 (01/20/2013)
41
+ 1.4 (2013/01/20)
37
42
  ---------------
38
43
 
39
44
  * Collect a JSON partial representation first, then fetch data from cache only once per-key - [@dblock](http://github.com/dblock), [@macreery](http://github.com/macreery).
40
45
  * Use `read_multi` if the cache store supports it to fetch data from cache in bulk - [@dblock](http://github.com/dblock), [@macreery](http://github.com/macreery).
41
46
  * Added a benchmark test suite - [@dblock](http://github.com/dblock), [@macreery](http://github.com/macreery).
42
47
 
43
- 1.3 (11/12/2012)
48
+ 1.3 (2012/11/12)
44
49
  ----------------
45
50
 
46
51
  * Removed requirement for `bson_ext`, support for Mongoid 3.0 - [@dblock](http://github.com/dblock).
47
52
 
48
- 1.2.3 (7/3/2012)
49
- ----------------
53
+ 1.2.3 (2012/07/03)
54
+ ------------------
50
55
 
51
56
  * Fix: including a `referenced_in` field in `json_fields` within a child `embedded_in` a parent causes an "access to the collection is not allowed since it is an embedded document" error - [@dblock](http://github.com/dblock).
52
57
 
53
- 1.2.2 (7/3/2012)
54
- ----------------
58
+ 1.2.2 (2012/07/03)
59
+ ------------------
55
60
 
56
61
  * Fix [#6](https://github.com/dblock/mongoid-cached-json/issues/6): including parent in `json_fields` within a polymorphic reference fails with an "uninitialized constant" error - [@dblock](http://github.com/dblock).
57
62
 
58
- 1.2.1 (6/12/2012)
59
- -----------------
63
+ 1.2.1 (2012/06/12)
64
+ ------------------
60
65
 
61
66
  * Allow `nil` parameter in as_json - [@dblock](http://github.com/dblock).
62
67
 
63
- 1.2.0 (5/28/2012)
68
+ 1.2.0 (2012/05/28)
64
69
  ------------------
65
70
 
66
71
  * Fix: cache key generation bug when using Mongoid 3 - [@marbemac](http://github.com/marbemac).
67
72
 
68
- 1.1.1 (3/21/2012)
69
- -----------------
73
+ 1.1.1 (2012/03/21)
74
+ ------------------
70
75
 
71
76
  * Fix: caching/invalidating referenced polymorphic documents - [@macreery](http://github.com/macreery).
72
77
 
73
- 1.1 (2/29/2012)
74
- ---------------
78
+ 1.1 (2012/02/29)
79
+ ----------------
75
80
 
76
81
  * Added support for versioning - [@dblock](http://github.com/dblock).
77
82
 
78
- 1.0 (2/20/2012)
79
- ---------------
83
+ 1.0 (2012/02/20)
84
+ ----------------
80
85
 
81
86
  * Initial release - [@aaw](http://github.com/aaw).
82
87
  * Retired support for `:markdown` in favor of `Mongoid::CachedJson::transform` - [@dblock](http://github.com/dblock).
data/CONTRIBUTING.md CHANGED
@@ -23,6 +23,15 @@ git pull upstream master
23
23
  git checkout -b my-feature-branch
24
24
  ```
25
25
 
26
+ #### Install Memcached and MongoDB
27
+
28
+ MongoDB and Memcached are required, use your favorite OS installer.
29
+
30
+ ```
31
+ brew install mongodb
32
+ brew install memcached
33
+ ```
34
+
26
35
  #### Bundle Install and Test
27
36
 
28
37
  Ensure that you can build the project and run tests.
data/Gemfile CHANGED
@@ -2,7 +2,9 @@ source 'http://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
- case version = ENV['MONGOID_VERSION'] || '~> 4.0'
5
+ case version = ENV['MONGOID_VERSION'] || '~> 5.0'
6
+ when /5/
7
+ gem 'mongoid', '~> 5.0'
6
8
  when /4/
7
9
  gem 'mongoid', '~> 4.0'
8
10
  when /3/
@@ -13,9 +15,13 @@ end
13
15
 
14
16
  group :development do
15
17
  gem 'rake'
16
- gem 'rspec', '~> 3.1'
17
18
  gem 'bundler', '~> 1.0'
18
19
  gem 'yard', '~> 0.6'
19
20
  gem 'dalli', '~> 2.6'
20
- gem 'rubocop', '0.28.0'
21
+ gem 'rubocop', '0.33.0'
22
+ end
23
+
24
+ group :test do
25
+ gem 'mongoid-compatibility'
26
+ gem 'rspec', '~> 3.1'
21
27
  end
data/README.md CHANGED
@@ -1,12 +1,22 @@
1
- Mongoid::CachedJson [![Build Status](https://secure.travis-ci.org/dblock/mongoid-cached-json.png)](http://travis-ci.org/dblock/mongoid-cached-json)
1
+ Mongoid::CachedJson
2
2
  ===================
3
3
 
4
+ [![Gem Version](https://badge.fury.io/rb/mongoid-cached-json.svg)](http://badge.fury.io/rb/mongoid-cached-json)
5
+ [![Build Status](https://travis-ci.org/dblock/mongoid-cached-json.svg?branch=master)](https://travis-ci.org/dblock/mongoid-cached-json)
6
+ [![Dependency Status](https://gemnasium.com/dblock/mongoid-cached-json.svg)](https://gemnasium.com/dblock/mongoid-cached-json)
7
+ [![Code Climate](https://codeclimate.com/github/dblock/mongoid-cached-json.svg)](https://codeclimate.com/github/dblock/mongoid-cached-json)
8
+
4
9
  Typical `as_json` definitions may involve lots of database point queries and method calls. When returning collections of objects, a single call may yield hundreds of database queries that can take seconds. This library mitigates the problem by implementing a module called `CachedJson`.
5
10
 
6
11
  `CachedJson` enables returning multiple JSON formats and versions from a single class and provides some rules for yielding embedded or referenced data. It then uses a scheme where fragments of JSON are cached for a particular (class, id) pair containing only the data that doesn't involve references/embedded documents. To get the full JSON for an instance, `CachedJson` will combine fragments of JSON from the instance with fragments representing the JSON for its references. In the best case, when all of these fragments are cached, this falls through to a few cache lookups followed by a couple Ruby hash merges to create the JSON.
7
12
 
8
13
  Using `Mongoid::CachedJson` we were able to cut our JSON API average response time by about a factor of 10.
9
14
 
15
+ Compatibility
16
+ -------------
17
+
18
+ This gem is compatible with Mongoid 3, 4 and 5.
19
+
10
20
  Resources
11
21
  ---------
12
22
 
@@ -76,10 +76,12 @@ module Mongoid
76
76
  is_top_level_json = options[:is_top_level_json] || false
77
77
  if object_def[:object]
78
78
  object_reference = object_def[:object]
79
- clazz, id = object_def[:object].class, object_def[:object].id
79
+ clazz = object_def[:object].class
80
+ id = object_def[:object].id
80
81
  else
81
82
  object_reference = nil
82
- clazz, id = object_def[:clazz], object_def[:id]
83
+ clazz = object_def[:clazz]
84
+ id = object_def[:id]
83
85
  end
84
86
  key = cached_json_key(options, clazz, id)
85
87
  json = { _ref: { _clazz: self, _key: key, _materialize_cached_json: [clazz, id, object_reference, options] } }
@@ -128,7 +130,7 @@ module Mongoid
128
130
  object_ids = object.send(key)
129
131
  if object_ids
130
132
  reference_json = object_ids.map do |id|
131
- materialize_keys, json = materialize_json(options, clazz: clazz, id: id)
133
+ materialize_keys, json = materialize_json(options, clazz: clazz, id: id)
132
134
  keys = keys ? keys.merge_set(materialize_keys) : materialize_keys
133
135
  json
134
136
  end.compact
@@ -179,7 +181,7 @@ module Mongoid
179
181
  Mongoid::CachedJson.config.cache.write(key, fetched_json) unless Mongoid::CachedJson.config.disable_caching
180
182
  else
181
183
  # fetch/write from cache
182
- fetched_json = (local_cache[key] = Mongoid::CachedJson.config.cache.fetch(key, force: !!Mongoid::CachedJson.config.disable_caching) do
184
+ fetched_json = (local_cache[key] = Mongoid::CachedJson.config.cache.fetch(key, force: !!Mongoid::CachedJson.config.disable_caching) do
183
185
  _ref[:_clazz].materialize_cached_json(* _ref[:_materialize_cached_json])
184
186
  end)
185
187
  end
@@ -203,7 +205,7 @@ module Mongoid
203
205
  # partial, unmaterialized JSON
204
206
  keys, partial_json = self.class.materialize_json({
205
207
  properties: :short, is_top_level_json: true, version: Mongoid::CachedJson.config.default_version
206
- }.merge(options), object: self)
208
+ }.merge(options), object: self)
207
209
  [keys, partial_json]
208
210
  end
209
211
 
@@ -42,7 +42,7 @@ module Mongoid
42
42
  end
43
43
 
44
44
  # Disable caching.
45
- option :disable_caching, default: false
45
+ option :disable_caching, default: false
46
46
 
47
47
  # Returns the default JSON version
48
48
  #
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module CachedJson
3
- VERSION = '1.5.2'
3
+ VERSION = '1.5.3'
4
4
  end
5
5
  end
@@ -331,7 +331,7 @@ describe Mongoid::CachedJson do
331
331
  expect(JsonTransform.new(upcase: 'upcase', downcase: 'DOWNCASE', nochange: 'eLiTe').as_json).to eq(upcase: 'UPCASE', downcase: 'downcase', nochange: 'eLiTe')
332
332
  end
333
333
  end
334
- context 'with mutliple transformations' do
334
+ context 'with multiple transformations' do
335
335
  before :each do
336
336
  Mongoid::CachedJson.config.transform do |_field, _definition, value|
337
337
  value.to_i + 1
@@ -354,7 +354,7 @@ describe Mongoid::CachedJson do
354
354
  key = "as_json/unspecified/JsonFoobar/#{example.id}/short/true"
355
355
  case cache_store
356
356
  when :memory_store then
357
- expect(Mongoid::CachedJson.config.cache).to receive(:fetch).with(key, force: true).twice
357
+ expect(Mongoid::CachedJson.config.cache).to receive(:fetch).with(key, force: true).twice
358
358
  when :dalli_store then
359
359
  expect(Mongoid::CachedJson.config.cache).not_to receive(:write)
360
360
  else
@@ -418,7 +418,7 @@ describe Mongoid::CachedJson do
418
418
  expect(@json_parent_foobar.as_json(properties: :all)[:json_polymorphic_referenced_foobar][:foo]).to eq('REFERENCED')
419
419
  end
420
420
  end
421
- context 'polymorhphic relationships' do
421
+ context 'polymorphic relationships' do
422
422
  before :each do
423
423
  @company = PolyCompany.create!
424
424
  @company_post = PolyPost.create!(postable: @company)
@@ -448,7 +448,7 @@ describe Mongoid::CachedJson do
448
448
  { nickname: 'Joe', person: { name: 'Joe' } },
449
449
  { nickname: 'Bob', person: { name: 'Bob' } }
450
450
  ]
451
- )
451
+ )
452
452
  end
453
453
  end
454
454
  context 'with repeated objects in the JSON' do
@@ -464,7 +464,7 @@ describe Mongoid::CachedJson do
464
464
  { nickname: 'Joe', person: { name: 'Evil' } },
465
465
  { nickname: 'Bob', person: { name: 'Evil' } }
466
466
  ]
467
- )
467
+ )
468
468
  end
469
469
  end
470
470
  context 'belongs_to relationship' do
@@ -68,7 +68,7 @@ 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
+ expect(Mongoid::CachedJson.config.cache).to receive(:write).with("as_json/unspecified/Tool/#{tool2.id}/all/true", name: 'screwdriver')
72
72
  expect(tool_box.tools.as_json(properties: :all)).to eq([
73
73
  { tool_box: { x: :y }, x: :y },
74
74
  { tool_box: { x: :y }, name: 'screwdriver' }
data/spec/spec_helper.rb CHANGED
@@ -3,6 +3,7 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
3
 
4
4
  require 'rspec'
5
5
  require 'mongoid-cached-json'
6
+ require 'mongoid-compatibility'
6
7
 
7
8
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each do |f|
8
9
  require f
@@ -17,4 +18,8 @@ RSpec.configure do |config|
17
18
  config.after :each do
18
19
  Mongoid::CachedJson.config.reset!
19
20
  end
21
+ config.before :all do
22
+ Mongoid.logger.level = Logger::INFO
23
+ Mongo::Logger.logger.level = Logger::INFO if Mongoid::Compatibility::Version.mongoid5?
24
+ end
20
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-cached-json
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.2
4
+ version: 1.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Windsor
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-12-29 00:00:00.000000000 Z
13
+ date: 2015-09-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: mongoid
@@ -107,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
107
  version: 1.3.6
108
108
  requirements: []
109
109
  rubyforge_project:
110
- rubygems_version: 2.4.5
110
+ rubygems_version: 2.2.2
111
111
  signing_key:
112
112
  specification_version: 4
113
113
  summary: Cached-json is a DSL for describing JSON representations of Mongoid models.