mongodb-mongo_record 0.3 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -18,6 +18,8 @@ A quick code sample:
18
18
  class Track < MongoRecord::Base
19
19
  collection_name :tracks
20
20
  fields :artist, :album, :song, :track
21
+ index :artist
22
+
21
23
  def to_s
22
24
  "artist: #{artist}, album: #{album}, song: #@song, track: #{@track ? @track.to_i : nil}"
23
25
  end
@@ -81,6 +83,5 @@ Jim Mulholland, jim at squeejee dot com
81
83
  * Assigning created_at and updated_at fields even if they are not declared
82
84
  * Alias methods for "first", "last" and "all"
83
85
 
84
- == To Do
85
-
86
- * DBRefs
86
+ Clinton R. Nixon, crnixon at gmail dot com
87
+ * Ability to define and query indexes from models
@@ -147,6 +147,41 @@ module MongoRecord
147
147
  # Return the field names.
148
148
  def field_names; @field_names; end
149
149
 
150
+ # Creates an index for this collection.
151
+ # +fields+ should be either a single field name (:title)
152
+ # or an array of fields ([:title, :author, :date])
153
+ # or an array of a field name and direction ([:title, :asc] or [:title, :desc])
154
+ # or an array of field names and directions ([[:title, :asc], [:author, :desc]])
155
+ # +unique+ should be true or false and indicates whether this index
156
+ # should enforce a uniqueness constraint.
157
+ def index(fields, unique = false)
158
+ fields = Array(fields)
159
+
160
+ if fields.length == 2 &&
161
+ ( fields[1].to_s == 'asc' || fields[1].to_s == 'desc' ||
162
+ fields[1] == XGen::Mongo::ASCENDING || fields[1] == XGen::Mongo::DESCENDING )
163
+ fields = [fields]
164
+ end
165
+
166
+ fields = fields.map do |field|
167
+ field = field.respond_to?(:[]) ? field : [field, :asc]
168
+ field[1] = (field[1] == :desc) ? XGen::Mongo::DESCENDING : XGen::Mongo::ASCENDING
169
+ field
170
+ end
171
+
172
+ collection.create_index(fields, unique)
173
+ end
174
+
175
+ # Returns list of indexes for model, unless fields are passed.
176
+ # In that case, creates an index.
177
+ def indexes(*fields)
178
+ if fields.empty?
179
+ collection.index_information
180
+ else
181
+ index(*fields)
182
+ end
183
+ end
184
+
150
185
  # Return the names of all instance variables that hold objects
151
186
  # declared using has_one. The names do not start with '@'.
152
187
  #
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'mongo_record'
3
- s.version = '0.3'
3
+ s.version = '0.3.1'
4
4
  s.platform = Gem::Platform::RUBY
5
5
  s.summary = 'ActiveRecord-like models for the 10gen Mongo DB'
6
6
  s.description = 'MongoRecord is an ActiveRecord-like framework for the 10gen Mongo database. For more information about Mongo, see http://www.mongodb.org.'
data/tests/test_mongo.rb CHANGED
@@ -24,6 +24,7 @@ require File.join(File.dirname(__FILE__), 'class_in_module')
24
24
  class Track < MongoRecord::Base
25
25
  collection_name :tracks
26
26
  fields :artist, :album, :song, :track, :created_at
27
+
27
28
  def to_s
28
29
  # Uses both accessor methods and ivars themselves
29
30
  "artist: #{artist}, album: #{album}, song: #@song, track: #{@track ? @track.to_i : nil}"
@@ -743,4 +744,20 @@ class MongoTest < Test::Unit::TestCase
743
744
  assert_equal p.artist, "Nickleback"
744
745
  end
745
746
 
747
+ def test_indexing
748
+ Track.index :artist
749
+ Track.index [:artist, :created_at]
750
+ Track.index [:song, :desc], true
751
+ Track.index [:artist, [:album, :desc]]
752
+ Track.index [:created_at, XGen::Mongo::ASCENDING]
753
+
754
+ assert_equal([ {"artist" => 1},
755
+ {"artist" => 1, "created_at" => 1},
756
+ {"song" => -1},
757
+ {"artist" => 1, "album" => -1},
758
+ {"created_at" => 1} ],
759
+ Track.indexes.map {|index| index[:keys]}.delete_if { |keys|
760
+ keys.has_key? "_id" })
761
+ end
762
+
746
763
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongodb-mongo_record
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.3"
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Menard
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-04-08 00:00:00 -07:00
13
+ date: 2009-05-16 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency