mongo_odm 0.2.17 → 0.2.18

Sign up to get free protection for your applications and to get access to all the features.
@@ -76,6 +76,8 @@ And because any query method returns a MongoODM::Criteria object, you can concat
76
76
  You can also define your own class methods that returns criteria objects, and concatenate them to obtain a single criteria with all the conditions merged in the calls order:
77
77
 
78
78
  class Shape
79
+ include MongoODM::Document
80
+
79
81
  def self.with_radius(n)
80
82
  find(:radius => n)
81
83
  end
@@ -87,6 +89,15 @@ You can also define your own class methods that returns criteria objects, and co
87
89
 
88
90
  Shape.with_radius(1).ordered_by_color # Returns the same criteria than the previous example
89
91
 
92
+ Default values for fields can be either a fixed value, or a block, in which case the block will be called each time an object is instantiated. Example:
93
+
94
+ class Timestamp
95
+ include MongoODM::Document
96
+
97
+ field :value, Time, :default => lambda { Time.now }
98
+ field :set, Set, :default => lambda { Set.new }
99
+ end
100
+
90
101
  Take a look at the Mongo Ruby driver documentation for the 'find' method to see the available options:
91
102
 
92
103
  http://api.mongodb.org/ruby/1.2.4/Mongo/Collection.html#find-instance_method
@@ -92,7 +92,7 @@ module MongoODM
92
92
 
93
93
  module ClassMethods
94
94
  def default_attributes
95
- HashWithIndifferentAccess[fields.values.map{|field| [field.name, field.default]}]
95
+ HashWithIndifferentAccess[fields.values.map{|field| [field.name, field.default.respond_to?(:call) ? field.default.call : field.default]}]
96
96
  end
97
97
 
98
98
  def define_attribute_methods_for_fields
@@ -2,6 +2,6 @@
2
2
  module MongoODM
3
3
  VERSION_MAJOR = "0"
4
4
  VERSION_MINOR = "2"
5
- VERSION_BUILD = "17"
5
+ VERSION_BUILD = "18"
6
6
  VERSION = "#{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_BUILD}"
7
7
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mongo_odm
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.17
5
+ version: 0.2.18
6
6
  platform: ruby
7
7
  authors:
8
8
  - Carlos Paramio