mongodb-mongo 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -75,7 +75,7 @@ how to interpret them. You can tell the Ruby Mongo driver how to create
75
75
  primary keys by passing in the :pk option to the Mongo#db method.
76
76
 
77
77
  include XGen::Mongo::Driver
78
- db = Mongo.new.db('dbname', :pk => MyPKFactory)
78
+ db = Mongo.new.db('dbname', :pk => MyPKFactory.new)
79
79
 
80
80
  A primary key factory object must respond to :create_pk, which should take a
81
81
  hash and return a hash which merges the original hash with any primary key
@@ -94,7 +94,7 @@ Here is a sample primary key factory, taken from the tests:
94
94
  end
95
95
  end
96
96
 
97
- A database's PK factory object may be changed, but this is not recommended.
97
+ A database's PK factory object may be set after you obtain it, but only once.
98
98
  The only reason it is changeable is so that libraries such as MongoRecord that
99
99
  use this driver can set the PK factory after obtaining the database but before
100
100
  using it for the first time.
data/lib/mongo/mongo.rb CHANGED
@@ -61,19 +61,12 @@ module XGen
61
61
  # Returns a hash containing database names as keys and disk space for
62
62
  # each as values.
63
63
  def database_info
64
- admin_db = nil
65
- begin
66
- admin_db = db('admin')
67
- doc = admin_db.db_command(:listDatabases => 1)
68
- raise "error retrieving database info" unless admin_db.ok?(doc)
69
- h = {}
70
- doc['databases'].each { |db|
71
- h[db['name']] = db['sizeOnDisk'].to_i
72
- }
73
- h
74
- ensure
75
- admin_db.close
76
- end
64
+ doc = single_db_command('admin', :listDatabases => 1)
65
+ h = {}
66
+ doc['databases'].each { |db|
67
+ h[db['name']] = db['sizeOnDisk'].to_i
68
+ }
69
+ h
77
70
  end
78
71
 
79
72
  # Returns an array of database names.
@@ -91,6 +84,28 @@ module XGen
91
84
  raise "not implemented"
92
85
  end
93
86
 
87
+ # Drops the database +name+.
88
+ def drop_database(name)
89
+ single_db_command(name, :dropDatabase => 1)
90
+ end
91
+
92
+ protected
93
+
94
+ # Send cmd (a hash, possibly ordered) to the admin database and return
95
+ # the answer. Raises an error unless the return is "ok" (DB#ok?
96
+ # returns +true+).
97
+ def single_db_command(db_name, cmd)
98
+ db = nil
99
+ begin
100
+ db = db(db_name)
101
+ doc = db.db_command(cmd)
102
+ raise "error retrieving database info" unless db.ok?(doc)
103
+ doc
104
+ ensure
105
+ db.close
106
+ end
107
+ end
108
+
94
109
  end
95
110
  end
96
111
  end
@@ -46,6 +46,8 @@ class BSON
46
46
  NUMBER_INT = 16
47
47
  MAXKEY = 127
48
48
 
49
+ BYTE_TYPE = 2
50
+
49
51
  if RUBY_VERSION >= '1.9'
50
52
  def self.to_utf8(str)
51
53
  str.encode("utf-8")
@@ -258,6 +260,8 @@ class BSON
258
260
  end
259
261
 
260
262
  def deserialize_binary_data(buf)
263
+ buf.get_int # length + 4; ignored
264
+ buf.get # byte type; ignored
261
265
  len = buf.get_int
262
266
  bytes = buf.get(len)
263
267
  str = ''
@@ -282,7 +286,7 @@ class BSON
282
286
  def serialize_binary_element(buf, key, val)
283
287
  buf.put(BINARY)
284
288
  self.class.serialize_cstr(buf, key)
285
- buf.put_int(val.length)
289
+
286
290
  bytes = if RUBY_VERSION >= '1.9'
287
291
  val.bytes.to_a
288
292
  else
@@ -290,6 +294,11 @@ class BSON
290
294
  val.each_byte { |byte| a << byte }
291
295
  a
292
296
  end
297
+
298
+ num_bytes = bytes.length
299
+ buf.put_int(num_bytes + 4)
300
+ buf.put(BYTE_TYPE)
301
+ buf.put_int(num_bytes)
293
302
  buf.put_array(bytes)
294
303
  end
295
304
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'mongo'
3
- s.version = '0.1.3'
3
+ s.version = '0.1.4'
4
4
  s.platform = Gem::Platform::RUBY
5
5
  s.summary = 'Simple pure-Ruby driver for the 10gen Mongo DB'
6
6
  s.description = 'A pure-Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.'
@@ -48,6 +48,7 @@ Gem::Specification.new do |s|
48
48
  'tests/test_db_api.rb',
49
49
  'tests/test_db_connection.rb',
50
50
  'tests/test_message.rb',
51
+ 'tests/test_mongo.rb',
51
52
  'tests/test_objectid.rb',
52
53
  'tests/test_ordered_hash.rb',
53
54
  'tests/test_round_trip.rb']
@@ -0,0 +1,44 @@
1
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'mongo'
3
+ require 'test/unit'
4
+
5
+ # NOTE: assumes Mongo is running
6
+ class MongoTest < Test::Unit::TestCase
7
+
8
+ include XGen::Mongo::Driver
9
+
10
+ def setup
11
+ @host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
12
+ @port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
13
+ @mongo = Mongo.new(@host, @port)
14
+ end
15
+
16
+ def test_database_info
17
+ info = @mongo.database_info
18
+ assert_not_nil info
19
+ assert_kind_of Hash, info
20
+ assert_not_nil info['admin']
21
+ assert info['admin'] > 0
22
+ end
23
+
24
+ def test_database_names
25
+ names = @mongo.database_names
26
+ assert_not_nil names
27
+ assert_kind_of Array, names
28
+ assert names.length >= 1
29
+ assert names.include?('admin')
30
+ end
31
+
32
+ def test_drop_database
33
+ db = @mongo.db('will-be-deleted')
34
+ coll = db.collection('temp')
35
+ coll.clear
36
+ coll.insert(:name => 'temp')
37
+ assert_equal 1, coll.count()
38
+ assert @mongo.database_names.include?('will-be-deleted')
39
+
40
+ @mongo.drop_database('will-be-deleted')
41
+ assert !@mongo.database_names.include?('will-be-deleted')
42
+ end
43
+
44
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongodb-mongo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Menard
@@ -95,6 +95,7 @@ test_files:
95
95
  - tests/test_db_api.rb
96
96
  - tests/test_db_connection.rb
97
97
  - tests/test_message.rb
98
+ - tests/test_mongo.rb
98
99
  - tests/test_objectid.rb
99
100
  - tests/test_ordered_hash.rb
100
101
  - tests/test_round_trip.rb