Class: MongoBrowser::Models::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo_browser/models/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_collectionObject (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_nameObject

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.

Returns:

  • MongoBrowser::Models::Document



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

#nameObject

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

#sizeObject

Return total number of documents.



21
22
23
# File 'lib/mongo_browser/models/collection.rb', line 21

def size
  mongo_collection.size
end

#statsHash

Return stats on the collection.

Returns:

  • (Hash)


28
29
30
# File 'lib/mongo_browser/models/collection.rb', line 28

def stats
  mongo_collection.stats
end