mongodb-mongo_record 0.4.1 → 0.4.2
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 +1 -5
- data/examples/tracks.rb +8 -8
- data/lib/mongo_record/base.rb +15 -15
- data/mongo-activerecord-ruby.gemspec +2 -2
- data/tests/test_log_device.rb +4 -4
- data/tests/test_mongo.rb +10 -10
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -3,10 +3,6 @@
|
|
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
|
-
|
10
6
|
This document assumes you have read the Mongo documentation.
|
11
7
|
|
12
8
|
A quick code sample:
|
@@ -26,7 +22,7 @@ A quick code sample:
|
|
26
22
|
end
|
27
23
|
|
28
24
|
MongoRecord::Base.connection =
|
29
|
-
|
25
|
+
Mongo::Mongo.new.db('mongorecord-test')
|
30
26
|
|
31
27
|
t = Track.new(:artist => 'Level 42', :album => 'Standing In The Light',
|
32
28
|
:song => 'Micro-Kid', :track => 1)
|
data/examples/tracks.rb
CHANGED
@@ -15,8 +15,8 @@ class Track < MongoRecord::Base
|
|
15
15
|
end
|
16
16
|
|
17
17
|
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
18
|
-
port = ENV['MONGO_RUBY_DRIVER_PORT'] ||
|
19
|
-
MongoRecord::Base.connection =
|
18
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::Mongo::DEFAULT_PORT
|
19
|
+
MongoRecord::Base.connection = Mongo::Mongo.new(host,port).db('mongorecord-test')
|
20
20
|
|
21
21
|
# Create data
|
22
22
|
|
@@ -24,12 +24,12 @@ puts "Creating 6 records using \"raw\" Mongo access..."
|
|
24
24
|
db = MongoRecord::Base.connection
|
25
25
|
coll = db.collection('tracks')
|
26
26
|
coll.remove({})
|
27
|
-
coll.insert({:_id =>
|
28
|
-
coll.insert({:_id =>
|
29
|
-
coll.insert({:_id =>
|
30
|
-
coll.insert({:_id =>
|
31
|
-
coll.insert({:_id =>
|
32
|
-
song_id =
|
27
|
+
coll.insert({:_id => Mongo::ObjectID.new, :artist => 'Thomas Dolby', :album => 'Aliens Ate My Buick', :song => 'The Ability to Swing'})
|
28
|
+
coll.insert({:_id => Mongo::ObjectID.new, :artist => 'Thomas Dolby', :album => 'Aliens Ate My Buick', :song => 'Budapest by Blimp'})
|
29
|
+
coll.insert({:_id => Mongo::ObjectID.new, :artist => 'Thomas Dolby', :album => 'The Golden Age of Wireless', :song => 'Europa and the Pirate Twins'})
|
30
|
+
coll.insert({:_id => Mongo::ObjectID.new, :artist => 'XTC', :album => 'Oranges & Lemons', :song => 'Garden Of Earthly Delights', :track => 1})
|
31
|
+
coll.insert({:_id => Mongo::ObjectID.new, :artist => 'XTC', :album => 'Oranges & Lemons', :song => 'The Mayor Of Simpleton', :track => 2})
|
32
|
+
song_id = Mongo::ObjectID.new
|
33
33
|
coll.insert({:_id => song_id, :artist => 'XTC', :album => 'Oranges & Lemons', :song => 'King For A Day', :track => 3})
|
34
34
|
puts "Data created. One song_id = #{song_id}."
|
35
35
|
puts "There are #{coll.count()} records in the tracks collection."
|
data/lib/mongo_record/base.rb
CHANGED
@@ -22,11 +22,11 @@ require 'mongo_record/sql'
|
|
22
22
|
class String
|
23
23
|
# Convert this String to an ObjectID.
|
24
24
|
def to_oid
|
25
|
-
|
25
|
+
Mongo::ObjectID.legal?(self) ? XGen::Mongo::Driver::ObjectID.from_string(self) : self
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
class
|
29
|
+
class Mongo::ObjectID
|
30
30
|
# Convert this object to an ObjectID.
|
31
31
|
def to_oid
|
32
32
|
self
|
@@ -39,7 +39,7 @@ module MongoRecord
|
|
39
39
|
def create_pk(row)
|
40
40
|
return row if row[:_id]
|
41
41
|
row.delete(:_id) # in case it is nil
|
42
|
-
row['_id'] ||=
|
42
|
+
row['_id'] ||= Mongo::ObjectID.new
|
43
43
|
row
|
44
44
|
end
|
45
45
|
end
|
@@ -157,13 +157,13 @@ module MongoRecord
|
|
157
157
|
|
158
158
|
if fields.length == 2 &&
|
159
159
|
( fields[1].to_s == 'asc' || fields[1].to_s == 'desc' ||
|
160
|
-
fields[1] ==
|
160
|
+
fields[1] == Mongo::ASCENDING || fields[1] == Mongo::DESCENDING )
|
161
161
|
fields = [fields]
|
162
162
|
end
|
163
163
|
|
164
164
|
fields = fields.map do |field|
|
165
165
|
field = field.respond_to?(:[]) ? field : [field, :asc]
|
166
|
-
field[1] = (field[1] == :desc) ?
|
166
|
+
field[1] = (field[1] == :desc) ? Mongo::DESCENDING : Mongo::ASCENDING
|
167
167
|
field
|
168
168
|
end
|
169
169
|
|
@@ -381,7 +381,7 @@ module MongoRecord
|
|
381
381
|
def count(options={})
|
382
382
|
criteria = criteria_from(options[:conditions],options[:criteria]).merge!(where_func(options[:where]))
|
383
383
|
begin
|
384
|
-
collection.
|
384
|
+
collection.find(criteria).count()
|
385
385
|
rescue => ex
|
386
386
|
if ex.to_s =~ /Error with count command.*ns missing/
|
387
387
|
# Return 0 because we will graciously assume that we are being
|
@@ -495,7 +495,7 @@ module MongoRecord
|
|
495
495
|
# def find_initial(options)
|
496
496
|
# criteria = criteria_from(options[:conditions]).merge!(where_func(options[:where]))
|
497
497
|
# fields = fields_from(options[:select])
|
498
|
-
# row = collection.
|
498
|
+
# row = collection.find_one(criteria, :fields => fields)
|
499
499
|
# (row.nil? || row['_id'] == nil) ? nil : self.new(row)
|
500
500
|
# end
|
501
501
|
|
@@ -541,7 +541,7 @@ module MongoRecord
|
|
541
541
|
fields = fields_from(options[:select])
|
542
542
|
|
543
543
|
if ids.length == 1
|
544
|
-
row = collection.
|
544
|
+
row = collection.find_one(criteria, :fields => fields)
|
545
545
|
raise RecordNotFound, "Couldn't find #{name} with ID=#{ids[0]} #{criteria.inspect}" if row == nil || row.empty?
|
546
546
|
self.new(row)
|
547
547
|
else
|
@@ -656,7 +656,7 @@ module MongoRecord
|
|
656
656
|
# +func+ must be +nil+ or a JavaScript expression or function in a
|
657
657
|
# string.
|
658
658
|
def where_func(func) # :nodoc:
|
659
|
-
func ? {'$where' =>
|
659
|
+
func ? {'$where' => Mongo::Code.new(func)} : {}
|
660
660
|
end
|
661
661
|
|
662
662
|
def replace_named_bind_variables(str, h) # :nodoc:
|
@@ -815,7 +815,7 @@ module MongoRecord
|
|
815
815
|
|
816
816
|
# Return true if this object is new---that is, does not yet have an id.
|
817
817
|
def new_record?
|
818
|
-
@_id.nil? || self.class.collection.
|
818
|
+
@_id.nil? || self.class.collection.find_one("_id" => @_id).nil?
|
819
819
|
end
|
820
820
|
|
821
821
|
# Convert this object to a Mongo value suitable for saving to the
|
@@ -826,11 +826,11 @@ module MongoRecord
|
|
826
826
|
key_names.each {|key|
|
827
827
|
value = instance_variable_get("@#{key}").to_mongo_value
|
828
828
|
if value.instance_of? Hash and value["_ns"]
|
829
|
-
value =
|
829
|
+
value = Mongo::DBRef.new(value["_ns"], value["_id"])
|
830
830
|
elsif value.instance_of? Array
|
831
831
|
value = value.map {|v|
|
832
832
|
if v.instance_of? Hash and v["_ns"]
|
833
|
-
|
833
|
+
Mongo::DBRef.new(v["_ns"], v["_id"])
|
834
834
|
else
|
835
835
|
v
|
836
836
|
end
|
@@ -855,7 +855,7 @@ module MongoRecord
|
|
855
855
|
# +self+ if all is well.
|
856
856
|
def update
|
857
857
|
set_update_times
|
858
|
-
self.class.collection.
|
858
|
+
self.class.collection.update({:_id => @_id}, to_mongo_value)
|
859
859
|
if self.class.collection.db.error?
|
860
860
|
return false
|
861
861
|
end
|
@@ -955,7 +955,7 @@ module MongoRecord
|
|
955
955
|
def init_ivar(ivar_name, val)
|
956
956
|
sym = ivar_name[1..-1].to_sym
|
957
957
|
if self.class.subobjects.keys.include?(sym)
|
958
|
-
if val.instance_of?
|
958
|
+
if val.instance_of? Mongo::DBRef
|
959
959
|
val = self.class.collection.db.dereference(val)
|
960
960
|
end
|
961
961
|
instance_variable_set(ivar_name, self.class.subobjects[sym].new(val))
|
@@ -963,7 +963,7 @@ module MongoRecord
|
|
963
963
|
klazz = self.class.arrays[sym]
|
964
964
|
val = [val] unless val.kind_of?(Array)
|
965
965
|
instance_variable_set(ivar_name, val.collect {|v|
|
966
|
-
if v.instance_of?
|
966
|
+
if v.instance_of? Mongo::DBRef
|
967
967
|
v = self.class.collection.db.dereference(v)
|
968
968
|
end
|
969
969
|
v.kind_of?(MongoRecord::Base) ? v : klazz.new(v)
|
@@ -1,11 +1,11 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'mongo_record'
|
3
|
-
s.version = '0.4.
|
3
|
+
s.version = '0.4.2'
|
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.'
|
7
7
|
|
8
|
-
s.add_dependency('mongodb-mongo', ['>= 0.
|
8
|
+
s.add_dependency('mongodb-mongo', ['>= 0.13'])
|
9
9
|
|
10
10
|
s.require_paths = ['lib']
|
11
11
|
|
data/tests/test_log_device.rb
CHANGED
@@ -24,8 +24,8 @@ class LoggerTest < Test::Unit::TestCase
|
|
24
24
|
MAX_RECS = 3
|
25
25
|
|
26
26
|
@@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
27
|
-
@@port = ENV['MONGO_RUBY_DRIVER_PORT'] ||
|
28
|
-
@@db =
|
27
|
+
@@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::Mongo::DEFAULT_PORT
|
28
|
+
@@db = Mongo::Mongo.new(@@host, @@port).db('mongorecord-test')
|
29
29
|
|
30
30
|
def setup
|
31
31
|
@@db.drop_collection('testlogger') # can't remove recs from capped colls
|
@@ -57,7 +57,7 @@ class LoggerTest < Test::Unit::TestCase
|
|
57
57
|
|
58
58
|
def test_alternate_connection
|
59
59
|
old_db = @@db
|
60
|
-
alt_db =
|
60
|
+
alt_db = Mongo::Mongo.new(@@host, @@port).db('mongorecord-test-log-device')
|
61
61
|
begin
|
62
62
|
@@db = nil
|
63
63
|
MongoRecord::LogDevice.connection = alt_db
|
@@ -67,7 +67,7 @@ class LoggerTest < Test::Unit::TestCase
|
|
67
67
|
|
68
68
|
coll = alt_db.collection('testlogger')
|
69
69
|
assert_equal 1, coll.count()
|
70
|
-
rec = coll.
|
70
|
+
rec = coll.find_one
|
71
71
|
assert_not_nil rec
|
72
72
|
assert_match /test message/, rec['msg']
|
73
73
|
rescue => ex
|
data/tests/test_mongo.rb
CHANGED
@@ -48,8 +48,8 @@ end
|
|
48
48
|
class MongoTest < Test::Unit::TestCase
|
49
49
|
|
50
50
|
@@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
51
|
-
@@port = ENV['MONGO_RUBY_DRIVER_PORT'] ||
|
52
|
-
@@db =
|
51
|
+
@@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::Mongo::DEFAULT_PORT
|
52
|
+
@@db = Mongo::Mongo.new(@@host, @@port).db('mongorecord-test')
|
53
53
|
@@students = @@db.collection('students')
|
54
54
|
@@courses = @@db.collection('courses')
|
55
55
|
@@tracks = @@db.collection('tracks')
|
@@ -65,13 +65,13 @@ class MongoTest < Test::Unit::TestCase
|
|
65
65
|
@@playlists.clear
|
66
66
|
|
67
67
|
# Manually insert data without using MongoRecord::Base
|
68
|
-
@@tracks.insert({:_id =>
|
69
|
-
@@tracks.insert({:_id =>
|
70
|
-
@@tracks.insert({:_id =>
|
71
|
-
@@tracks.insert({:_id =>
|
72
|
-
@mayor_id =
|
68
|
+
@@tracks.insert({:_id => Mongo::ObjectID.new, :artist => 'Thomas Dolby', :album => 'Aliens Ate My Buick', :song => 'The Ability to Swing'})
|
69
|
+
@@tracks.insert({:_id => Mongo::ObjectID.new, :artist => 'Thomas Dolby', :album => 'Aliens Ate My Buick', :song => 'Budapest by Blimp'})
|
70
|
+
@@tracks.insert({:_id => Mongo::ObjectID.new, :artist => 'Thomas Dolby', :album => 'The Golden Age of Wireless', :song => 'Europa and the Pirate Twins'})
|
71
|
+
@@tracks.insert({:_id => Mongo::ObjectID.new, :artist => 'XTC', :album => 'Oranges & Lemons', :song => 'Garden Of Earthly Delights', :track => 1})
|
72
|
+
@mayor_id = Mongo::ObjectID.new
|
73
73
|
@@tracks.insert({:_id => @mayor_id, :artist => 'XTC', :album => 'Oranges & Lemons', :song => 'The Mayor Of Simpleton', :track => 2})
|
74
|
-
@@tracks.insert({:_id =>
|
74
|
+
@@tracks.insert({:_id => Mongo::ObjectID.new, :artist => 'XTC', :album => 'Oranges & Lemons', :song => 'King For A Day', :track => 3})
|
75
75
|
|
76
76
|
@mayor_str = "artist: XTC, album: Oranges & Lemons, song: The Mayor Of Simpleton, track: 2"
|
77
77
|
@mayor_song = 'The Mayor Of Simpleton'
|
@@ -624,7 +624,7 @@ class MongoTest < Test::Unit::TestCase
|
|
624
624
|
def test_alternate_connection
|
625
625
|
old_db = MongoRecord::Base.connection
|
626
626
|
assert_equal @@db, old_db
|
627
|
-
alt_db =
|
627
|
+
alt_db = Mongo::Mongo.new(@@host, @@port).db('mongorecord-test-alt-conn')
|
628
628
|
assert_not_equal old_db, alt_db
|
629
629
|
alt_db.drop_collection('students')
|
630
630
|
begin
|
@@ -755,7 +755,7 @@ class MongoTest < Test::Unit::TestCase
|
|
755
755
|
Track.index [:artist, :created_at]
|
756
756
|
Track.index [:song, :desc], true
|
757
757
|
Track.index [:artist, [:album, :desc]]
|
758
|
-
Track.index [:created_at,
|
758
|
+
Track.index [:created_at, Mongo::ASCENDING]
|
759
759
|
|
760
760
|
assert Track.indexes.has_key?("artist_1")
|
761
761
|
assert Track.indexes.has_key?("artist_1_created_at_1")
|
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.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jim Menard
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
requirements:
|
22
22
|
- - ">="
|
23
23
|
- !ruby/object:Gem::Version
|
24
|
-
version: "0.
|
24
|
+
version: "0.13"
|
25
25
|
version:
|
26
26
|
description: MongoRecord is an ActiveRecord-like framework for the 10gen Mongo database. For more information about Mongo, see http://www.mongodb.org.
|
27
27
|
email: mongodb-user@googlegroups.com
|