mongodb-mongo_record 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 +7 -2
- data/examples/tracks.rb +3 -1
- data/lib/mongo_record/base.rb +14 -3
- data/mongo-activerecord-ruby.gemspec +3 -3
- data/tests/test_log_device.rb +15 -12
- data/tests/test_mongo.rb +30 -23
- metadata +5 -5
data/README.rdoc
CHANGED
@@ -3,6 +3,10 @@
|
|
3
3
|
MongoRecord is an ActiveRecord-like framework for the 10gen
|
4
4
|
Mongo[http://www.mongodb.org/] database.
|
5
5
|
|
6
|
+
This library is for use outside of Ruby on Rails. If you want to use Mongo
|
7
|
+
with Ruby on Rails, please see the 'activerecord-mongo-adapter' project at
|
8
|
+
http://github.com/mongodb/activerecord-mongo-adapter.
|
9
|
+
|
6
10
|
This document assumes you have read the Mongo documentation.
|
7
11
|
|
8
12
|
A quick code sample:
|
@@ -34,8 +38,9 @@ A quick code sample:
|
|
34
38
|
$ gem sources -a http://gems.github.com
|
35
39
|
$ sudo gem install mongodb-mongo-activerecord-ruby
|
36
40
|
|
37
|
-
MongoRecord depends on the Mongo Ruby Driver
|
38
|
-
will also install the Mongo Ruby Driver if you
|
41
|
+
MongoRecord depends on the Mongo Ruby Driver, version 0.5.4 or higher.
|
42
|
+
Installing the MongoRecord gem will also install the Mongo Ruby Driver if you
|
43
|
+
don't have it already.
|
39
44
|
|
40
45
|
The source code is available at http://github.com/mongodb/mongo-ruby-driver.
|
41
46
|
You can either clone the git repository or download a tarball or zip file.
|
data/examples/tracks.rb
CHANGED
@@ -14,7 +14,9 @@ class Track < MongoRecord::Base
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
|
17
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
18
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
|
19
|
+
MongoRecord::Base.connection = XGen::Mongo::Driver::Mongo.new(host,port).db('mongorecord-test')
|
18
20
|
|
19
21
|
# Create data
|
20
22
|
|
data/lib/mongo_record/base.rb
CHANGED
@@ -299,7 +299,18 @@ module MongoRecord
|
|
299
299
|
# Returns the number of matching records.
|
300
300
|
def count(options={})
|
301
301
|
criteria = criteria_from(options[:conditions]).merge!(where_func(options[:where]))
|
302
|
-
|
302
|
+
begin
|
303
|
+
collection.count(criteria)
|
304
|
+
rescue => ex
|
305
|
+
if ex.to_s =~ /Error with count command.*ns does not exist/
|
306
|
+
# Return 0 because we will graciously assume that we are being
|
307
|
+
# called from a subclass that has been initialized properly, and
|
308
|
+
# is therefore mentioned in the schema.
|
309
|
+
0
|
310
|
+
else
|
311
|
+
raise ex
|
312
|
+
end
|
313
|
+
end
|
303
314
|
end
|
304
315
|
|
305
316
|
# Deletes the record with the given id from the collection.
|
@@ -398,7 +409,7 @@ module MongoRecord
|
|
398
409
|
def find_initial(options)
|
399
410
|
criteria = criteria_from(options[:conditions]).merge!(where_func(options[:where]))
|
400
411
|
fields = fields_from(options[:select])
|
401
|
-
row = collection.
|
412
|
+
row = collection.find_first(criteria, :fields => fields)
|
402
413
|
(row.nil? || row['_id'] == nil) ? nil : self.new(row)
|
403
414
|
end
|
404
415
|
|
@@ -427,7 +438,7 @@ module MongoRecord
|
|
427
438
|
fields = fields_from(options[:select])
|
428
439
|
|
429
440
|
if ids.length == 1
|
430
|
-
row = collection.
|
441
|
+
row = collection.find_first(criteria, :fields => fields)
|
431
442
|
raise RecordNotFound, "Couldn't find #{name} with ID=#{ids[0]} #{criteria.inspect}" if row == nil || row.empty?
|
432
443
|
self.new(row)
|
433
444
|
else
|
@@ -1,11 +1,11 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'mongo_record'
|
3
|
-
s.version = '0.1.
|
3
|
+
s.version = '0.1.1'
|
4
4
|
s.platform = Gem::Platform::RUBY
|
5
5
|
s.summary = 'ActiveRecord-like models for the 10gen Mongo DB'
|
6
|
-
s.description = 'MongoRecord is an ActiveRecord-like framework for the 10gen
|
6
|
+
s.description = 'MongoRecord is an ActiveRecord-like framework for the 10gen Mongo database. For more information about Mongo, see http://www.mongodb.org.'
|
7
7
|
|
8
|
-
s.add_dependency('mongodb-mongo
|
8
|
+
s.add_dependency('mongodb-mongo', ['>= 0.5.4'])
|
9
9
|
|
10
10
|
s.require_paths = ['lib']
|
11
11
|
|
data/tests/test_log_device.rb
CHANGED
@@ -23,23 +23,26 @@ class LoggerTest < Test::Unit::TestCase
|
|
23
23
|
|
24
24
|
MAX_RECS = 3
|
25
25
|
|
26
|
+
@@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
27
|
+
@@port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
|
28
|
+
@@db = XGen::Mongo::Driver::Mongo.new(@@host, @@port).db('mongorecord-test')
|
29
|
+
|
26
30
|
def setup
|
27
|
-
|
28
|
-
|
29
|
-
MongoRecord::LogDevice.connection = @db
|
31
|
+
@@db.drop_collection('testlogger') # can't remove recs from capped colls
|
32
|
+
MongoRecord::LogDevice.connection = @@db
|
30
33
|
# Create a log device with a max of MAX_RECS records
|
31
34
|
@logger = Logger.new(MongoRecord::LogDevice.new('testlogger', :size => 1_000_000, :max => MAX_RECS))
|
32
35
|
end
|
33
36
|
|
34
37
|
def teardown
|
35
|
-
|
38
|
+
@@db.drop_collection('testlogger') # can't remove recs from capped colls
|
36
39
|
end
|
37
40
|
|
38
41
|
# We really don't have to test much more than this. We can trust that Mongo
|
39
42
|
# works properly.
|
40
43
|
def test_max
|
41
|
-
assert_not_nil
|
42
|
-
assert_equal
|
44
|
+
assert_not_nil @@db
|
45
|
+
assert_equal @@db.name, MongoRecord::LogDevice.connection.name
|
43
46
|
collection = MongoRecord::LogDevice.connection.collection('testlogger')
|
44
47
|
MAX_RECS.times { |i|
|
45
48
|
@logger.debug("test message #{i+1}")
|
@@ -53,10 +56,10 @@ class LoggerTest < Test::Unit::TestCase
|
|
53
56
|
end
|
54
57
|
|
55
58
|
def test_alternate_connection
|
56
|
-
old_db =
|
57
|
-
alt_db = XGen::Mongo::Driver::Mongo.new.db('mongorecord-test-log-device')
|
59
|
+
old_db = @@db
|
60
|
+
alt_db = XGen::Mongo::Driver::Mongo.new(@@host, @@port).db('mongorecord-test-log-device')
|
58
61
|
begin
|
59
|
-
|
62
|
+
@@db = nil
|
60
63
|
MongoRecord::LogDevice.connection = alt_db
|
61
64
|
|
62
65
|
logger = Logger.new(MongoRecord::LogDevice.new('testlogger', :size => 1_000_000, :max => MAX_RECS))
|
@@ -64,14 +67,14 @@ class LoggerTest < Test::Unit::TestCase
|
|
64
67
|
|
65
68
|
coll = alt_db.collection('testlogger')
|
66
69
|
assert_equal 1, coll.count()
|
67
|
-
rec = coll.
|
70
|
+
rec = coll.find_first
|
68
71
|
assert_not_nil rec
|
69
72
|
assert_match /test message/, rec['msg']
|
70
73
|
rescue => ex
|
71
74
|
fail ex.to_s
|
72
75
|
ensure
|
73
|
-
|
74
|
-
MongoRecord::LogDevice.connection =
|
76
|
+
@@db = old_db
|
77
|
+
MongoRecord::LogDevice.connection = @@db
|
75
78
|
alt_db.drop_collection('testlogger')
|
76
79
|
end
|
77
80
|
end
|
data/tests/test_mongo.rb
CHANGED
@@ -40,27 +40,29 @@ end
|
|
40
40
|
|
41
41
|
class MongoTest < Test::Unit::TestCase
|
42
42
|
|
43
|
+
@@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
44
|
+
@@port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
|
45
|
+
@@db = XGen::Mongo::Driver::Mongo.new(@@host, @@port).db('mongorecord-test')
|
46
|
+
@@students = @@db.collection('students')
|
47
|
+
@@courses = @@db.collection('courses')
|
48
|
+
@@tracks = @@db.collection('tracks')
|
49
|
+
|
43
50
|
def setup
|
44
51
|
super
|
45
|
-
|
46
|
-
MongoRecord::Base.connection = @db
|
47
|
-
|
48
|
-
@students = @db.collection('students')
|
49
|
-
@courses = @db.collection('courses')
|
50
|
-
@tracks = @db.collection('tracks')
|
52
|
+
MongoRecord::Base.connection = @@db
|
51
53
|
|
52
|
-
|
53
|
-
|
54
|
-
|
54
|
+
@@students.clear
|
55
|
+
@@courses.clear
|
56
|
+
@@tracks.clear
|
55
57
|
|
56
58
|
# Manually insert data without using MongoRecord::Base
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
59
|
+
@@tracks.insert({:_id => XGen::Mongo::Driver::ObjectID.new, :artist => 'Thomas Dolby', :album => 'Aliens Ate My Buick', :song => 'The Ability to Swing'})
|
60
|
+
@@tracks.insert({:_id => XGen::Mongo::Driver::ObjectID.new, :artist => 'Thomas Dolby', :album => 'Aliens Ate My Buick', :song => 'Budapest by Blimp'})
|
61
|
+
@@tracks.insert({:_id => XGen::Mongo::Driver::ObjectID.new, :artist => 'Thomas Dolby', :album => 'The Golden Age of Wireless', :song => 'Europa and the Pirate Twins'})
|
62
|
+
@@tracks.insert({:_id => XGen::Mongo::Driver::ObjectID.new, :artist => 'XTC', :album => 'Oranges & Lemons', :song => 'Garden Of Earthly Delights', :track => 1})
|
61
63
|
@mayor_id = XGen::Mongo::Driver::ObjectID.new
|
62
|
-
|
63
|
-
|
64
|
+
@@tracks.insert({:_id => @mayor_id, :artist => 'XTC', :album => 'Oranges & Lemons', :song => 'The Mayor Of Simpleton', :track => 2})
|
65
|
+
@@tracks.insert({:_id => XGen::Mongo::Driver::ObjectID.new, :artist => 'XTC', :album => 'Oranges & Lemons', :song => 'King For A Day', :track => 3})
|
64
66
|
|
65
67
|
@mayor_str = "artist: XTC, album: Oranges & Lemons, song: The Mayor Of Simpleton, track: 2"
|
66
68
|
@mayor_song = 'The Mayor Of Simpleton'
|
@@ -74,9 +76,9 @@ class MongoTest < Test::Unit::TestCase
|
|
74
76
|
end
|
75
77
|
|
76
78
|
def teardown
|
77
|
-
|
78
|
-
|
79
|
-
|
79
|
+
@@students.clear
|
80
|
+
@@courses.clear
|
81
|
+
@@tracks.clear
|
80
82
|
super
|
81
83
|
end
|
82
84
|
|
@@ -299,6 +301,11 @@ class MongoTest < Test::Unit::TestCase
|
|
299
301
|
assert_equal 3, Track.count(:conditions => {:artist => 'XTC'})
|
300
302
|
end
|
301
303
|
|
304
|
+
def test_count_collection_missing
|
305
|
+
@@db.drop_collection('tracks')
|
306
|
+
assert_equal 0, Track.count
|
307
|
+
end
|
308
|
+
|
302
309
|
def test_select
|
303
310
|
str = Track.find(:all, :select => :album).inject('') { |str, t| str + t.to_s }
|
304
311
|
assert str.include?("artist: , album: Oranges & Lemons, song: , track:")
|
@@ -568,12 +575,12 @@ class MongoTest < Test::Unit::TestCase
|
|
568
575
|
|
569
576
|
def test_alternate_connection
|
570
577
|
old_db = MongoRecord::Base.connection
|
571
|
-
assert_equal
|
572
|
-
alt_db = XGen::Mongo::Driver::Mongo.new.db('mongorecord-test-alt-conn')
|
578
|
+
assert_equal @@db, old_db
|
579
|
+
alt_db = XGen::Mongo::Driver::Mongo.new(@@host, @@port).db('mongorecord-test-alt-conn')
|
573
580
|
assert_not_equal old_db, alt_db
|
574
581
|
alt_db.drop_collection('students')
|
575
582
|
begin
|
576
|
-
|
583
|
+
@@db = nil
|
577
584
|
MongoRecord::Base.connection = alt_db
|
578
585
|
assert_equal alt_db, MongoRecord::Base.connection
|
579
586
|
|
@@ -587,8 +594,8 @@ class MongoTest < Test::Unit::TestCase
|
|
587
594
|
assert s.save, "save failed"
|
588
595
|
assert_equal 1, coll.count()
|
589
596
|
ensure
|
590
|
-
|
591
|
-
MongoRecord::Base.connection =
|
597
|
+
@@db = old_db
|
598
|
+
MongoRecord::Base.connection = @@db
|
592
599
|
alt_db.drop_collection('students')
|
593
600
|
end
|
594
601
|
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.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jim Menard
|
@@ -9,19 +9,19 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-02-05 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
|
-
name: mongodb-mongo
|
16
|
+
name: mongodb-mongo
|
17
17
|
version_requirement:
|
18
18
|
version_requirements: !ruby/object:Gem::Requirement
|
19
19
|
requirements:
|
20
20
|
- - ">="
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: 0.
|
22
|
+
version: 0.5.4
|
23
23
|
version:
|
24
|
-
description: MongoRecord is an ActiveRecord-like framework for the 10gen
|
24
|
+
description: MongoRecord is an ActiveRecord-like framework for the 10gen Mongo database. For more information about Mongo, see http://www.mongodb.org.
|
25
25
|
email: jim@10gen.com
|
26
26
|
executables: []
|
27
27
|
|