Class: MongoBrowser::Models::Collection
- Inherits:
-
Object
- Object
- MongoBrowser::Models::Collection
- Defined in:
- lib/mongo_browser/models/collection.rb
Instance Attribute Summary collapse
-
#mongo_collection ⇒ Object
readonly
Returns the value of attribute mongo_collection.
Instance Method Summary collapse
-
#db_name ⇒ Object
Return database name for the current collection.
- #documents_with_pagination(page_number = 1) ⇒ Object
- #drop! ⇒ Object
-
#find(id) ⇒ Object
Finds a document with the given id.
-
#initialize(mongo_collection) ⇒ Collection
constructor
A new instance of Collection.
-
#name ⇒ Object
Return collection name.
-
#remove!(document) ⇒ Object
Removes the given document from the collection.
-
#size ⇒ Object
Return total number of documents.
-
#stats ⇒ Hash
Return stats on the collection.
Constructor Details
#initialize(mongo_collection) ⇒ Collection
Returns a new instance of Collection.
6 7 8 |
# File 'lib/mongo_browser/models/collection.rb', line 6 def initialize(mongo_collection) @mongo_collection = mongo_collection end |
Instance Attribute Details
#mongo_collection ⇒ Object (readonly)
Returns the value of attribute mongo_collection.
4 5 6 |
# File 'lib/mongo_browser/models/collection.rb', line 4 def mongo_collection @mongo_collection end |
Instance Method Details
#db_name ⇒ Object
Return database name for the current collection.
11 12 13 |
# File 'lib/mongo_browser/models/collection.rb', line 11 def db_name mongo_collection.db.name end |
#documents_with_pagination(page_number = 1) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/mongo_browser/models/collection.rb', line 32 def documents_with_pagination(page_number = 1) pager = Pager.new(page_number, size) documents = mongo_collection.find .skip(pager.offset) .limit(pager.per_page) .map { |document| Document.new(db_name, name, document) } OpenStruct.new pager.to_hash.merge(documents: documents) end |
#drop! ⇒ Object
43 44 45 |
# File 'lib/mongo_browser/models/collection.rb', line 43 def drop! mongo_collection.drop end |
#find(id) ⇒ Object
Finds a document with the given id.
50 51 52 53 54 |
# File 'lib/mongo_browser/models/collection.rb', line 50 def find(id) condition = { _id: BSON::ObjectId(id.to_s) } mongo_document = mongo_collection.find(condition).first Document.new(db_name, name, mongo_document) if mongo_document end |
#name ⇒ Object
Return collection name.
16 17 18 |
# File 'lib/mongo_browser/models/collection.rb', line 16 def name mongo_collection.name end |
#remove!(document) ⇒ Object
Removes the given document from the collection.
57 58 59 |
# File 'lib/mongo_browser/models/collection.rb', line 57 def remove!(document) mongo_collection.remove(_id: document.id) end |
#size ⇒ Object
Return total number of documents.
21 22 23 |
# File 'lib/mongo_browser/models/collection.rb', line 21 def size mongo_collection.size end |
#stats ⇒ Hash
Return stats on the collection.
28 29 30 |
# File 'lib/mongo_browser/models/collection.rb', line 28 def stats mongo_collection.stats end |