mongoid_cached_document 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +52 -2
- data/VERSION +1 -1
- data/mongoid_cached_document.gemspec +1 -1
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -1,6 +1,56 @@
|
|
1
|
-
=
|
1
|
+
= About Mongoid::CachedDocument
|
2
2
|
|
3
|
-
|
3
|
+
Mongoid is an ODM (Object-Document-Mapper) framework for MongoDB in Ruby. Mongoid::CachedDocument adds a new field type of the same name that supports the caching attributes from a related document.
|
4
|
+
|
5
|
+
== Example
|
6
|
+
|
7
|
+
class User
|
8
|
+
include Mongoid::Document
|
9
|
+
|
10
|
+
field :login
|
11
|
+
#...
|
12
|
+
end
|
13
|
+
|
14
|
+
class Post
|
15
|
+
include Mongoid::Document
|
16
|
+
|
17
|
+
field :title
|
18
|
+
field :body
|
19
|
+
field :author, :type => Mongoid::CachedDocument
|
20
|
+
end
|
21
|
+
|
22
|
+
Assuming <tt>@user</tt> is a valid <tt>User</tt> with an <tt>id</tt> of 42 and <tt>login</tt> of '<tt>jsmith</tt>':
|
23
|
+
|
24
|
+
@post = Post.create(:title => 'First Post', :body => 'This is a post.', :author => @user)
|
25
|
+
|
26
|
+
This will create a new <tt>Post</tt> document, with an <tt>author</tt> field, the content of which is a hash:
|
27
|
+
|
28
|
+
{ '_type' => 'User', '_id' = 1 }
|
29
|
+
|
30
|
+
@post.author._type
|
31
|
+
=> 'User'
|
32
|
+
@post.author._id
|
33
|
+
=> 1
|
34
|
+
|
35
|
+
Attempting to get the user's <tt>login</tt> will cause the real user document to be fetched from the collection, which replaces the cached values.
|
36
|
+
|
37
|
+
class User
|
38
|
+
#...
|
39
|
+
|
40
|
+
def cached_attributes
|
41
|
+
[ :login ]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
With the above class definition, the cached attributes will be:
|
46
|
+
|
47
|
+
{ '_type' => 'User', '_id' = 1, 'login' => 'jsmith' }
|
48
|
+
|
49
|
+
To use the above in a criteria:
|
50
|
+
|
51
|
+
Post.criteria.where('author.login' => 'jsmith')
|
52
|
+
|
53
|
+
Enjoy!
|
4
54
|
|
5
55
|
== Note on Patches/Pull Requests
|
6
56
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|