Module: MongoThing::Document::ClassMethods

Defined in:
lib/mongo_thing/document.rb

Instance Method Summary collapse

Instance Method Details

#[](doc_id) ⇒ Object



57
58
59
# File 'lib/mongo_thing/document.rb', line 57

def [](doc_id)
  find_one(doc_id)
end

#collectionObject



43
44
45
# File 'lib/mongo_thing/document.rb', line 43

def collection
  MongoThing.db.collection(self.name)
end

#create(document) ⇒ Object

Creating



75
76
77
# File 'lib/mongo_thing/document.rb', line 75

def create(document)
  self.new(document).save
end

#dbObject

Return the database associated with this class.



39
40
41
# File 'lib/mongo_thing/document.rb', line 39

def db
  collection.db
end

#dropObject



84
85
86
# File 'lib/mongo_thing/document.rb', line 84

def drop
  collection.drop
end

#find(selector = {}, opts = {}) ⇒ Object Also known as: all

Finding Returns an array of Document objects Wonder if it would be desirable to expose the cursor?



50
51
52
53
# File 'lib/mongo_thing/document.rb', line 50

def find(selector={}, opts={})
  mongo_cursor = collection.find(selector, opts)
  Cursor.new(self, mongo_cursor).to_a
end

#find_one(spec_or_object_id = nil, opts = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mongo_thing/document.rb', line 61

def find_one(spec_or_object_id=nil, opts={})
  # Why do it this way?
  # Abstraction is breaking down...
  #
  # We want to just be able to to pass ID strings,
  # but mongo driver requires hash or BSON::ObjectID
  if spec_or_object_id.is_a? String
    spec_or_object_id = BSON::ObjectId.from_string(spec_or_object_id)
  end
  mongo_doc = collection.find_one(spec_or_object_id, opts)
  mongo_doc && self.new(mongo_doc)
end

#remove(selector = {}, opts = {}) ⇒ Object

Deleting



80
81
82
# File 'lib/mongo_thing/document.rb', line 80

def remove(selector={}, opts={})
  collection.remove(selector, opts)
end