This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract class connection and finder methods to separate modules
- Loading branch information
Showing
3 changed files
with
78 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class Mingo | ||
module Connection | ||
attr_writer :db, :collection | ||
|
||
def db | ||
@db || superclass.db | ||
end | ||
|
||
def connect(dbname_or_uri) | ||
self.collection = nil | ||
self.db = if dbname_or_uri.index('mongodb://') == 0 | ||
connection = Mongo::Connection.from_uri(dbname_or_uri) | ||
connection.db(connection.auths.last['db_name']) | ||
else | ||
Mongo::Connection.new.db(dbname_or_uri) | ||
end | ||
end | ||
|
||
def collection_name | ||
self.name | ||
end | ||
|
||
def collection | ||
@collection ||= db.collection(collection_name).tap { |col| | ||
col.extend Cursor::CollectionPlugin | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class Mingo | ||
module Finders | ||
def first(id_or_selector = nil, options = {}) | ||
unless id_or_selector.nil? or Hash === id_or_selector | ||
id_or_selector = BSON::ObjectId[id_or_selector] | ||
end | ||
options = { :transformer => lambda {|doc| self.new(doc)} }.update(options) | ||
collection.find_one(id_or_selector, options) | ||
end | ||
|
||
def find(selector = {}, options = {}, &block) | ||
options = { :transformer => lambda {|doc| self.new(doc)} }.update(options) | ||
collection.find(selector, options, &block) | ||
end | ||
|
||
def find_by_ids(object_ids, query = {}, options = {}) | ||
find({:_id => {'$in' => object_ids}}.update(query), options) | ||
end | ||
|
||
def find_ordered_ids(object_ids, query = {}, options = {}) | ||
indexed = find_by_ids(object_ids, query, options).inject({}) do |hash, object| | ||
hash[object.id] = object | ||
hash | ||
end | ||
|
||
object_ids.map { |id| indexed[id] } | ||
end | ||
|
||
def paginate_ids(object_ids, paginate_options, options = {}) | ||
object_ids.paginate(paginate_options).tap do |collection| | ||
collection.replace find_ordered_ids(collection, {}, options) | ||
end | ||
end | ||
end | ||
end |