Class: Notificon::NotificationStore

Inherits:
Object
  • Object
show all
Includes:
MongoStore
Defined in:
lib/notificon/notification_store.rb

Instance Method Summary collapse

Methods included from MongoStore

#collection, #drop_collection, #ensure_indexes, included, #open_connection, #open_store

Instance Method Details

#add(notification) ⇒ Object



11
12
13
14
15
# File 'lib/notificon/notification_store.rb', line 11

def add(notification)
  collection.insert('username' => notification.username, 'item_url' => notification.item_url, 
    'item_text' => notification.item_text, 'actor' => notification.actor, 'action' => notification.action,
    'occured_at' => notification.occured_at && notification.occured_at.utc, 'item_id' => notification.item_id).to_s
end

#get(id) ⇒ Object



17
18
19
20
21
# File 'lib/notificon/notification_store.rb', line 17

def get(id)
  if item_hash = collection.find_one('_id' => BSON::ObjectId.from_string(id))
    Notification.new(item_hash)
  end
end

#get_for_user(username, limit) ⇒ Object



23
24
25
26
27
# File 'lib/notificon/notification_store.rb', line 23

def get_for_user(username, limit)
  collection.find({'username' => username}, { :limit => limit, :sort => [['occured_at', :descending]]}).collect { |item_hash|
      Notification.new(item_hash)
    }
end

#mark_all_read_for_item(username, item_id, read_at) ⇒ Object



42
43
44
# File 'lib/notificon/notification_store.rb', line 42

def mark_all_read_for_item(username, item_id, read_at)
  collection.update({'username' => username, 'item_id' => item_id, 'read_at' => { '$exists' => false } }, { '$set' => { 'read_at' => read_at.utc}}, { :multi => true})
end

#mark_all_read_for_user(username, read_at) ⇒ Object



38
39
40
# File 'lib/notificon/notification_store.rb', line 38

def mark_all_read_for_user(username, read_at)
  collection.update({'username' => username, 'read_at' => { '$exists' => false } }, { '$set' => { 'read_at' => read_at.utc}}, { :multi => true})
end

#mark_as_read(id, read_at) ⇒ Object



33
34
35
36
# File 'lib/notificon/notification_store.rb', line 33

def mark_as_read(id, read_at)
  collection.update({'_id' => BSON::ObjectId.from_string(id)}, { '$set' => { 'read_at' => read_at.utc } })
  get(id)
end

#unread_count_for_user(username) ⇒ Object



29
30
31
# File 'lib/notificon/notification_store.rb', line 29

def unread_count_for_user(username)
  collection.count(:query => {'username' => username, 'read_at' => { '$exists' => false }})
end