mongo_odm 0.2.15 → 0.2.16

Sign up to get free protection for your applications and to get access to all the features.
@@ -161,9 +161,11 @@ To embed just one copy of another class, just define the field type of that clas
161
161
  field :name
162
162
  field :rgb, RGB
163
163
 
164
- create_index :name, :unique => true
164
+ index :name, :unique => true
165
165
  end
166
166
 
167
+ Color.create_indexes
168
+
167
169
  color = Color.new(:name => "red", :rgb => RGB.new(255,0,0))
168
170
  color.save
169
171
 
@@ -10,6 +10,7 @@ module MongoODM
10
10
  autoload :AttributeMethods
11
11
  autoload :Callbacks
12
12
  autoload :Fields
13
+ autoload :Indexes
13
14
  autoload :Inspect
14
15
  autoload :Persistence
15
16
  autoload :Timestamps
@@ -27,6 +28,7 @@ module MongoODM
27
28
  include Document::Persistence
28
29
  include Document::AttributeMethods
29
30
  include Document::Fields
31
+ include Document::Indexes
30
32
  include Document::Inspect
31
33
  include Document::Callbacks
32
34
  include Document::Validations
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+
3
+ module MongoODM
4
+ module Document
5
+ module Indexes
6
+
7
+ extend ActiveSupport::Concern
8
+
9
+ class Index
10
+ attr_reader :spec
11
+ attr_reader :opts
12
+
13
+ # Creates a new document index.
14
+ # @param [String, Array] spec should be either a single field name or an array of field name, direction pairs. Directions should be specified as Mongo::ASCENDING, Mongo::DESCENDING, or Mongo::GEO2D.
15
+ #
16
+ # Note that geospatial indexing only works with versions of MongoDB >= 1.3.3+. Keep in mind, too, that in order to geo-index a given field, that field must reference either an array or a sub-object where the first two values represent x- and y-coordinates. Examples can be seen below.
17
+ #
18
+ # Also note that it is permissible to create compound indexes that include a geospatial index as long as the geospatial index comes first.
19
+ #
20
+ # If your code calls create_index frequently, you can use Collection#ensure_index to cache these calls and thereby prevent excessive round trips to the database.
21
+ # @option opts [Hash] :default ({}) a customizable set of options
22
+ def initialize(spec, opts = {})
23
+ @spec = spec
24
+ @opts = opts
25
+ end
26
+ end
27
+
28
+ module ClassMethods
29
+ def inherited(subclass)
30
+ super
31
+ indexes.concat(subclass.indexes)
32
+ end
33
+
34
+ def indexes
35
+ @indexes ||= []
36
+ end
37
+
38
+ def index(spec, opts = {})
39
+ new_index = Index.new(spec, opts)
40
+ indexes << new_index
41
+ new_index
42
+ end
43
+
44
+ def create_indexes
45
+ indexes.each {|index| create_index index.spec, index.opts }
46
+ end
47
+ end
48
+
49
+ end
50
+ end
51
+ end
@@ -2,6 +2,6 @@
2
2
  module MongoODM
3
3
  VERSION_MAJOR = "0"
4
4
  VERSION_MINOR = "2"
5
- VERSION_BUILD = "15"
5
+ VERSION_BUILD = "16"
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.15
5
+ version: 0.2.16
6
6
  platform: ruby
7
7
  authors:
8
8
  - Carlos Paramio
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-17 00:00:00 +01:00
13
+ date: 2011-03-20 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -267,6 +267,7 @@ files:
267
267
  - lib/mongo_odm/document/callbacks.rb
268
268
  - lib/mongo_odm/document/equality.rb
269
269
  - lib/mongo_odm/document/fields.rb
270
+ - lib/mongo_odm/document/indexes.rb
270
271
  - lib/mongo_odm/document/inspect.rb
271
272
  - lib/mongo_odm/document/persistence.rb
272
273
  - lib/mongo_odm/document/timestamps.rb