Class: Kongo::Collection
- Inherits:
-
Object
- Object
- Kongo::Collection
- Defined in:
- lib/kongo.rb
Class Method Summary collapse
-
.add_extension(collection_name, mod) ⇒ Object
Declare extensions using this method:.
-
.fetch_collections_using(&block) ⇒ Object
This method must be called statically on the ORM once before being used, so that the ORM knows how to connect to mongo and fetch collections.
Instance Method Summary collapse
-
#coll ⇒ Object
This method returns the Mongo::Collection object for this collection.
-
#count(*args) ⇒ Object
Count, just forwards to driver.
-
#extend(const) ⇒ Object
This method just adds the extension to the list of extension, for the sake of inspect, and call super:.
-
#find(*args) ⇒ Object
(also: #find_many)
‘find`, aka.
-
#find_by_id(id) ⇒ Object
If passed a legal BSON ObjectId string, will convert to BSON::ObjectId.
-
#find_one(*args) ⇒ Object
When passed a native BSON::ObjectId, it will act as a find_by_id, otherwise, expects a regular query hash.
-
#has?(id) ⇒ Boolean
Verify existence of record by id.
-
#initialize(name) ⇒ Collection
constructor
Initialize with a collection name (as symbol), will use that collection name to generate the connection lazily when it is first used, thru the collection callback (must be defined).
-
#insert!(hash) ⇒ Object
Insert a record, returns a Model object.
-
#inspect ⇒ Object
Inspecting a Mongo Model attempts to only show useful information, such as what its extensions are as well as certain ivars.
Constructor Details
#initialize(name) ⇒ Collection
Initialize with a collection name (as symbol), will use that collection name to generate the connection lazily when it is first used, thru the collection callback (must be defined).
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/kongo.rb', line 13 def initialize(name) @coll_name = name @visible_ivars = [] @@extensions ||= {} if @@extensions[name.to_sym] @@extensions[name.to_sym].each do |const| extend(const) end end end |
Class Method Details
.add_extension(collection_name, mod) ⇒ Object
Declare extensions using this method:
Kongo::Collection.add_extension(:collection_name, module)
134 135 136 |
# File 'lib/kongo.rb', line 134 def self.add_extension(collection_name, mod) ((@@extensions ||= {})[collection_name.to_sym] ||= []) << mod end |
.fetch_collections_using(&block) ⇒ Object
This method must be called statically on the ORM once before being used, so that the ORM knows how to connect to mongo and fetch collections.
A simple usage example might look like this:
class MongoCollectionFetcher
def self.fetch(name)
unless @mongo
@mongo = Mongo::Connection.new
end
return @mongo['database'][name.to_s]
end
end
Kongo::Collection.fetch_collections_using do |n|
MongoCollectionFetcher.fetch(n)
end
97 98 99 |
# File 'lib/kongo.rb', line 97 def self.fetch_collections_using(&block) @@collection_fetcher = block end |
Instance Method Details
#coll ⇒ Object
This method returns the Mongo::Collection object for this collection.
104 105 106 107 108 109 110 111 |
# File 'lib/kongo.rb', line 104 def coll return @coll if @coll @coll ||= begin raise 'Kongo has not been initialized with a collection fetcher.' unless @@collection_fetcher @@collection_fetcher.call(@coll_name) end end |
#count(*args) ⇒ Object
Count, just forwards to driver.
52 53 54 |
# File 'lib/kongo.rb', line 52 def count(*args) coll.count(*args) end |
#extend(const) ⇒ Object
This method just adds the extension to the list of extension, for the sake of inspect, and call super:
74 75 76 |
# File 'lib/kongo.rb', line 74 def extend(arg) super(arg.is_a?(Symbol) ? Extensions::Collections.const_get(arg) : arg) end |
#find(*args) ⇒ Object Also known as: find_many
‘find`, aka. `find_many` returns a Kongo::Cursor wrapping the Mongo cursor.
44 45 46 47 |
# File 'lib/kongo.rb', line 44 def find(*args) (c = coll.find(*args)).is_a?(::Mongo::Cursor) ? Cursor.new(c, coll) : c end |
#find_by_id(id) ⇒ Object
If passed a legal BSON ObjectId string, will convert to BSON::ObjectId. Otherwise finds by value.
36 37 38 39 |
# File 'lib/kongo.rb', line 36 def find_by_id(id) id = BSON::ObjectId(id) if id.is_a?(String) && BSON::ObjectId.legal?(id) find_one(_id: id) end |
#find_one(*args) ⇒ Object
When passed a native BSON::ObjectId, it will act as a find_by_id, otherwise, expects a regular query hash. Will return a Kongo::Model.
28 29 30 31 |
# File 'lib/kongo.rb', line 28 def find_one(*args) (r = coll.find_one(*args)) ? Model.new(r, coll) : r end |
#has?(id) ⇒ Boolean
Verify existence of record by id.
58 59 60 |
# File 'lib/kongo.rb', line 58 def has?(id) count(:query => {_id: id}) == 1 end |
#insert!(hash) ⇒ Object
Insert a record, returns a Model object.
64 65 66 67 68 |
# File 'lib/kongo.rb', line 64 def insert!(hash) coll.insert(hash) hash['_id'] = hash.delete(:_id) if hash[:_id] Model.new(hash, coll) end |
#inspect ⇒ Object
Inspecting a Mongo Model attempts to only show useful information, such as what its extensions are as well as certain ivars.
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/kongo.rb', line 118 def inspect proxy = Object.new @visible_ivars.each do |ivar| val = instance_variable_get(ivar) proxy.instance_variable_set(ivar, val) unless val.nil? end string = proxy.inspect ext_info = @extensions ? '(+ '+@extensions.join(', ')+')' : '' string.gsub(/Object:0x[0-9a-f]+/, "Kongo::Collection#{ext_info}:0x#{object_id}") end |