Permalink
Browse files

Push next version

  • Loading branch information...
1 parent f939c21 commit cc3e3f52807f58f085d4c341d0742ca7b11efaed @durran durran committed Jun 17, 2012
@@ -267,16 +267,39 @@
<h3>Custom field serialization</h3>
<p>
You can define custom types in Mongoid and determine how they are
- serialized and deserialized. You simply need to define the class, include
- <code>Mongoid::Fields::Serializable</code>, and override the
- <code>serialize</code> and <code>deserialize</code> methods as needed.
- Deserialization is used to convert from the value that is stored in the
- database to a value that is used when accessed. Serialization is used to
- convert the object to a MongoDB friendly value.
+ serialized and deserialized. You simply need to provide 3 methods on it
+ for Mongoid to call to convert your object to and from MongoDB friendly
+ values.
</p>
<div class="CodeRay">
- <div class="code"><pre><span class="keyword">class</span> <span class="class">Profile</span>&#x000A; include <span class="constant">Mongoid</span>::<span class="constant">Document</span>&#x000A; field <span class="symbol">:location</span>, type: <span class="constant">Point</span>&#x000A;<span class="keyword">end</span>&#x000A;&#x000A;<span class="keyword">class</span> <span class="class">Point</span>&#x000A; include <span class="constant">Mongoid</span>::<span class="constant">Fields</span>::<span class="constant">Serializable</span>&#x000A;&#x000A; <span class="keyword">def</span> <span class="function">deserialize</span>(object)&#x000A; [ object[<span class="string"><span class="delimiter">&quot;</span><span class="content">x</span><span class="delimiter">&quot;</span></span>], object[<span class="string"><span class="delimiter">&quot;</span><span class="content">y</span><span class="delimiter">&quot;</span></span>] ]&#x000A; <span class="keyword">end</span>&#x000A;&#x000A; <span class="keyword">def</span> <span class="function">serialize</span>(object)&#x000A; { <span class="string"><span class="delimiter">&quot;</span><span class="content">x</span><span class="delimiter">&quot;</span></span> =&gt; object[<span class="integer">0</span>], <span class="string"><span class="delimiter">&quot;</span><span class="content">y</span><span class="delimiter">&quot;</span></span> =&gt; object[<span class="integer">1</span>] }&#x000A; <span class="keyword">end</span>&#x000A;<span class="keyword">end</span>&#x000A;</pre></div>
+ <div class="code"><pre><span class="keyword">class</span> <span class="class">Profile</span>&#x000A; include <span class="constant">Mongoid</span>::<span class="constant">Document</span>&#x000A; field <span class="symbol">:location</span>, type: <span class="constant">Point</span>&#x000A;<span class="keyword">end</span>&#x000A;&#x000A;<span class="keyword">class</span> <span class="class">Point</span>&#x000A;&#x000A; attr_reader <span class="symbol">:x</span>, <span class="symbol">:y</span>&#x000A;&#x000A; <span class="keyword">def</span> <span class="function">initialize</span>(x, y)&#x000A; <span class="instance-variable">@x</span>, <span class="instance-variable">@y</span> = x, y&#x000A; <span class="keyword">end</span>&#x000A;&#x000A; <span class="keyword">def</span> <span class="function">mongoize</span>&#x000A; [ x, y ]&#x000A; <span class="keyword">end</span>&#x000A;&#x000A; <span class="keyword">class</span> &lt;&lt; <span class="class">self</span>&#x000A;&#x000A; <span class="keyword">def</span> <span class="function">demongoize</span>(object)&#x000A; <span class="constant">Point</span>.new(object[<span class="integer">0</span>], object[<span class="integer">1</span>])&#x000A; <span class="keyword">end</span>&#x000A;&#x000A; <span class="keyword">def</span> <span class="function">evolve</span>(object)&#x000A; <span class="keyword">if</span> object.is_a?(<span class="constant">Point</span>)&#x000A; [ object.x, object.y ]&#x000A; <span class="keyword">else</span>&#x000A; object&#x000A; <span class="keyword">end</span>&#x000A; <span class="keyword">end</span>&#x000A; <span class="keyword">end</span>&#x000A;<span class="keyword">end</span>&#x000A;</pre></div>
</div>
+ <p>
+ The instance method <code>mongoize</code> take an instance of your object,
+ and converts it into how it will be stored in the database. In our example
+ above, we want to store our point object as an array in the form
+ <code>[ x, y ]</code>.
+ </p>
+ <p>
+ The class method <code>demongoize</code> takes an object as how it was
+ stored in the database, and is responsible for instantiating an object of
+ your custom type. In this case, we take an array and instantiate a
+ <code>Point</code> from it.
+ </p>
+ <p>
+ The class method <code>evolve</code> takes an object, and determines how it
+ is to be transformed for use in criteria. For example we may want to write
+ a query like so:
+ </p>
+ <div class="CodeRay">
+ <div class="code"><pre>point = <span class="constant">Point</span>.new(<span class="integer">12</span>, <span class="integer">24</span>)&#x000A;<span class="constant">Venue</span>.where(location: point)&#x000A;</pre></div>
+ </div>
+ <p>
+ We need the point object in the criteria to be transformed to a Mongo
+ friendly value when it is not as well, and <code>evolve</code> is the method
+ that takes care of this. We check if the passed in object is a <code>Point</code>
+ first, in case we also want to be able to pass in ordinary arrays instead.
+ </p>
<h3>Reserved names</h3>
<p>
If you define a field on your document that conflicts with a reserved method
@@ -96,15 +96,9 @@
to be memory efficient for large queries and data sets. However if you
want the query to load all matching documents in memory and return
them on <i>n</i> iterations without hitting the database again you
- may cache a class or criteria:
+ may cache a criteria:
</p>
<p>
- To have all queries for a model "cache":
- </p>
- <div class="CodeRay">
- <div class="code"><pre><span class="keyword">class</span> <span class="class">Person</span>&#x000A; include <span class="constant">Mongoid</span>::<span class="constant">Document</span>&#x000A; cache&#x000A;<span class="keyword">end</span>&#x000A;</pre></div>
- </div>
- <p>
To cache on a per-query basis:
</p>
<div class="CodeRay">
@@ -145,6 +145,17 @@
<div class="CodeRay">
<div class="code"><pre><span class="error">$</span> gem install mongoid&#x000A;</pre></div>
</div>
+ <div class='well'>
+ <table>
+ <tr>
+ <td class='samurai'><img src="/images/samurai.png" /></td>
+ <td class='note'>
+ The minimum version of MongoDB that is required for you to run Mongoid 3
+ is <code>2.0.0</code>.
+ </td>
+ </tr>
+ </table>
+ </div>
</section>
<section id='configuration'>
<h2>Configuration</h2>
@@ -251,7 +262,7 @@
<h3>Getting Rid of Active Record</h3>
<p>
Now that you have a <code>mongoid.yml</code> you can't wait to delete that
- pesky <code>database.yml</code>, right? Do it and you'll start gecodeing
+ pesky <code>database.yml</code>, right? Do it and you'll start getting
<code>ActiveRecord</code> errors all over the place.
You don't need ActiveRecord unless you're trying to use Mongo in concert
with a SQL database. Here's how you remove ActiveRecord from the most
@@ -293,7 +304,7 @@
<h2>Logging</h2>
<p>
Changing logging options is done simply by telling Mongoid or Moped's
- logger to have a different level.
+ logger to have a different level. Logging is turned off by default.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="keyword">module</span> <span class="class">MyApplication</span>&#x000A; <span class="keyword">class</span> <span class="class">Application</span> &lt;&lt; <span class="constant">Rails</span>::<span class="constant">Application</span>&#x000A; <span class="constant">Mongoid</span>.logger.level = <span class="constant">Logger</span>::<span class="constant">DEBUG</span>&#x000A; <span class="constant">Moped</span>.logger.level = <span class="constant">Logger</span>::<span class="constant">DEBUG</span>&#x000A; <span class="keyword">end</span>&#x000A;<span class="keyword">end</span>&#x000A;</pre></div>
@@ -104,8 +104,8 @@
<p>
Mongoid's standard persistence methods come in the form of common methods
you would find in other mapping frameworks. The following table is a cheat
- sheet with the method in Mongoid on the right, and the Moped driver operation
- on the left.
+ sheet with the method in Mongoid on the left, and the Moped driver operation
+ on the right.
</p>
<div class='well'>
<table>
@@ -824,7 +824,7 @@
functions.
</p>
<div class="CodeRay">
- <div class="code"><pre>map = <span class="string"><span class="delimiter">%Q{</span><span class="content">&#x000A; function() </span><span class="content">{</span><span class="content">&#x000A; emit(this.name, </span><span class="content">{</span><span class="content"> likes: this.likes </span><span class="content">}</span><span class="content">);&#x000A; </span><span class="content">}</span><span class="content">&#x000A;</span><span class="delimiter">}</span></span>&#x000A;&#x000A;reduce = <span class="string"><span class="delimiter">%Q{</span><span class="content">&#x000A; function(key, values) </span><span class="content">{</span><span class="content">&#x000A; var result = </span><span class="content">{</span><span class="content"> likes: 0 </span><span class="content">}</span><span class="content">;&#x000A; values.forEach(function(value) </span><span class="content">{</span><span class="content">&#x000A; result.likes += value.likes;&#x000A; </span><span class="content">}</span><span class="content">);&#x000A; return result;&#x000A; </span><span class="content">}</span><span class="content">&#x000A;</span><span class="delimiter">}</span></span>&#x000A;&#x000A;<span class="constant">Band</span>.where(<span class="symbol">:likeg</span>.gt =&gt; <span class="integer">100</span>).map_reduce(map, reduce).out(inline: <span class="predefined-constant">true</span>)&#x000A;</pre></div>
+ <div class="code"><pre>map = <span class="string"><span class="delimiter">%Q{</span><span class="content">&#x000A; function() </span><span class="content">{</span><span class="content">&#x000A; emit(this.name, </span><span class="content">{</span><span class="content"> likes: this.likes </span><span class="content">}</span><span class="content">);&#x000A; </span><span class="content">}</span><span class="content">&#x000A;</span><span class="delimiter">}</span></span>&#x000A;&#x000A;reduce = <span class="string"><span class="delimiter">%Q{</span><span class="content">&#x000A; function(key, values) </span><span class="content">{</span><span class="content">&#x000A; var result = </span><span class="content">{</span><span class="content"> likes: 0 </span><span class="content">}</span><span class="content">;&#x000A; values.forEach(function(value) </span><span class="content">{</span><span class="content">&#x000A; result.likes += value.likes;&#x000A; </span><span class="content">}</span><span class="content">);&#x000A; return result;&#x000A; </span><span class="content">}</span><span class="content">&#x000A;</span><span class="delimiter">}</span></span>&#x000A;&#x000A;<span class="constant">Band</span>.where(<span class="symbol">:likes</span>.gt =&gt; <span class="integer">100</span>).map_reduce(map, reduce).out(inline: <span class="predefined-constant">true</span>)&#x000A;</pre></div>
</div>
<p>
Just like criteria, map/reduce calls are lazily evaluated. So nothing will
@@ -102,6 +102,11 @@
<ul class='incompatible'>
<li>
<i class='icon-warning-sign'></i>
+ bson and bson_ext gems are no longer compatible. Please remove both from
+ your Gemfile and any other dependencies. They have been replaced by Moped.
+ </li>
+ <li>
+ <i class='icon-warning-sign'></i>
GridFS support no longer exists. If you are using GridFS, you cannot
upgrade.
</li>
@@ -192,7 +197,7 @@
<code>Model.identity</code> and <code>Model.key</code> have been removed.
For custom ids, users must now override the _id field.
<p>
- When the default value is a proc, the default is applied *after%li all
+ When the default value is a proc, the default is applied *after* all
other attributes are set.
</p>
<div class="CodeRay">
@@ -252,8 +257,9 @@
<i class='icon-warning-sign'></i>
<a href="http://github.com/mongoid/mongoid/issues/1342">#1342</a>
<code>Model.find</code> and <code>model.relation.find</code> now only
- take a single or multiple ids. The first/last/all with a conditions hash
- has been removed.
+ take a single or multiple ids. <code>Model.first</code>, <code>Model.last</code>
+ also no longer take arguments. For these use <code>Model.find_by</code>
+ instead.
</li>
<li>
<i class='icon-warning-sign'></i>

0 comments on commit cc3e3f5

Please sign in to comment.