mongoid-cached-json 1.5.1 → 1.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +21 -0
- data/.rspec +3 -0
- data/.rubocop.yml +6 -0
- data/.rubocop_todo.yml +80 -0
- data/.travis.yml +16 -0
- data/CHANGELOG.md +85 -0
- data/CONTRIBUTING.md +118 -0
- data/Gemfile +21 -0
- data/README.md +49 -52
- data/Rakefile +34 -0
- data/lib/mongoid-cached-json.rb +0 -1
- data/lib/mongoid-cached-json/array.rb +1 -3
- data/lib/mongoid-cached-json/cached_json.rb +52 -50
- data/lib/mongoid-cached-json/config.rb +20 -21
- data/lib/mongoid-cached-json/hash.rb +1 -3
- data/lib/mongoid-cached-json/key_references.rb +0 -3
- data/lib/mongoid-cached-json/mongoid_criteria.rb +1 -3
- data/lib/mongoid-cached-json/version.rb +1 -2
- data/mongoid-cached-json.gemspec +17 -0
- data/spec/array_spec.rb +48 -0
- data/spec/benchmark_spec.rb +115 -0
- data/spec/cached_json_spec.rb +501 -0
- data/spec/config_spec.rb +21 -0
- data/spec/dalli_spec.rb +37 -0
- data/spec/hash_spec.rb +66 -0
- data/spec/mongoid_criteria_spec.rb +78 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/support/awesome_artwork.rb +11 -0
- data/spec/support/awesome_image.rb +14 -0
- data/spec/support/fast_json_artwork.rb +15 -0
- data/spec/support/fast_json_image.rb +12 -0
- data/spec/support/fast_json_url.rb +12 -0
- data/spec/support/json_embedded_foobar.rb +5 -0
- data/spec/support/json_employee.rb +12 -0
- data/spec/support/json_foobar.rb +19 -0
- data/spec/support/json_manager.rb +14 -0
- data/spec/support/json_math.rb +9 -0
- data/spec/support/json_parent_foobar.rb +11 -0
- data/spec/support/json_polymorphic_embedded_foobar.rb +9 -0
- data/spec/support/json_polymorphic_referenced_foobar.rb +9 -0
- data/spec/support/json_referenced_foobar.rb +5 -0
- data/spec/support/json_supervisor.rb +13 -0
- data/spec/support/json_transform.rb +13 -0
- data/spec/support/matchers/invalidate.rb +22 -0
- data/spec/support/person.rb +20 -0
- data/spec/support/poly_company.rb +10 -0
- data/spec/support/poly_person.rb +10 -0
- data/spec/support/poly_post.rb +9 -0
- data/spec/support/prison_cell.rb +11 -0
- data/spec/support/prison_inmate.rb +12 -0
- data/spec/support/secret_parent.rb +11 -0
- data/spec/support/sometimes_secret.rb +11 -0
- data/spec/support/tool.rb +11 -0
- data/spec/support/tool_box.rb +10 -0
- metadata +62 -137
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0e18f9babf5b55e3d785541737870a25b29ca0c0
|
4
|
+
data.tar.gz: 20d28efe0aa10fe48518bfff03015033fa9199ce
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 750a96034f2430ade4c8ff7ce27f18b36fb17a2c1173f3bb583f0c82dbd54cb1bdfee27872876291b9dd3106d453dd4dad445eb6d9081177aa4ae5bcdeb9bbcc
|
7
|
+
data.tar.gz: d06238d96ef027691d0de888dc76f205964d9b8434511a8c521a09a3ec7a7aac6411f4bb05a765d0652e28a2f0de0699f44b8d83cd191889e12326d3b048caaf
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,80 @@
|
|
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.
|
3
|
+
# The point is for the user to remove these configuration records
|
4
|
+
# one by one as the offenses are removed from the code base.
|
5
|
+
# Note that changes in the inspected code, or installation of new
|
6
|
+
# versions of RuboCop, may require this file to be generated again.
|
7
|
+
|
8
|
+
# Offense count: 1
|
9
|
+
# Configuration parameters: AllowSafeAssignment.
|
10
|
+
Lint/AssignmentInCondition:
|
11
|
+
Enabled: false
|
12
|
+
|
13
|
+
# Offense count: 1
|
14
|
+
Lint/UnderscorePrefixedVariableName:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
# Offense count: 2
|
18
|
+
Lint/UselessAssignment:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
# Offense count: 5
|
22
|
+
Metrics/AbcSize:
|
23
|
+
Max: 54
|
24
|
+
|
25
|
+
# Offense count: 2
|
26
|
+
Metrics/BlockNesting:
|
27
|
+
Max: 4
|
28
|
+
|
29
|
+
# Offense count: 4
|
30
|
+
Metrics/CyclomaticComplexity:
|
31
|
+
Max: 15
|
32
|
+
|
33
|
+
# Offense count: 201
|
34
|
+
# Configuration parameters: AllowURI, URISchemes.
|
35
|
+
Metrics/LineLength:
|
36
|
+
Max: 202
|
37
|
+
|
38
|
+
# Offense count: 10
|
39
|
+
# Configuration parameters: CountComments.
|
40
|
+
Metrics/MethodLength:
|
41
|
+
Max: 36
|
42
|
+
|
43
|
+
# Offense count: 4
|
44
|
+
Metrics/PerceivedComplexity:
|
45
|
+
Max: 16
|
46
|
+
|
47
|
+
# Offense count: 34
|
48
|
+
Style/Documentation:
|
49
|
+
Enabled: false
|
50
|
+
|
51
|
+
# Offense count: 2
|
52
|
+
Style/DoubleNegation:
|
53
|
+
Enabled: false
|
54
|
+
|
55
|
+
# Offense count: 1
|
56
|
+
Style/EachWithObject:
|
57
|
+
Enabled: false
|
58
|
+
|
59
|
+
# Offense count: 1
|
60
|
+
# Configuration parameters: Exclude.
|
61
|
+
Style/FileName:
|
62
|
+
Enabled: false
|
63
|
+
|
64
|
+
# Offense count: 5
|
65
|
+
Style/Lambda:
|
66
|
+
Enabled: false
|
67
|
+
|
68
|
+
# Offense count: 1
|
69
|
+
Style/ModuleFunction:
|
70
|
+
Enabled: false
|
71
|
+
|
72
|
+
# Offense count: 1
|
73
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
74
|
+
Style/RaiseArgs:
|
75
|
+
Enabled: false
|
76
|
+
|
77
|
+
# Offense count: 2
|
78
|
+
# Configuration parameters: Methods.
|
79
|
+
Style/SingleLineBlockParams:
|
80
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
1.5.2 (29/12/2014)
|
2
|
+
------------------
|
3
|
+
|
4
|
+
* Fixed support for Ruby 2.2.0 - [@dblock](http://github.com/dblock).
|
5
|
+
* Implemented RuboCop, Ruby-style linter - [@dblock](http://github.com/dblock).
|
6
|
+
* Upgraded to RSpec 3.1 - [@dblock](http://github.com/dblock).
|
7
|
+
* Removed Jeweler - [@dblock](http://github.com/dblock).
|
8
|
+
|
9
|
+
1.5.1 (05/07/2013)
|
10
|
+
--------------------
|
11
|
+
|
12
|
+
* 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
|
+
|
14
|
+
1.5 (04/13/2013)
|
15
|
+
----------------
|
16
|
+
|
17
|
+
* Added `:reference_properties` that disables dynamic selection of the type of JSON to return for a reference - [@dblock](https://github.com/dblock).
|
18
|
+
|
19
|
+
1.4.3 (01/25/2013)
|
20
|
+
------------------
|
21
|
+
|
22
|
+
* 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
|
+
|
24
|
+
1.4.2 (01/24/2013)
|
25
|
+
------------------
|
26
|
+
|
27
|
+
* 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
|
+
|
29
|
+
1.4.1 (01/22/2013)
|
30
|
+
------------------
|
31
|
+
|
32
|
+
* Invalidate cache in `after_destroy` - [@dblock](http://github.com/dblock).
|
33
|
+
* Do not invalidate cache when the document is created - [@dblock](http://github.com/dblock).
|
34
|
+
* Invalidate cache in `after_update` instead of `before_update` - [@dblock](http://github.com/dblock).
|
35
|
+
|
36
|
+
1.4 (01/20/2013)
|
37
|
+
---------------
|
38
|
+
|
39
|
+
* 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
|
+
* 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
|
+
* Added a benchmark test suite - [@dblock](http://github.com/dblock), [@macreery](http://github.com/macreery).
|
42
|
+
|
43
|
+
1.3 (11/12/2012)
|
44
|
+
----------------
|
45
|
+
|
46
|
+
* Removed requirement for `bson_ext`, support for Mongoid 3.0 - [@dblock](http://github.com/dblock).
|
47
|
+
|
48
|
+
1.2.3 (7/3/2012)
|
49
|
+
----------------
|
50
|
+
|
51
|
+
* 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
|
+
|
53
|
+
1.2.2 (7/3/2012)
|
54
|
+
----------------
|
55
|
+
|
56
|
+
* 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
|
+
|
58
|
+
1.2.1 (6/12/2012)
|
59
|
+
-----------------
|
60
|
+
|
61
|
+
* Allow `nil` parameter in as_json - [@dblock](http://github.com/dblock).
|
62
|
+
|
63
|
+
1.2.0 (5/28/2012)
|
64
|
+
------------------
|
65
|
+
|
66
|
+
* Fix: cache key generation bug when using Mongoid 3 - [@marbemac](http://github.com/marbemac).
|
67
|
+
|
68
|
+
1.1.1 (3/21/2012)
|
69
|
+
-----------------
|
70
|
+
|
71
|
+
* Fix: caching/invalidating referenced polymorphic documents - [@macreery](http://github.com/macreery).
|
72
|
+
|
73
|
+
1.1 (2/29/2012)
|
74
|
+
---------------
|
75
|
+
|
76
|
+
* Added support for versioning - [@dblock](http://github.com/dblock).
|
77
|
+
|
78
|
+
1.0 (2/20/2012)
|
79
|
+
---------------
|
80
|
+
|
81
|
+
* Initial release - [@aaw](http://github.com/aaw).
|
82
|
+
* Retired support for `:markdown` in favor of `Mongoid::CachedJson::transform` - [@dblock](http://github.com/dblock).
|
83
|
+
* Added `Mongoid::CachedJson::configure` - [@dblock](http://github.com/dblock).
|
84
|
+
* Added support for `:markdown` - [@macreery](http://github.com/macreery).
|
85
|
+
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
Contributing to Mongoid::CachedJson
|
2
|
+
===================================
|
3
|
+
|
4
|
+
Mongoid::CachedJson is work of [many of contributors](https://github.com/dblock/mongoid-cached-json/graphs/contributors). You're encouraged to submit [pull requests](https://github.com/dblock/mongoid-cached-json/pulls), [propose features and discuss issues](https://github.com/dblock/mongoid-cached-json/issues). When in doubt, ask a question in the [Google Group](http://groups.google.com/group/mongoid-cached-json).
|
5
|
+
|
6
|
+
#### Fork the Project
|
7
|
+
|
8
|
+
Fork the [project on Github](https://github.com/dblock/mongoid-cached-json) and check out your copy.
|
9
|
+
|
10
|
+
```
|
11
|
+
git clone https://github.com/contributor/mongoid-cached-json.git
|
12
|
+
cd mongoid-cached-json
|
13
|
+
git remote add upstream https://github.com/dblock/mongoid-cached-json.git
|
14
|
+
```
|
15
|
+
|
16
|
+
#### Create a Topic Branch
|
17
|
+
|
18
|
+
Make sure your fork is up-to-date and create a topic branch for your feature or bug fix.
|
19
|
+
|
20
|
+
```
|
21
|
+
git checkout master
|
22
|
+
git pull upstream master
|
23
|
+
git checkout -b my-feature-branch
|
24
|
+
```
|
25
|
+
|
26
|
+
#### Bundle Install and Test
|
27
|
+
|
28
|
+
Ensure that you can build the project and run tests.
|
29
|
+
|
30
|
+
```
|
31
|
+
bundle install
|
32
|
+
bundle exec rake
|
33
|
+
```
|
34
|
+
|
35
|
+
#### Write Tests
|
36
|
+
|
37
|
+
Try to write a test that reproduces the problem you're trying to fix or describes a feature that you want to build.
|
38
|
+
|
39
|
+
We definitely appreciate pull requests that highlight or reproduce a problem, even without a fix.
|
40
|
+
|
41
|
+
#### Write Code
|
42
|
+
|
43
|
+
Implement your feature or bug fix.
|
44
|
+
|
45
|
+
Ruby style is enforced with [Rubocop](https://github.com/bbatsov/rubocop), run `bundle exec rubocop` and fix any style issues highlighted.
|
46
|
+
|
47
|
+
Make sure that `bundle exec rake` completes without errors.
|
48
|
+
|
49
|
+
#### Write Documentation
|
50
|
+
|
51
|
+
Document any external behavior in the [README](README.md).
|
52
|
+
|
53
|
+
#### Update Changelog
|
54
|
+
|
55
|
+
Add a line to [CHANGELOG](CHANGELOG.md) under *Next Release*. Make it look like every other line, including your name and link to your Github account.
|
56
|
+
|
57
|
+
#### Commit Changes
|
58
|
+
|
59
|
+
Make sure git knows your name and email address:
|
60
|
+
|
61
|
+
```
|
62
|
+
git config --global user.name "Your Name"
|
63
|
+
git config --global user.email "contributor@example.com"
|
64
|
+
```
|
65
|
+
|
66
|
+
Writing good commit logs is important. A commit log should describe what changed and why.
|
67
|
+
|
68
|
+
```
|
69
|
+
git add ...
|
70
|
+
git commit
|
71
|
+
```
|
72
|
+
|
73
|
+
#### Push
|
74
|
+
|
75
|
+
```
|
76
|
+
git push origin my-feature-branch
|
77
|
+
```
|
78
|
+
|
79
|
+
#### Make a Pull Request
|
80
|
+
|
81
|
+
Go to https://github.com/contributor/mongoid-cached-json and select your feature branch. Click the 'Pull Request' button and fill out the form. Pull requests are usually reviewed within a few days.
|
82
|
+
|
83
|
+
#### Rebase
|
84
|
+
|
85
|
+
If you've been working on a change for a while, rebase with upstream/master.
|
86
|
+
|
87
|
+
```
|
88
|
+
git fetch upstream
|
89
|
+
git rebase upstream/master
|
90
|
+
git push origin my-feature-branch -f
|
91
|
+
```
|
92
|
+
|
93
|
+
#### Update CHANGELOG Again
|
94
|
+
|
95
|
+
Update the [CHANGELOG](CHANGELOG.md) with the pull request number. A typical entry looks as follows.
|
96
|
+
|
97
|
+
```
|
98
|
+
* [#123](https://github.com/dblock/mongoid-cached-json/pull/123): Reticulated splines - [@contributor](https://github.com/contributor).
|
99
|
+
```
|
100
|
+
|
101
|
+
Amend your previous commit and force push the changes.
|
102
|
+
|
103
|
+
```
|
104
|
+
git commit --amend
|
105
|
+
git push origin my-feature-branch -f
|
106
|
+
```
|
107
|
+
|
108
|
+
#### Check on Your Pull Request
|
109
|
+
|
110
|
+
Go back to your pull request after a few minutes and see whether it passed muster with Travis-CI. Everything should look green, otherwise fix issues and amend your commit as described above.
|
111
|
+
|
112
|
+
#### Be Patient
|
113
|
+
|
114
|
+
It's likely that your change will not be merged and that the nitpicky maintainers will ask you to do more, or fix seemingly benign problems. Hang on there!
|
115
|
+
|
116
|
+
#### Thank You
|
117
|
+
|
118
|
+
Please do know that we really appreciate and value your time and work. We love you, really.
|
data/Gemfile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
case version = ENV['MONGOID_VERSION'] || '~> 4.0'
|
6
|
+
when /4/
|
7
|
+
gem 'mongoid', '~> 4.0'
|
8
|
+
when /3/
|
9
|
+
gem 'mongoid', '~> 3.1'
|
10
|
+
else
|
11
|
+
gem 'mongoid', version
|
12
|
+
end
|
13
|
+
|
14
|
+
group :development do
|
15
|
+
gem 'rake'
|
16
|
+
gem 'rspec', '~> 3.1'
|
17
|
+
gem 'bundler', '~> 1.0'
|
18
|
+
gem 'yard', '~> 0.6'
|
19
|
+
gem 'dalli', '~> 2.6'
|
20
|
+
gem 'rubocop', '0.28.0'
|
21
|
+
end
|
data/README.md
CHANGED
@@ -18,7 +18,7 @@ Quickstart
|
|
18
18
|
|
19
19
|
Add `mongoid-cached-json` to your Gemfile.
|
20
20
|
|
21
|
-
gem
|
21
|
+
gem 'mongoid-cached-json'
|
22
22
|
|
23
23
|
Include `Mongoid::CachedJson` in your models.
|
24
24
|
|
@@ -32,8 +32,8 @@ class Gadget
|
|
32
32
|
belongs_to :widget
|
33
33
|
|
34
34
|
json_fields \
|
35
|
-
:
|
36
|
-
:
|
35
|
+
name: {},
|
36
|
+
extras: { properties: :public }
|
37
37
|
|
38
38
|
end
|
39
39
|
|
@@ -44,8 +44,8 @@ class Widget
|
|
44
44
|
has_many :gadgets
|
45
45
|
|
46
46
|
json_fields \
|
47
|
-
:
|
48
|
-
:
|
47
|
+
name: {},
|
48
|
+
gadgets: { type: :reference, properties: :public }
|
49
49
|
|
50
50
|
end
|
51
51
|
```
|
@@ -59,13 +59,13 @@ widget = Widget.first
|
|
59
59
|
widget.as_json
|
60
60
|
|
61
61
|
# equivalent to the above
|
62
|
-
widget.as_json(
|
62
|
+
widget.as_json(properties: :short)
|
63
63
|
|
64
64
|
# `:public` version of the JSON, `gadgets` returned with `:short` JSON, no `:extras`
|
65
|
-
widget.as_json(
|
65
|
+
widget.as_json(properties: :public)
|
66
66
|
|
67
67
|
# `:all` version of the JSON, `gadgets` returned with `:all` JSON, including `:extras`
|
68
|
-
widget.as_json(
|
68
|
+
widget.as_json(properties: :all)
|
69
69
|
```
|
70
70
|
|
71
71
|
Configuration
|
@@ -92,11 +92,11 @@ Defining Fields
|
|
92
92
|
|
93
93
|
`Mongoid::CachedJson` supports the following options:
|
94
94
|
|
95
|
-
* `:hide_as_child_json_when` is an optional function that hides the child JSON from `as_json` parent objects, eg. `cached_json :
|
95
|
+
* `:hide_as_child_json_when` is an optional function that hides the child JSON from `as_json` parent objects, eg. `cached_json hide_as_child_json_when: lambda { |instance| ! instance.secret? }`
|
96
96
|
|
97
97
|
`Mongoid::CachedJson` field definitions support the following options:
|
98
98
|
|
99
|
-
* `:definition` can be a symbol or an anonymous function, eg.
|
99
|
+
* `:definition` can be a symbol or an anonymous function, eg. `description: { definition: :name }` or `description: { definition: lambda { |instance| instance.name } }`
|
100
100
|
* `:type` can be `:reference`, required for referenced objects
|
101
101
|
* `:properties` can be one of `:short`, `:public`, `:all`, in this order
|
102
102
|
* `:version` can be a single version for this field to appear in
|
@@ -115,7 +115,7 @@ When calling `as_json` on a model that contains references to other models the v
|
|
115
115
|
|
116
116
|
The dynamic selection where `:public` generates `:short` references allows to return smaller embedded collections, while `:all` allows to fetch deep data. Another way of looking at this is to say that a field in a `:short` JSON appears in collections, a field declared in the `:public` JSON appears for all users and the field declared in the `:all` JSON appears for object owners only.
|
117
117
|
|
118
|
-
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(
|
118
|
+
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.
|
119
119
|
|
120
120
|
``` ruby
|
121
121
|
class Person
|
@@ -124,12 +124,12 @@ class Person
|
|
124
124
|
|
125
125
|
field :name
|
126
126
|
field :ssn
|
127
|
-
has_and_belongs_to_many :friends, :
|
127
|
+
has_and_belongs_to_many :friends, class_name: 'Person'
|
128
128
|
|
129
129
|
json_fields \
|
130
|
-
:
|
131
|
-
:
|
132
|
-
friends: { :
|
130
|
+
name: {},
|
131
|
+
ssn: { properties: :all },
|
132
|
+
friends: { properties: :public, reference_properties: :short }
|
133
133
|
|
134
134
|
end
|
135
135
|
```
|
@@ -148,24 +148,24 @@ class Person
|
|
148
148
|
field :last
|
149
149
|
|
150
150
|
def name
|
151
|
-
[ first, middle, last ].compact.join(
|
151
|
+
[ first, middle, last ].compact.join(' ')
|
152
152
|
end
|
153
153
|
|
154
154
|
json_fields \
|
155
|
-
:
|
156
|
-
:
|
157
|
-
:
|
158
|
-
:
|
159
|
-
:
|
155
|
+
first: { versions: [ :v2, :v3 ] },
|
156
|
+
last: { versions: [ :v2, :v3 ] },
|
157
|
+
middle: { versions: [ :v2, :v3 ] },
|
158
|
+
born: { versions: :v3 },
|
159
|
+
name: { definition: :name }
|
160
160
|
|
161
161
|
end
|
162
162
|
```
|
163
163
|
|
164
164
|
``` ruby
|
165
|
-
person = Person.create(
|
166
|
-
person.as_json # { :
|
167
|
-
person.as_json(
|
168
|
-
person.as_json(
|
165
|
+
person = Person.create(first: 'John', middle: 'F.', last: 'Kennedy', born: 'May 29, 1917')
|
166
|
+
person.as_json # { name: 'John F. Kennedy' }
|
167
|
+
person.as_json(version: :v2) # { first: 'John', middle: 'F.', last: 'Kennedy', name: 'John F. Kennedy' }
|
168
|
+
person.as_json(version: :v3) # { first: 'John', middle: 'F.', last: 'Kennedy', name: 'John F. Kennedy', born: 'May 29, 1917' }
|
169
169
|
```
|
170
170
|
|
171
171
|
Transformations
|
@@ -182,14 +182,14 @@ class Widget
|
|
182
182
|
field :description
|
183
183
|
|
184
184
|
json_fields \
|
185
|
-
:
|
186
|
-
:
|
185
|
+
name: { trusted: true },
|
186
|
+
description: {}
|
187
187
|
end
|
188
188
|
```
|
189
189
|
|
190
190
|
``` ruby
|
191
191
|
Mongoid::CachedJson.config.transform do |field, definition, value|
|
192
|
-
trusted = !!
|
192
|
+
trusted = !!definition[:trusted]
|
193
193
|
trusted ? value : CGI.escapeHTML(value)
|
194
194
|
end
|
195
195
|
```
|
@@ -210,12 +210,12 @@ Testing JSON
|
|
210
210
|
This library overrides `as_json`, hence testing JSON results can be done at model level.
|
211
211
|
|
212
212
|
``` ruby
|
213
|
-
describe
|
213
|
+
describe 'as_json' do
|
214
214
|
before :each do
|
215
|
-
@person = Person.create!(
|
215
|
+
@person = Person.create!(first: 'John', last: 'Doe')
|
216
216
|
end
|
217
|
-
it
|
218
|
-
@person.as_json(
|
217
|
+
it 'returns name' do
|
218
|
+
expect(@person.as_json(properties: :public)[:name]).to eql 'John Doe'
|
219
219
|
end
|
220
220
|
end
|
221
221
|
```
|
@@ -223,13 +223,13 @@ end
|
|
223
223
|
It's also common to test the results of the API using the [Pathy](https://github.com/twoism/pathy) library.
|
224
224
|
|
225
225
|
``` ruby
|
226
|
-
describe
|
226
|
+
describe 'as_json' do
|
227
227
|
before :each do
|
228
|
-
person = Person.create!(
|
228
|
+
person = Person.create!(first: 'John', last: 'Doe')
|
229
229
|
end
|
230
|
-
it
|
230
|
+
it 'returns name' do
|
231
231
|
get "/api/person/#{person.id}"
|
232
|
-
response.body.at_json_path(
|
232
|
+
expect(response.body.at_json_path('name')).to eql 'John Doe'
|
233
233
|
end
|
234
234
|
end
|
235
235
|
```
|
@@ -240,24 +240,24 @@ Testing Cache Invalidation
|
|
240
240
|
Cache is invalidated by calling `:expire_cached_json` on an instance.
|
241
241
|
|
242
242
|
``` ruby
|
243
|
-
describe
|
243
|
+
describe 'updating a person' do
|
244
244
|
before :each
|
245
|
-
@person = Person.create!(
|
245
|
+
@person = Person.create!(name: 'John Doe')
|
246
246
|
end
|
247
|
-
it
|
248
|
-
@person.
|
249
|
-
@person.update_attributes!(
|
247
|
+
it 'invalidates cache' do
|
248
|
+
expect(@person).to receive(:expire_cached_json)
|
249
|
+
@person.update_attributes!(name: 'updated')
|
250
250
|
end
|
251
251
|
end
|
252
252
|
```
|
253
|
-
You may also want to use [this RSpec matcher](
|
253
|
+
You may also want to use [this RSpec matcher](spec/support/matchers/invalidate.rb).
|
254
254
|
|
255
255
|
```ruby
|
256
|
-
describe
|
257
|
-
it
|
258
|
-
|
259
|
-
@person.update_attributes!(
|
260
|
-
|
256
|
+
describe 'updating a person' do
|
257
|
+
it 'invalidates cache' do
|
258
|
+
expect do
|
259
|
+
@person.update_attributes!(name: 'updated')
|
260
|
+
end.to invalidate @person
|
261
261
|
end
|
262
262
|
end
|
263
263
|
```
|
@@ -278,14 +278,11 @@ Various cache stores, including Memcached, support bulk read operations. The [Da
|
|
278
278
|
Contributing
|
279
279
|
------------
|
280
280
|
|
281
|
-
|
282
|
-
* Make your feature addition or bug fix with tests.
|
283
|
-
* Don't forget to update `CHANGELOG.md`.
|
284
|
-
* Send a pull request. Bonus points for topic branches.
|
281
|
+
See [CONTRIBUTING](CONTRIBUTING.md).
|
285
282
|
|
286
283
|
Copyright and License
|
287
284
|
---------------------
|
288
285
|
|
289
286
|
MIT License, see [LICENSE](https://github.com/dblock/mongoid-cached-json/blob/master/LICENSE.md) for details.
|
290
287
|
|
291
|
-
(c) 2012 [
|
288
|
+
(c) 2012-2014 [Artsy](https://artsy.net) and [Contributors](CHANGELOG.md)
|