-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_note_mongo.rb
46 lines (38 loc) · 1018 Bytes
/
web_note_mongo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require 'rubygems'
require 'mongo'
module WebNoteMongo
DB_NAME = 'notes'
#COLLECTION_NAME = 'note'
class << self
def connect()
@db = Mongo::Connection.new('localhost',27017).db(DB_NAME)
@collection = @db.collection(DB_NAME)#COLLECTION_NAME)
end
def find_by_tag(tags)
if tags.size == 1
tag_hash = {'tags'=>tags[0]}
else
tag_hash = {
'$and'=> tags.collect{ |t| {'tags'=>t} }
}
end
@collection.find(tag_hash).sort(["_id",Mongo::DESCENDING])
end
def find_by_id(oid)
@collection.find_one( str_to_obj_id(oid) )
end
def save(note)
note['_id'] = str_to_obj_id(note['_id']) if note['_id']
@collection.save(note)
end
def delete(oid)
@collection.remove({'_id'=>str_to_obj_id(oid)})
end
def check_pin(pin)
@db.collection('PIN').remove({'pin'=>pin.strip}, {:safe=>true})["n"] > 0
end
def str_to_obj_id(oid)
BSON::ObjectId.from_string(oid)
end
end
end