Module: MongoHashie::Rails

Defined in:
lib/mongo-hashie/rails.rb

Instance Method Summary collapse

Instance Method Details

#all(options = {}) ⇒ Object



5
6
7
# File 'lib/mongo-hashie/rails.rb', line 5

def all(options = {})
  self.new.collection.find.collect {|doc| self.new(doc)}
end

#countObject



23
24
25
# File 'lib/mongo-hashie/rails.rb', line 23

def count
  self.new.collection.count
end

#create(hash = {}) ⇒ Object



3
# File 'lib/mongo-hashie/rails.rb', line 3

def create(hash = {}); self.new(hash).save; end

#destroy_allObject



27
28
29
30
31
# File 'lib/mongo-hashie/rails.rb', line 27

def destroy_all
  target = self.new
  target.collection.remove
  target.reset_keys
end

#find(*args) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/mongo-hashie/rails.rb', line 14

def find(*args)
  if args.first.is_a?(Hash)
    result = self.new.collection.find(args.first)
  else
    result = self.new.collection.find('_id' => Mongo::ObjectID.from_string(args.first))
  end
  result.count == 1 ? result.collect {|doc| self.new(doc)}.first : result.collect {|doc| self.new(doc)}
end

#first(options = {}) ⇒ Object



9
10
11
12
# File 'lib/mongo-hashie/rails.rb', line 9

def first(options = {})
  result = self.new.collection.find_one(options)
  self.new(result) if result
end