mongodb-mongo_record 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +68 -0
- data/Rakefile +38 -0
- data/examples/tracks.rb +107 -0
- data/lib/mongo_record/base.rb +830 -0
- data/lib/mongo_record/convert.rb +45 -0
- data/lib/mongo_record/log_device.rb +113 -0
- data/lib/mongo_record/sql.rb +237 -0
- data/lib/mongo_record/subobject.rb +111 -0
- data/lib/mongo_record.rb +23 -0
- data/mongo-activerecord-ruby.gemspec +35 -0
- data/tests/address.rb +12 -0
- data/tests/course.rb +10 -0
- data/tests/student.rb +34 -0
- data/tests/test_log_device.rb +79 -0
- data/tests/test_mongo.rb +614 -0
- data/tests/test_sql.rb +176 -0
- data/tests/track2.rb +9 -0
- data/tests/track3.rb +9 -0
- metadata +79 -0
data/README.rdoc
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
= Welcome to MongoRecord
|
2
|
+
|
3
|
+
MongoRecord is an ActiveRecord-like framework for the 10gen
|
4
|
+
Mongo[http://www.mongodb.org/] database.
|
5
|
+
|
6
|
+
This document assumes you have read the Mongo documentation.
|
7
|
+
|
8
|
+
A quick code sample:
|
9
|
+
|
10
|
+
require 'rubygems'
|
11
|
+
require 'mongo'
|
12
|
+
require 'mongo_record'
|
13
|
+
|
14
|
+
class Track < MongoRecord::Base
|
15
|
+
collection_name :tracks
|
16
|
+
fields :artist, :album, :song, :track
|
17
|
+
def to_s
|
18
|
+
"artist: #{artist}, album: #{album}, song: #@song, track: #{@track ? @track.to_i : nil}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
MongoRecord::Base.connection =
|
23
|
+
XGen::Mongo::Driver::Mongo.new.db('mongorecord-test')
|
24
|
+
|
25
|
+
t = Track.new(:artist => 'Level 42', :album => 'Standing In The Light',
|
26
|
+
:song => 'Micro-Kid', :track => 1)
|
27
|
+
t.save
|
28
|
+
puts "There are #{Track.count()} tracks."
|
29
|
+
t = Track.find(:first, :conditions => {:song => 'Micro-Kid'})
|
30
|
+
Track.find(:all, :sort => 'song').each { |t| puts t.to_s }
|
31
|
+
|
32
|
+
== Installation
|
33
|
+
|
34
|
+
$ gem sources -a http://gems.github.com
|
35
|
+
$ sudo gem install mongodb-mongo-activerecord-ruby
|
36
|
+
|
37
|
+
MongoRecord depends on the Mongo Ruby Driver. Installing the MongoRecord gem
|
38
|
+
will also install the Mongo Ruby Driver if you don't have it already.
|
39
|
+
|
40
|
+
The source code is available at http://github.com/mongodb/mongo-ruby-driver.
|
41
|
+
You can either clone the git repository or download a tarball or zip file.
|
42
|
+
Once you have the source, you can use it from wherever you downloaded it or
|
43
|
+
you can install it as a gem from the source by typing
|
44
|
+
|
45
|
+
$ rake gem:install
|
46
|
+
|
47
|
+
|
48
|
+
== Getting Started
|
49
|
+
|
50
|
+
See the examples, read the MongoRecord::Base and MongoRecord::Cursor
|
51
|
+
documentation, and look at tests/test_mongo.rb.
|
52
|
+
|
53
|
+
=== Persistence
|
54
|
+
|
55
|
+
You can use MongoRecord::Base or talk to the database (stored in the $db
|
56
|
+
object) directly.
|
57
|
+
|
58
|
+
See MongoRecord::Base and MongoRecord::Cursor.
|
59
|
+
|
60
|
+
=== Logger
|
61
|
+
|
62
|
+
See MongoRecord::LogDevice. When running outside of the cloud (for example,
|
63
|
+
during development), all log messages are echoed to $stderr which is normally
|
64
|
+
the console.
|
65
|
+
|
66
|
+
== To Do
|
67
|
+
|
68
|
+
* DBRefs
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rubygems/specification'
|
3
|
+
require 'fileutils'
|
4
|
+
require 'rake'
|
5
|
+
require 'rake/testtask'
|
6
|
+
require 'rake/gempackagetask'
|
7
|
+
require 'rake/contrib/rubyforgepublisher'
|
8
|
+
|
9
|
+
|
10
|
+
# NOTE: some of the tests assume Mongo is running
|
11
|
+
Rake::TestTask.new do |t|
|
12
|
+
t.test_files = FileList['tests/test*.rb']
|
13
|
+
end
|
14
|
+
|
15
|
+
desc "Generate documentation"
|
16
|
+
task :rdoc do
|
17
|
+
FileUtils.rm_rf('html')
|
18
|
+
system "rdoc --main README.rdoc --op html --inline-source --quiet README.rdoc `find lib -name '*.rb'`"
|
19
|
+
end
|
20
|
+
|
21
|
+
namespace :gem do
|
22
|
+
|
23
|
+
desc "Install the gem locally"
|
24
|
+
task :install do
|
25
|
+
sh <<EOS
|
26
|
+
gem build mongo-activerecord-ruby.gemspec &&
|
27
|
+
sudo gem install mongo_record*.gem &&
|
28
|
+
rm mongo_record-*.gem
|
29
|
+
EOS
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
task :default => :list
|
35
|
+
|
36
|
+
task :list do
|
37
|
+
system 'rake -T'
|
38
|
+
end
|
data/examples/tracks.rb
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
# Use the local copy, even if this gem is already installed.
|
2
|
+
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '../lib')
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'mongo'
|
6
|
+
require 'mongo_record'
|
7
|
+
|
8
|
+
class Track < MongoRecord::Base
|
9
|
+
collection_name :tracks
|
10
|
+
fields :artist, :album, :song, :track
|
11
|
+
def to_s
|
12
|
+
# Uses both accessor methods and ivars themselves
|
13
|
+
"artist: #{artist}, album: #{album}, song: #@song, track: #{@track ? @track.to_i : nil}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
MongoRecord::Base.connection = XGen::Mongo::Driver::Mongo.new.db('mongorecord-test')
|
18
|
+
|
19
|
+
# Create data
|
20
|
+
|
21
|
+
puts "Creating 6 records using \"raw\" Mongo access..."
|
22
|
+
db = MongoRecord::Base.connection
|
23
|
+
coll = db.collection('tracks')
|
24
|
+
coll.remove({})
|
25
|
+
coll.insert({:_id => XGen::Mongo::Driver::ObjectID.new, :artist => 'Thomas Dolby', :album => 'Aliens Ate My Buick', :song => 'The Ability to Swing'})
|
26
|
+
coll.insert({:_id => XGen::Mongo::Driver::ObjectID.new, :artist => 'Thomas Dolby', :album => 'Aliens Ate My Buick', :song => 'Budapest by Blimp'})
|
27
|
+
coll.insert({:_id => XGen::Mongo::Driver::ObjectID.new, :artist => 'Thomas Dolby', :album => 'The Golden Age of Wireless', :song => 'Europa and the Pirate Twins'})
|
28
|
+
coll.insert({:_id => XGen::Mongo::Driver::ObjectID.new, :artist => 'XTC', :album => 'Oranges & Lemons', :song => 'Garden Of Earthly Delights', :track => 1})
|
29
|
+
coll.insert({:_id => XGen::Mongo::Driver::ObjectID.new, :artist => 'XTC', :album => 'Oranges & Lemons', :song => 'The Mayor Of Simpleton', :track => 2})
|
30
|
+
song_id = XGen::Mongo::Driver::ObjectID.new
|
31
|
+
coll.insert({:_id => song_id, :artist => 'XTC', :album => 'Oranges & Lemons', :song => 'King For A Day', :track => 3})
|
32
|
+
puts "Data created. One song_id = #{song_id}."
|
33
|
+
puts "There are #{coll.count()} records in the tracks collection."
|
34
|
+
|
35
|
+
|
36
|
+
puts "\nSimple find"
|
37
|
+
puts Track.find(song_id).to_s
|
38
|
+
puts Track.find(song_id, :select => :album).to_s
|
39
|
+
puts Track.find_by_id(song_id).to_s
|
40
|
+
puts Track.find_by_song("Budapest by Blimp").to_s
|
41
|
+
|
42
|
+
puts "\nCount"
|
43
|
+
puts "Yup; there are indeed #{Track.count} records in the tracks collection."
|
44
|
+
|
45
|
+
puts "\nUpdate"
|
46
|
+
x = Track.find_by_track(2)
|
47
|
+
x.track = 99
|
48
|
+
x.save
|
49
|
+
puts Track.find_by_track(99).to_s
|
50
|
+
|
51
|
+
puts "\nComplex find"
|
52
|
+
puts Track.find_by_song('The Mayor Of Simpleton').to_s
|
53
|
+
|
54
|
+
puts "\nFind all"
|
55
|
+
Track.find(:all).each { |t| puts t.to_s }
|
56
|
+
|
57
|
+
puts "\nFind song /to/"
|
58
|
+
Track.find(:all, :conditions => {:song => /to/}).each { |row| puts row.to_s }
|
59
|
+
|
60
|
+
puts "\nFind limit 2"
|
61
|
+
Track.find(:all, :limit => 2).each { |t| puts t.to_s }
|
62
|
+
|
63
|
+
puts "\nFind by album"
|
64
|
+
Track.find(:all, :conditions => {:album => 'Aliens Ate My Buick'}).each { |t| puts t.to_s }
|
65
|
+
|
66
|
+
puts "\nFind first"
|
67
|
+
puts Track.find(:first).to_s
|
68
|
+
|
69
|
+
puts "\nFind track 3"
|
70
|
+
puts Track.find(:first, :conditions => {:track => 3}).to_s
|
71
|
+
|
72
|
+
puts "\nfind_by_album"
|
73
|
+
Track.find_all_by_album('Oranges & Lemons').each { |t| puts t.to_s }
|
74
|
+
|
75
|
+
puts "\nSorting"
|
76
|
+
Track.find(:all, :order => 'album desc').each { |t| puts t.to_s }
|
77
|
+
|
78
|
+
puts "\nTrack.new"
|
79
|
+
|
80
|
+
puts Track.new.to_s
|
81
|
+
|
82
|
+
t = Track.new(:artist => 'Level 42', :album => 'Standing In The Light', :song => 'Micro-Kid', :track => 1)
|
83
|
+
puts t.to_s
|
84
|
+
puts "save returned #{t.save}"
|
85
|
+
|
86
|
+
puts "\nTrack.find_or_create_by_song"
|
87
|
+
|
88
|
+
s, a = 'The Ability to Swing', 'ignored because song found'
|
89
|
+
puts Track.find_or_create_by_song(s, :artist => a).to_s
|
90
|
+
|
91
|
+
s, ar, al = 'New Song', 'New Artist', 'New Album'
|
92
|
+
puts Track.find_or_create_by_song(s, :artist => ar, :album => al).to_s
|
93
|
+
|
94
|
+
puts "\nTrack.find(:first, :conditions => {:song => 'King For A Day'}).delete"
|
95
|
+
t = Track.find(:first, :conditions => {:song => 'King For A Day'}).delete
|
96
|
+
Track.find(:all).each { |t| puts t.to_s }
|
97
|
+
|
98
|
+
puts "\nTrack.find('bogus_id')"
|
99
|
+
puts "I should see an exception here:"
|
100
|
+
begin
|
101
|
+
Track.find('bogus_id')
|
102
|
+
rescue => ex
|
103
|
+
puts ex.to_s
|
104
|
+
end
|
105
|
+
|
106
|
+
puts "\nexplain()"
|
107
|
+
puts Track.find(:all, :conditions => {:song => 'King For A Day'}).explain().inspect
|