Permalink
Browse files

Use deep_clone on inserted objects; fix types in switch statements; a…

…dd collections_info method
  • Loading branch information...
1 parent 16b4493 commit eb289747ac412761b2cf9433dbcbe0f4f5e62c7a @gdb committed Jul 11, 2011
Showing with 19 additions and 12 deletions.
  1. +6 −10 lib/embedded-mongo/backend/collection.rb
  2. +7 −0 lib/embedded-mongo/db.rb
  3. +6 −2 lib/embedded-mongo/util.rb
@@ -1,10 +1,9 @@
-# TODO: deep clone upon insert
-
module EmbeddedMongo::Backend
class Collection
class DuplicateKeyError < StandardError; end
def initialize(db, name)
+ # TODO: system.namespaces
raise ArgumentError.new("Invalid collection name #{name.inspect}") if name['.'] or name['$']
@db = db
@name = name
@@ -85,7 +84,7 @@ def insert(doc)
return
end
- @data << doc
+ @data << EmbeddedMongo::Util.deep_clone(doc)
end
def sort_cmp(sort, x, y)
@@ -129,7 +128,7 @@ def selector_match?(selector, doc)
def partial_match?(partial_selector, value)
EmbeddedMongo.log.debug("partial_match? #{partial_selector.inspect} #{value.inspect}")
case partial_selector
- when Array, Numeric, String, BSON::ObjectId, Boolean, nil
+ when Array, Numeric, String, BSON::ObjectId, TrueClass, FalseClass, nil
partial_selector == value
when Hash
if no_directive?(partial_selector)
@@ -172,21 +171,18 @@ def directive_match?(directive_key, directive_value, value)
def apply_update!(update, doc)
EmbeddedMongo.log.info("Applying update: #{update.inspect} to #{doc.inspect}")
+ id = doc['_id']
if no_directive?(update)
- id = doc['_id']
doc.clear
- update.each do |k, v|
- doc[k] = v
- end
- doc['_id'] ||= id
+ update.each { |k, v| doc[k] = v }
else
# TODO: should set_last_error to {"err"=>"Modifiers and non-modifiers cannot be mixed", "code"=>10154, "n"=>0, "ok"=>1.0}
raise NotImplementedError.new("Modifiers and non-modifiers cannot be mixed #{update.inspect}") if has_non_directive?(update)
update.each do |directive_key, directive_value|
apply_update_directive!(directive_key, directive_value, doc)
end
- doc['_id'] ||= id
end
+ doc['_id'] ||= id
end
def apply_update_directive!(directive_key, directive_value, doc)
View
@@ -43,5 +43,12 @@ def collections
Collection.new(name, self)
end
end
+
+ # verbatim
+ def collections_info(coll_name=nil)
+ selector = {}
+ selector[:name] = full_collection_name(coll_name) if coll_name
+ Cursor.new(Collection.new(SYSTEM_NAMESPACE_COLLECTION, self), :selector => selector)
+ end
end
end
@@ -1,11 +1,15 @@
module EmbeddedMongo
module Util
- # TODO: make this recursive / stringify values
def self.stringify_hash!(hash)
raise ArgumentError.new("Argument is not a hash: #{hash.inspect}") unless hash.kind_of?(Hash)
stringify!(hash)
end
+ def self.deep_clone(obj)
+ # TODO: come up with something less hackish
+ Marshal.load(Marshal.dump(obj))
+ end
+
private
def self.stringify!(struct)
@@ -19,7 +23,7 @@ def self.stringify!(struct)
struct.each_with_index { |entry, i| struct[i] = stringify!(entry) }
when Symbol
struct.to_s
- when String, Numeric, BSON::ObjectId, Regexp, Boolean, nil
+ when String, Numeric, BSON::ObjectId, Regexp, TrueClass, FalseClass, nil
struct
else
raise "Cannot stringify #{struct.inspect}"

0 comments on commit eb28974

Please sign in to comment.