mongoid-cached-json 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,11 +1,11 @@
1
1
  Mongoid::CachedJson [![Build Status](https://secure.travis-ci.org/dblock/mongoid-cached-json.png)](http://travis-ci.org/dblock/mongoid-cached-json)
2
2
  ===================
3
3
 
4
- 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*.
4
+ 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
5
 
6
- CachedJson enables returning multiple JSON formats and versions from a single class and provides some rules for returning 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.
6
+ `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
7
 
8
- Using Mongoid::CachedJson we were able to cut our JSON API average response time by about a factor of 10.
8
+ Using `Mongoid::CachedJson` we were able to cut our JSON API average response time by about a factor of 10.
9
9
 
10
10
  Resources
11
11
  ---------
@@ -54,17 +54,19 @@ end
54
54
  Invoke `as_json`.
55
55
 
56
56
  ``` ruby
57
+ widget = Widget.first
58
+
57
59
  # the `:short` version of the JSON, `gadgets` not included
58
- Widget.first.as_json
60
+ widget.as_json
59
61
 
60
62
  # equivalent to the above
61
- Widget.first.as_json({ :properties => :short })
63
+ widget.as_json({ :properties => :short })
62
64
 
63
65
  # `:public` version of the JSON, `gadgets` returned with `:short` JSON, no `:extras`
64
- Widget.first.as_json({ :properties => :public })
66
+ widget.as_json({ :properties => :public })
65
67
 
66
68
  # `:all` version of the JSON, `gadgets` returned with `:all` JSON, including `:extras`
67
- Widget.first.as_json({ :properties => :all })
69
+ widget.as_json({ :properties => :all })
68
70
  ```
69
71
 
70
72
  Configuration
@@ -89,11 +91,11 @@ end
89
91
  Defining Fields
90
92
  ---------------
91
93
 
92
- Mongoid::CachedJson supports the following options.
94
+ `Mongoid::CachedJson` supports the following options:
93
95
 
94
96
  * `: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.is_secret? }`
95
97
 
96
- Mongoid::CachedJson field definitions support the following options.
98
+ `Mongoid::CachedJson` field definitions support the following options:
97
99
 
98
100
  * `:definition` can be a symbol or an anonymous function, eg. `:description => { :definition => :name }` or `:description => { :definition => lambda { |instance| instance.name } }`
99
101
  * `:type` can be `:reference`, required for referenced objects
@@ -156,14 +158,15 @@ end
156
158
 
157
159
  ``` ruby
158
160
  Mongoid::CachedJson.config.transform do |field, definition, value|
159
- (!! definition[:trusted]) ? value : CGI.escapeHTML(value)
161
+ trusted = !! definition[:trusted]
162
+ trusted ? value : CGI.escapeHTML(value)
160
163
  end
161
164
  ```
162
165
 
163
166
  Mixing with Standard as_json
164
167
  ----------------------------
165
168
 
166
- Taking part in the Mongoid::CachedJson `json_fields` scheme is optional: you can still write *as_json* methods where it makes sense.
169
+ Taking part in the `Mongoid::CachedJson` `json_fields` scheme is optional: you can still write `as_json` methods where it makes sense.
167
170
 
168
171
  Turning Caching Off
169
172
  -------------------
@@ -181,7 +184,7 @@ describe "as_json" do
181
184
  @person = Person.create!({ :first => "John", :last => "Doe" })
182
185
  end
183
186
  it "returns name" do
184
- @person.as_json({ :properties => :public })[:name].should== "John Doe"
187
+ @person.as_json({ :properties => :public })[:name].should == "John Doe"
185
188
  end
186
189
  end
187
190
  ```
@@ -239,3 +242,4 @@ Copyright and License
239
242
  MIT License, see [LICENSE](https://github.com/dblock/mongoid-cached-json/blob/master/LICENSE.md) for details.
240
243
 
241
244
  (c) 2012 [Art.sy Inc.](http://artsy.github.com) and [Contributors](https://github.com/dblock/mongoid-cached-json/blob/master/HISTORY.md)
245
+
@@ -104,20 +104,22 @@ module Mongoid
104
104
  def resolve_json_reference(options, object, field, reference_def)
105
105
  reference_json = nil
106
106
  if reference_def[:metadata]
107
- clazz = reference_def[:metadata].class_name.constantize
108
107
  key = reference_def[:metadata].key.to_sym
109
108
  if reference_def[:metadata].relation == Mongoid::Relations::Referenced::ManyToMany
109
+ clazz = reference_def[:metadata].class_name.constantize
110
110
  reference_json = object.send(key).map do |id|
111
111
  materialize_json(options, { :clazz => clazz, :id => id })
112
112
  end.compact
113
113
  elsif reference_def[:metadata].relation == Mongoid::Relations::Referenced::In
114
+ clazz = reference_def[:metadata].inverse_class_name.constantize
114
115
  reference_json = materialize_json(options, { :clazz => clazz, :id => object.send(key) })
115
116
  end
116
117
  end
117
118
  # If we get to this point and reference_json is still nil, there's no chance we can
118
119
  # load the JSON from cache so we go ahead and call as_json on the object.
119
- if !reference_json
120
- reference = reference_def[:definition].is_a?(Symbol) ? object.send(reference_def[:definition]) : reference_def[:definition].call(object)
120
+ if ! reference_json
121
+ reference_def_definition = reference_def[:definition]
122
+ reference = reference_def_definition.is_a?(Symbol) ? object.send(reference_def_definition) : reference_def_definition.call(object)
121
123
  reference_json = reference.as_json(options) if reference
122
124
  end
123
125
  reference_json
@@ -65,7 +65,7 @@ module Mongoid
65
65
  settings[:default_version] = default_version
66
66
  end
67
67
 
68
- # Returns the default cache store, which is either a Rails logger of stdout logger
68
+ # Returns the default cache store, for example Rails cache or an instance of ActiveSupport::Cache::MemoryStore.
69
69
  #
70
70
  # @example Get the default cache store
71
71
  # config.default_cache
@@ -75,7 +75,7 @@ module Mongoid
75
75
  defined?(Rails) && Rails.respond_to?(:cache) ? Rails.cache : ::ActiveSupport::Cache::MemoryStore.new
76
76
  end
77
77
 
78
- # Returns the cache, or defaults to Rails cache or ActiveSupport::Cache::MemoryStore logger.
78
+ # Returns the cache, or defaults to Rails cache when running under Rails or ActiveSupport::Cache::MemoryStore.
79
79
  #
80
80
  # @example Get the cache.
81
81
  # config.cache
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid
3
3
  module CachedJson
4
- VERSION = '1.2.1'
4
+ VERSION = '1.2.2'
5
5
  end
6
6
  end
7
7
 
metadata CHANGED
@@ -1,114 +1,150 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: mongoid-cached-json
3
- version: !ruby/object:Gem::Version
4
- version: 1.2.1
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
5
  prerelease:
6
+ segments:
7
+ - 1
8
+ - 2
9
+ - 2
10
+ version: 1.2.2
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Aaron Windsor
9
14
  - Daniel Doubrovkine
10
15
  - Frank Macreery
11
16
  autorequire:
12
17
  bindir: bin
13
18
  cert_chain: []
14
- date: 2012-06-12 00:00:00.000000000 Z
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
17
- name: activesupport
18
- requirement: &70288576783800 !ruby/object:Gem::Requirement
19
+
20
+ date: 2012-07-03 00:00:00 Z
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ type: :runtime
24
+ requirement: &id001 !ruby/object:Gem::Requirement
19
25
  none: false
20
- requirements:
21
- - - ! '>='
22
- - !ruby/object:Gem::Version
23
- version: '0'
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ prerelease: false
34
+ name: activesupport
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
24
37
  type: :runtime
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
25
47
  prerelease: false
26
- version_requirements: *70288576783800
27
- - !ruby/object:Gem::Dependency
28
48
  name: mongoid
29
- requirement: &70288576799380 !ruby/object:Gem::Requirement
30
- none: false
31
- requirements:
32
- - - ! '>='
33
- - !ruby/object:Gem::Version
34
- version: '0'
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
35
51
  type: :runtime
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
36
61
  prerelease: false
37
- version_requirements: *70288576799380
38
- - !ruby/object:Gem::Dependency
39
62
  name: bson_ext
40
- requirement: &70288576798740 !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
63
+ version_requirements: *id003
64
+ - !ruby/object:Gem::Dependency
46
65
  type: :runtime
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
47
75
  prerelease: false
48
- version_requirements: *70288576798740
49
- - !ruby/object:Gem::Dependency
50
76
  name: hpricot
51
- requirement: &70288576798260 !ruby/object:Gem::Requirement
77
+ version_requirements: *id004
78
+ - !ruby/object:Gem::Dependency
79
+ type: :development
80
+ requirement: &id005 !ruby/object:Gem::Requirement
52
81
  none: false
53
- requirements:
54
- - - ! '>='
55
- - !ruby/object:Gem::Version
56
- version: '0'
57
- type: :runtime
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ hash: 9
86
+ segments:
87
+ - 2
88
+ - 5
89
+ version: "2.5"
58
90
  prerelease: false
59
- version_requirements: *70288576798260
60
- - !ruby/object:Gem::Dependency
61
91
  name: rspec
62
- requirement: &70288576797660 !ruby/object:Gem::Requirement
92
+ version_requirements: *id005
93
+ - !ruby/object:Gem::Dependency
94
+ type: :development
95
+ requirement: &id006 !ruby/object:Gem::Requirement
63
96
  none: false
64
- requirements:
97
+ requirements:
65
98
  - - ~>
66
- - !ruby/object:Gem::Version
67
- version: '2.5'
68
- type: :development
99
+ - !ruby/object:Gem::Version
100
+ hash: 15
101
+ segments:
102
+ - 1
103
+ - 0
104
+ version: "1.0"
69
105
  prerelease: false
70
- version_requirements: *70288576797660
71
- - !ruby/object:Gem::Dependency
72
106
  name: bundler
73
- requirement: &70288576797160 !ruby/object:Gem::Requirement
107
+ version_requirements: *id006
108
+ - !ruby/object:Gem::Dependency
109
+ type: :development
110
+ requirement: &id007 !ruby/object:Gem::Requirement
74
111
  none: false
75
- requirements:
112
+ requirements:
76
113
  - - ~>
77
- - !ruby/object:Gem::Version
78
- version: '1.0'
79
- type: :development
114
+ - !ruby/object:Gem::Version
115
+ hash: 3
116
+ segments:
117
+ - 1
118
+ - 6
119
+ version: "1.6"
80
120
  prerelease: false
81
- version_requirements: *70288576797160
82
- - !ruby/object:Gem::Dependency
83
121
  name: jeweler
84
- requirement: &70288576796680 !ruby/object:Gem::Requirement
85
- none: false
86
- requirements:
87
- - - ~>
88
- - !ruby/object:Gem::Version
89
- version: '1.6'
122
+ version_requirements: *id007
123
+ - !ruby/object:Gem::Dependency
90
124
  type: :development
91
- prerelease: false
92
- version_requirements: *70288576796680
93
- - !ruby/object:Gem::Dependency
94
- name: yard
95
- requirement: &70288576796140 !ruby/object:Gem::Requirement
125
+ requirement: &id008 !ruby/object:Gem::Requirement
96
126
  none: false
97
- requirements:
127
+ requirements:
98
128
  - - ~>
99
- - !ruby/object:Gem::Version
100
- version: '0.6'
101
- type: :development
129
+ - !ruby/object:Gem::Version
130
+ hash: 7
131
+ segments:
132
+ - 0
133
+ - 6
134
+ version: "0.6"
102
135
  prerelease: false
103
- version_requirements: *70288576796140
136
+ name: yard
137
+ version_requirements: *id008
104
138
  description: Cached-json is a DSL for describing JSON representations of Mongoid models.
105
139
  email: dblock@dblock.org
106
140
  executables: []
141
+
107
142
  extensions: []
108
- extra_rdoc_files:
143
+
144
+ extra_rdoc_files:
109
145
  - LICENSE.md
110
146
  - README.md
111
- files:
147
+ files:
112
148
  - lib/mongoid-cached-json.rb
113
149
  - lib/mongoid-cached-json/cached_json.rb
114
150
  - lib/mongoid-cached-json/config.rb
@@ -116,31 +152,37 @@ files:
116
152
  - LICENSE.md
117
153
  - README.md
118
154
  homepage: http://github.com/dblock/mongoid-cached-json
119
- licenses:
155
+ licenses:
120
156
  - MIT
121
157
  post_install_message:
122
158
  rdoc_options: []
123
- require_paths:
159
+
160
+ require_paths:
124
161
  - lib
125
- required_ruby_version: !ruby/object:Gem::Requirement
162
+ required_ruby_version: !ruby/object:Gem::Requirement
126
163
  none: false
127
- requirements:
128
- - - ! '>='
129
- - !ruby/object:Gem::Version
130
- version: '0'
131
- segments:
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ hash: 3
168
+ segments:
132
169
  - 0
133
- hash: 3411124763805078163
134
- required_rubygems_version: !ruby/object:Gem::Requirement
170
+ version: "0"
171
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
172
  none: false
136
- requirements:
137
- - - ! '>='
138
- - !ruby/object:Gem::Version
139
- version: '0'
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ hash: 3
177
+ segments:
178
+ - 0
179
+ version: "0"
140
180
  requirements: []
181
+
141
182
  rubyforge_project:
142
183
  rubygems_version: 1.8.10
143
184
  signing_key:
144
185
  specification_version: 3
145
186
  summary: Effective model-level JSON caching for the Mongoid ODM.
146
187
  test_files: []
188
+