mongo_record 0.4.4 → 0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +3 -3
- data/lib/mongo_record/base.rb +16 -37
- data/lib/mongo_record/{convert.rb → core_ext.rb} +17 -1
- data/lib/mongo_record/log_device.rb +1 -1
- data/lib/mongo_record/sql.rb +1 -1
- data/lib/mongo_record/subobject.rb +1 -1
- data/lib/mongo_record.rb +1 -1
- data/{mongo-activerecord-ruby.gemspec → mongo-record.gemspec} +6 -6
- data/tests/test_log_device.rb +1 -1
- data/tests/test_mongo.rb +20 -12
- data/tests/test_sql.rb +1 -1
- metadata +24 -14
data/Rakefile
CHANGED
@@ -23,9 +23,9 @@ namespace :gem do
|
|
23
23
|
desc "Install the gem locally"
|
24
24
|
task :install do
|
25
25
|
sh <<EOS
|
26
|
-
gem build mongo-
|
27
|
-
|
28
|
-
rm mongo_record
|
26
|
+
gem build mongo-record.gemspec &&
|
27
|
+
gem install mongo_record*.gem &&
|
28
|
+
rm mongo_record*.gem
|
29
29
|
EOS
|
30
30
|
end
|
31
31
|
|
data/lib/mongo_record/base.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright 2009 10gen, Inc.
|
1
|
+
# Copyright 2009-2010 10gen, Inc.
|
2
2
|
#
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
4
|
# you may not use this file except in compliance with the License.
|
@@ -13,26 +13,8 @@
|
|
13
13
|
# limitations under the License.
|
14
14
|
|
15
15
|
require 'rubygems'
|
16
|
-
require '
|
17
|
-
require 'mongo/types/code'
|
18
|
-
require 'mongo/cursor'
|
19
|
-
require 'mongo_record/convert'
|
16
|
+
require 'mongo_record/core_ext'
|
20
17
|
require 'mongo_record/sql'
|
21
|
-
#require 'active_support/core_ext' # symbolize_keys!
|
22
|
-
|
23
|
-
class String
|
24
|
-
# Convert this String to an ObjectID.
|
25
|
-
def to_oid
|
26
|
-
Mongo::ObjectID.legal?(self) ? Mongo::ObjectID.from_string(self) : self
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
class Mongo::ObjectID
|
31
|
-
# Convert this object to an ObjectID.
|
32
|
-
def to_oid
|
33
|
-
self
|
34
|
-
end
|
35
|
-
end
|
36
18
|
|
37
19
|
module MongoRecord
|
38
20
|
|
@@ -40,7 +22,7 @@ module MongoRecord
|
|
40
22
|
def create_pk(row)
|
41
23
|
return row if row[:_id]
|
42
24
|
row.delete(:_id) # in case it is nil
|
43
|
-
row['_id'] ||=
|
25
|
+
row['_id'] ||= BSON::ObjectID.new
|
44
26
|
row
|
45
27
|
end
|
46
28
|
end
|
@@ -152,9 +134,8 @@ module MongoRecord
|
|
152
134
|
# or an array of fields ([:title, :author, :date])
|
153
135
|
# or an array of a field name and direction ([:title, :asc] or [:title, :desc])
|
154
136
|
# or an array of field names and directions ([[:title, :asc], [:author, :desc]])
|
155
|
-
# +
|
156
|
-
|
157
|
-
def index(fields, unique = false)
|
137
|
+
# +options+ Same as options for create index in the ruby driver
|
138
|
+
def index(fields, options={})
|
158
139
|
fields = Array(fields)
|
159
140
|
|
160
141
|
if fields.length == 2 &&
|
@@ -169,7 +150,7 @@ module MongoRecord
|
|
169
150
|
field
|
170
151
|
end
|
171
152
|
|
172
|
-
collection.create_index(fields,
|
153
|
+
collection.create_index(fields, options)
|
173
154
|
end
|
174
155
|
|
175
156
|
# Returns list of indexes for model, unless fields are passed.
|
@@ -321,9 +302,6 @@ module MongoRecord
|
|
321
302
|
# Person.find(:all, :offset => 10, :limit => 10, :order => :created_on)
|
322
303
|
# is the same as
|
323
304
|
# Person.find(:all).skip(10).limit(10).sort({:created_on => 1})
|
324
|
-
#
|
325
|
-
|
326
|
-
|
327
305
|
def find(*args)
|
328
306
|
options = extract_options_from_args!(args)
|
329
307
|
options.symbolize_keys!
|
@@ -525,7 +503,7 @@ module MongoRecord
|
|
525
503
|
cursor = find_every(options)
|
526
504
|
one = cursor.detect {|c| c}
|
527
505
|
cursor.close
|
528
|
-
one
|
506
|
+
one.nil? ? nil : new(one)
|
529
507
|
end
|
530
508
|
|
531
509
|
def find_every(options)
|
@@ -534,14 +512,15 @@ module MongoRecord
|
|
534
512
|
|
535
513
|
find_options = {}
|
536
514
|
find_options[:fields] = fields_from(options[:select]) if options[:select]
|
537
|
-
find_options[:limit]
|
515
|
+
find_options[:limit] = options[:limit].to_i if options[:limit]
|
538
516
|
find_options[:offset] = options[:offset].to_i if options[:offset]
|
539
|
-
find_options[:
|
517
|
+
find_options[:hint] = options[:hint] if options[:hint]
|
518
|
+
find_options[:sort] = sort_by_from(options[:order]) if options[:order]
|
540
519
|
|
541
520
|
cursor = collection.find(criteria, find_options)
|
542
521
|
|
543
522
|
# Override cursor.next_object so it returns a new instance of this class
|
544
|
-
eval "def cursor.
|
523
|
+
eval "def cursor.next_document; #{self.name}.new(super()); end"
|
545
524
|
cursor
|
546
525
|
end
|
547
526
|
|
@@ -670,7 +649,7 @@ module MongoRecord
|
|
670
649
|
# +func+ must be +nil+ or a JavaScript expression or function in a
|
671
650
|
# string.
|
672
651
|
def where_func(func) # :nodoc:
|
673
|
-
func ? {'$where' =>
|
652
|
+
func ? {'$where' => BSON::Code.new(func)} : {}
|
674
653
|
end
|
675
654
|
|
676
655
|
def replace_named_bind_variables(str, h) # :nodoc:
|
@@ -870,11 +849,11 @@ module MongoRecord
|
|
870
849
|
key_names.each {|key|
|
871
850
|
value = instance_variable_get("@#{key}").to_mongo_value
|
872
851
|
if value.instance_of? Hash and value["_ns"]
|
873
|
-
value =
|
852
|
+
value = BSON::DBRef.new(value["_ns"], value["_id"])
|
874
853
|
elsif value.instance_of? Array
|
875
854
|
value = value.map {|v|
|
876
855
|
if v.instance_of? Hash and v["_ns"]
|
877
|
-
|
856
|
+
BSON::DBRef.new(v["_ns"], v["_id"])
|
878
857
|
else
|
879
858
|
v
|
880
859
|
end
|
@@ -1011,7 +990,7 @@ module MongoRecord
|
|
1011
990
|
def init_ivar(ivar_name, val)
|
1012
991
|
sym = ivar_name[1..-1].to_sym
|
1013
992
|
if self.class.subobjects.keys.include?(sym)
|
1014
|
-
if val.instance_of?
|
993
|
+
if val.instance_of? BSON::DBRef
|
1015
994
|
val = self.class.collection.db.dereference(val)
|
1016
995
|
end
|
1017
996
|
instance_variable_set(ivar_name, self.class.subobjects[sym].new(val))
|
@@ -1019,7 +998,7 @@ module MongoRecord
|
|
1019
998
|
klazz = self.class.arrays[sym]
|
1020
999
|
val = [val] unless val.kind_of?(Array)
|
1021
1000
|
instance_variable_set(ivar_name, val.collect {|v|
|
1022
|
-
if v.instance_of?
|
1001
|
+
if v.instance_of? BSON::DBRef
|
1023
1002
|
v = self.class.collection.db.dereference(v)
|
1024
1003
|
end
|
1025
1004
|
v.kind_of?(MongoRecord::Base) ? v : klazz.new(v)
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright 2009 10gen, Inc.
|
1
|
+
# Copyright 2009-2010 10gen, Inc.
|
2
2
|
#
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
4
|
# you may not use this file except in compliance with the License.
|
@@ -62,3 +62,19 @@ class Hash
|
|
62
62
|
self.replace(self.symbolize_keys)
|
63
63
|
end
|
64
64
|
end
|
65
|
+
|
66
|
+
class String
|
67
|
+
|
68
|
+
# Convert this String to an ObjectID.
|
69
|
+
def to_oid
|
70
|
+
BSON::ObjectID.legal?(self) ? BSON::ObjectID.from_string(self) : self
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
class BSON::ObjectID
|
75
|
+
|
76
|
+
# Convert this object to an ObjectID.
|
77
|
+
def to_oid
|
78
|
+
self
|
79
|
+
end
|
80
|
+
end
|
data/lib/mongo_record/sql.rb
CHANGED
data/lib/mongo_record.rb
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'mongo_record'
|
3
|
-
s.version = '0.
|
3
|
+
s.version = '0.5'
|
4
4
|
s.platform = Gem::Platform::RUBY
|
5
|
-
s.summary = 'ActiveRecord-like models for
|
6
|
-
s.description = 'MongoRecord is an ActiveRecord-like framework for MongoDB. For more information about
|
5
|
+
s.summary = 'ActiveRecord-like models for MongoDB'
|
6
|
+
s.description = 'MongoRecord is an ActiveRecord-like framework for MongoDB. For more information about MongoDB, see http://www.mongodb.org.'
|
7
7
|
|
8
|
-
s.add_dependency('mongo', ['>= 0.
|
8
|
+
s.add_dependency('mongo', ['>= 0.20.1'])
|
9
9
|
|
10
10
|
s.require_paths = ['lib']
|
11
11
|
|
12
12
|
s.files = ['examples/tracks.rb', 'lib/mongo_record.rb',
|
13
13
|
'lib/mongo_record/base.rb',
|
14
|
-
'lib/mongo_record/
|
14
|
+
'lib/mongo_record/core_ext.rb',
|
15
15
|
'lib/mongo_record/log_device.rb',
|
16
16
|
'lib/mongo_record/sql.rb',
|
17
17
|
'lib/mongo_record/subobject.rb',
|
18
18
|
'README.rdoc', 'Rakefile',
|
19
|
-
'mongo-
|
19
|
+
'mongo-record.gemspec',
|
20
20
|
'LICENSE']
|
21
21
|
s.test_files = ['tests/address.rb',
|
22
22
|
'tests/course.rb',
|
data/tests/test_log_device.rb
CHANGED
data/tests/test_mongo.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright 2009 10gen, Inc.
|
1
|
+
# Copyright 2009-2010 10gen, Inc.
|
2
2
|
#
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
4
|
# you may not use this file except in compliance with the License.
|
@@ -65,13 +65,13 @@ class MongoTest < Test::Unit::TestCase
|
|
65
65
|
@@playlists.remove
|
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 => BSON::ObjectID.new, :artist => 'Thomas Dolby', :album => 'Aliens Ate My Buick', :song => 'The Ability to Swing'})
|
69
|
+
@@tracks.insert({:_id => BSON::ObjectID.new, :artist => 'Thomas Dolby', :album => 'Aliens Ate My Buick', :song => 'Budapest by Blimp'})
|
70
|
+
@@tracks.insert({:_id => BSON::ObjectID.new, :artist => 'Thomas Dolby', :album => 'The Golden Age of Wireless', :song => 'Europa and the Pirate Twins'})
|
71
|
+
@@tracks.insert({:_id => BSON::ObjectID.new, :artist => 'XTC', :album => 'Oranges & Lemons', :song => 'Garden Of Earthly Delights', :track => 1})
|
72
|
+
@mayor_id = BSON::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 => BSON::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'
|
@@ -323,6 +323,16 @@ class MongoTest < Test::Unit::TestCase
|
|
323
323
|
assert_equal(x.id, z.id)
|
324
324
|
end
|
325
325
|
|
326
|
+
def test_find_with_hint
|
327
|
+
@@tracks.create_index([['artist', 1]])
|
328
|
+
assert_equal "BtreeCursor artist_1",
|
329
|
+
Track.find(:all, :conditions => {:artist => 'XTC'}).explain["cursor"]
|
330
|
+
|
331
|
+
assert_equal "BasicCursor",
|
332
|
+
Track.find(:all, :hint => {'$natural' => 1},
|
333
|
+
:conditions => {:artist => 'XTC'}).explain["cursor"]
|
334
|
+
end
|
335
|
+
|
326
336
|
def test_find_or_create_but_already_exists
|
327
337
|
assert_equal("artist: Thomas Dolby, album: Aliens Ate My Buick, song: The Ability to Swing, track: ",
|
328
338
|
Track.find_or_create_by_song('The Ability to Swing', :artist => 'ignored because song found').to_s)
|
@@ -404,7 +414,6 @@ class MongoTest < Test::Unit::TestCase
|
|
404
414
|
assert_no_match(/song: Budapest by Blimp/, Track.all.inject('') { |str, t| str + t.to_s })
|
405
415
|
|
406
416
|
assert_equal 6, Track.count
|
407
|
-
Track.index [:song, :desc], true # reindex
|
408
417
|
end
|
409
418
|
|
410
419
|
def test_delete_all
|
@@ -447,9 +456,8 @@ class MongoTest < Test::Unit::TestCase
|
|
447
456
|
t = Track.find_by_song('King For A Day')
|
448
457
|
tid = t._id
|
449
458
|
# first is string id, second is ObjectID
|
450
|
-
|
451
|
-
|
452
|
-
assert str.include?('King For A Day')
|
459
|
+
records = Track.find([@mayor_id, tid]).to_a
|
460
|
+
assert_equal 2, records.size
|
453
461
|
end
|
454
462
|
|
455
463
|
def test_find_one_using_id
|
@@ -890,7 +898,7 @@ class MongoTest < Test::Unit::TestCase
|
|
890
898
|
def test_indexing
|
891
899
|
Track.index :artist
|
892
900
|
Track.index [:artist, :created_at]
|
893
|
-
Track.index [:song, :desc], true
|
901
|
+
Track.index [:song, :desc], :unique => true
|
894
902
|
Track.index [:artist, [:album, :desc]]
|
895
903
|
Track.index [:created_at, Mongo::ASCENDING]
|
896
904
|
|
data/tests/test_sql.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 5
|
8
|
+
version: "0.5"
|
5
9
|
platform: ruby
|
6
10
|
authors:
|
7
11
|
- Jim Menard
|
@@ -10,20 +14,24 @@ autorequire:
|
|
10
14
|
bindir: bin
|
11
15
|
cert_chain: []
|
12
16
|
|
13
|
-
date:
|
17
|
+
date: 2010-04-27 00:00:00 -04:00
|
14
18
|
default_executable:
|
15
19
|
dependencies:
|
16
20
|
- !ruby/object:Gem::Dependency
|
17
21
|
name: mongo
|
18
|
-
|
19
|
-
|
20
|
-
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
21
24
|
requirements:
|
22
25
|
- - ">="
|
23
26
|
- !ruby/object:Gem::Version
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
- 20
|
30
|
+
- 1
|
31
|
+
version: 0.20.1
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
description: MongoRecord is an ActiveRecord-like framework for MongoDB. For more information about MongoDB, see http://www.mongodb.org.
|
27
35
|
email: mongodb-user@googlegroups.com
|
28
36
|
executables: []
|
29
37
|
|
@@ -35,13 +43,13 @@ files:
|
|
35
43
|
- examples/tracks.rb
|
36
44
|
- lib/mongo_record.rb
|
37
45
|
- lib/mongo_record/base.rb
|
38
|
-
- lib/mongo_record/
|
46
|
+
- lib/mongo_record/core_ext.rb
|
39
47
|
- lib/mongo_record/log_device.rb
|
40
48
|
- lib/mongo_record/sql.rb
|
41
49
|
- lib/mongo_record/subobject.rb
|
42
50
|
- README.rdoc
|
43
51
|
- Rakefile
|
44
|
-
- mongo-
|
52
|
+
- mongo-record.gemspec
|
45
53
|
- LICENSE
|
46
54
|
has_rdoc: true
|
47
55
|
homepage: http://www.mongodb.org
|
@@ -58,21 +66,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
58
66
|
requirements:
|
59
67
|
- - ">="
|
60
68
|
- !ruby/object:Gem::Version
|
69
|
+
segments:
|
70
|
+
- 0
|
61
71
|
version: "0"
|
62
|
-
version:
|
63
72
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
73
|
requirements:
|
65
74
|
- - ">="
|
66
75
|
- !ruby/object:Gem::Version
|
76
|
+
segments:
|
77
|
+
- 0
|
67
78
|
version: "0"
|
68
|
-
version:
|
69
79
|
requirements: []
|
70
80
|
|
71
81
|
rubyforge_project:
|
72
|
-
rubygems_version: 1.3.
|
82
|
+
rubygems_version: 1.3.6
|
73
83
|
signing_key:
|
74
84
|
specification_version: 3
|
75
|
-
summary: ActiveRecord-like models for
|
85
|
+
summary: ActiveRecord-like models for MongoDB
|
76
86
|
test_files:
|
77
87
|
- tests/address.rb
|
78
88
|
- tests/course.rb
|