@@ -9,7 +9,8 @@ class Collection
99 :name ,
1010 :pk_factory ,
1111 :hint ,
12- :write_concern
12+ :write_concern ,
13+ :capped
1314
1415 # Read Preference
1516 attr_accessor :read ,
@@ -20,20 +21,20 @@ class Collection
2021 #
2122 # @param [String, Symbol] name the name of the collection.
2223 # @param [DB] db a MongoDB database instance.
23- #
24+ #
2425 # @option opts [String, Integer, Symbol] :w (1) Set default number of nodes to which a write
2526 # should be acknowledged
2627 # @option opts [Boolean] :j (false) Set journal acknowledgement
2728 # @option opts [Integer] :wtimeout (nil) Set replica set acknowledgement timeout
2829 # @option opts [Boolean] :fsync (false) Set fsync acknowledgement.
29- #
30+ #
3031 # Notes about write concern:
31- # These write concern options will be used for insert, update, and remove methods called on this
32- # Collection instance. If no value is provided, the default values set on this instance's DB will be used.
32+ # These write concern options will be used for insert, update, and remove methods called on this
33+ # Collection instance. If no value is provided, the default values set on this instance's DB will be used.
3334 # These option values can be overridden for any invocation of insert, update, or remove.
3435 #
3536 # @option opts [:create_pk] :pk (BSON::ObjectId) A primary key factory to use
36- # other than the default BSON::ObjectId.
37+ # other than the default BSON::ObjectId.
3738 # @option opts [:primary, :secondary] :read The default read preference for queries
3839 # initiates from this connection object. If +:secondary+ is chosen, reads will be sent
3940 # to one of the closest available secondary nodes. If a secondary node cannot be located, the
@@ -57,30 +58,26 @@ def initialize(name, db, opts={})
5758 db , name = name , db
5859 end
5960
60- case name
61- when Symbol , String
62- else
63- raise TypeError , "new_name must be a string or symbol"
64- end
65-
61+ raise TypeError ,
62+ "Collection name must be a String or Symbol." unless [ String , Symbol ] . include? ( name . class )
6663 name = name . to_s
6764
68- if name . empty? or name . include? ".."
69- raise Mongo ::InvalidNSName , "collection names cannot be empty"
70- end
71- if name . include? "$"
72- raise Mongo ::InvalidNSName , "collection names must not contain '$'" unless name =~ /((^\$ cmd)|(oplog\. \$ main))/
73- end
74- if name . match ( /^\. / ) or name . match ( /\. $/ )
75- raise Mongo ::InvalidNSName , "collection names must not start or end with '.'"
65+ raise Mongo ::InvalidNSName ,
66+ "Collection names cannot be empty." if name . empty? || name . include? ( ".." )
67+
68+ if name . include? ( "$" )
69+ raise Mongo ::InvalidNSName ,
70+ "Collection names must not contain '$'" unless name =~ /((^\$ cmd)|(oplog\. \$ main))/
7671 end
7772
73+ raise Mongo ::InvalidNSName ,
74+ "Collection names must not start or end with '.'" if name . match ( /^\. / ) || name . match ( /\. $/ )
75+
76+ pk_factory = nil
7877 if opts . respond_to? ( :create_pk ) || !opts . is_a? ( Hash )
7978 warn "The method for specifying a primary key factory on a Collection has changed.\n " +
80- "Please specify it as an option (e.g., :pk => PkFactory)."
79+ "Please specify it as an option (e.g., :pk => PkFactory)."
8180 pk_factory = opts
82- else
83- pk_factory = nil
8481 end
8582
8683 @db , @name = db , name
@@ -92,6 +89,7 @@ def initialize(name, db, opts={})
9289 @write_concern = get_write_concern ( opts , db )
9390 @read = opts [ :read ] || @db . read
9491 Mongo ::ReadPreference ::validate ( @read )
92+ @capped = opts [ :capped ]
9593 @tag_sets = opts . fetch ( :tag_sets , @db . tag_sets )
9694 @acceptable_latency = opts . fetch ( :acceptable_latency , @db . acceptable_latency )
9795 end
@@ -106,7 +104,7 @@ def initialize(name, db, opts={})
106104 #
107105 # @return [Boolean]
108106 def capped?
109- [ 1 , true ] . include? @db . command ( { :collstats => @name } ) [ 'capped' ]
107+ @capped ||= [ 1 , true ] . include? ( @db . command ( { :collstats => @name } ) [ 'capped' ] )
110108 end
111109
112110 # Return a sub-collection of this collection by name. If 'users' is a collection, then
@@ -361,13 +359,13 @@ def save(doc, opts={})
361359 # triggers a database assertion (as in a duplicate insert, for instance).
362360 # If not acknowledging writes, the list of ids returned will
363361 # include the object ids of all documents attempted on insert, even
364- # if some are rejected on error. When acknowledging writes, any error will raise an
362+ # if some are rejected on error. When acknowledging writes, any error will raise an
365363 # OperationFailure exception.
366364 # MongoDB v2.0+.
367365 # @option opts [Boolean] :collect_on_error (+false+) if true, then
368366 # collects invalid documents as an array. Note that this option changes the result format.
369367 #
370- # @raise [Mongo::OperationFailure] will be raised iff :w > 0 and the operation fails.
368+ # @raise [Mongo::OperationFailure] will be raised iff :w > 0 and the operation fails.
371369 #
372370 # @core insert insert-instance_method
373371 def insert ( doc_or_docs , opts = { } )
@@ -392,7 +390,7 @@ def insert(doc_or_docs, opts={})
392390 #
393391 # Notes on write concern:
394392 # Options provided here will override any write concern options set on this collection,
395- # its database object, or the current connection. See the options for +DB#get_last_error+.
393+ # its database object, or the current connection. See the options for +DB#get_last_error+.
396394 #
397395 # @example remove all documents from the 'users' collection:
398396 # users.remove
@@ -446,7 +444,7 @@ def remove(selector={}, opts={})
446444 #
447445 # Notes on write concern:
448446 # Options provided here will override any write concern options set on this collection,
449- # its database object, or the current connection. See the options for DB#get_last_error.
447+ # its database object, or the current connection. See the options for DB#get_last_error.
450448 #
451449 # @return [Hash, true] Returns a Hash containing the last error object if acknowledging writes.
452450 # Otherwise, returns true.
@@ -631,7 +629,7 @@ def find_and_modify(opts={})
631629 #
632630 # @param [Array] pipeline Should be a single array of pipeline operator hashes.
633631 #
634- # '$project' Reshapes a document stream by including fields, excluding fields, inserting computed fields,
632+ # '$project' Reshapes a document stream by including fields, excluding fields, inserting computed fields,
635633 # renaming fields,or creating/populating fields that hold sub-documents.
636634 #
637635 # '$match' Query-like interface for filtering documents out of the aggregation pipeline.
0 commit comments