Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add update method

  • Loading branch information...
commit 868069867c56755c110f35be15988e7a8a8238a8 1 parent 5f95468
@gdb authored
View
22 lib/embedded-mongo/backend/collection.rb
@@ -1,3 +1,5 @@
+# TODO: deep clone upon insert
+
module EmbeddedMongo::Backend
class Collection
class DuplicateKeyError < StandardError; end
@@ -25,6 +27,14 @@ def find(selector)
results
end
+ def update(selector, update, opts)
+ @data.each do |doc|
+ next unless selector_match?(selector, doc)
+ apply_update!(update, doc)
+ break unless opts[:multi]
+ end
+ end
+
private
def check_id(doc)
@@ -41,7 +51,7 @@ def insert(doc)
check_id(doc)
check_duplicate_key(doc)
rescue DuplicateKeyError
- $stderr.puts "Duplicate key error: #{id}"
+ EmbeddedMongo.log.info("Duplicate key error: #{id}")
return
end
@@ -51,5 +61,15 @@ def insert(doc)
def selector_match?(selector, doc)
selector.all? { |k, v| doc[k] == v }
end
+
+ def apply_update!(update, doc)
+ EmbeddedMongo.log.info("Applying update: #{update.inspect} to #{doc.inspect}")
+ id = doc['_id']
+ doc.clear
+ update.each do |k, v|
+ doc[k] = v
+ end
+ doc['_id'] ||= id
+ end
end
end
View
6 lib/embedded-mongo/backend/manager.rb
@@ -17,6 +17,12 @@ def find(db_name, collection_name, selector)
collection.find(selector)
end
+ def update(db_name, collection_name, selector, update, opts)
+ EmbeddedMongo.log.info("FIND: #{db_name.inspect} #{collection_name.inspect} #{selector.inspect} #{update.inspect} #{opts.inspect}")
+ collection = get_collection(db_name, collection_name)
+ collection.update(selector, update, opts)
+ end
+
private
def get_db(db_name)
View
8 lib/embedded-mongo/collection.rb
@@ -1,10 +1,16 @@
module EmbeddedMongo
class Collection < Mongo::Collection
def insert_documents(documents, collection_name=@name, check_keys=true, safe=false)
- puts "documents: #{documents.inspect}, #{collection_name.inspect}, #{check_keys.inspect}, #{safe.inspect}"
+ EmbeddedMongo.log.debug("insert_documents: #{documents.inspect}, #{collection_name.inspect}, #{check_keys.inspect}, #{safe.inspect}")
@connection.request(:insert_documents, @db.name, collection_name, documents)
end
+ def update(selector, document, opts={})
+ EmbeddedMongo.log.debug("update: #{selector.inspect}, #{document.inspect}, #{opts.inspect}")
+ opts = { :safe => @safe }.merge(opts)
+ @connection.request(:update, @db.name, @name, selector, document, opts)
+ end
+
# verbatim
def find(selector={}, opts={})
fields = opts.delete(:fields)
View
10 lib/embedded-mongo/util.rb
@@ -0,0 +1,10 @@
+module EmbeddedMongo
+ module Util
+ def self.stringify_keys!(hash)
+ hash.keys.each do |key|
+ hash[key.to_s] = hash.delete(key)
+ end
+ hash
+ end
+ end
+end
View
22 test/functional/test_interface.rb
@@ -5,7 +5,9 @@
class InterfaceTest < Test::Unit::TestCase
def setup
+ # Tests should pass with either of the following lines
@conn = EmbeddedMongo::Connection.new
+ # @conn = Mongo::Connection.new
@test_db = @conn['test']
@foo_collection = @test_db['foo']
end
@@ -17,4 +19,24 @@ def test_insert_and_find
assert_equal(1, cursor.count)
assert_equal({ '_id' => id, 'bar' => 'baz'}, cursor.first)
end
+
+ def test_insert_update_and_find
+ id = @foo_collection.insert({ 'zombie' => 'baz' })
+ @foo_collection.update({ 'zombie' => 'baz' }, { 'test' => 'tar' })
+
+ cursor = @foo_collection.find({ '_id' => id })
+ assert_equal(1, cursor.count)
+ assert_equal({ '_id' => id, 'test' => 'tar'}, cursor.first)
+ end
+
+ def test_changing_ids
+ id = @foo_collection.insert({ 'zing' => 'zong' })
+ @foo_collection.update({ '_id' => id }, { '_id' => 'other_id' })
+
+ cursor = @foo_collection.find({ '_id' => id })
+ assert_equal(0, cursor.count)
+ cursor = @foo_collection.find({ '_id' => 'other_id' })
+ assert_equal(1, cursor.count)
+ assert_equal({ '_id' => 'other_id' }, cursor.first)
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.