Class: Notificon::UserStateStore

Inherits:
Object
  • Object
show all
Includes:
Cache, MongoStore
Defined in:
lib/notificon/user_state_store.rb

Instance Method Summary collapse

Methods included from Cache

#cache_delete, #cache_enabled?, #cache_fetch

Methods included from MongoStore

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

Instance Method Details

#clear_notifications(username) ⇒ Object



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

def clear_notifications(username)
  collection.update({'username' => username}, { '$set' => { 'notifications' => 0 } }, { :upsert => true })
  delete_cached(username)
end

#delete_cached(username) ⇒ Object



28
29
30
# File 'lib/notificon/user_state_store.rb', line 28

def delete_cached(username)
  cache_delete("user_state_#{username}")
end

#get(username) ⇒ Object



12
13
14
15
16
# File 'lib/notificon/user_state_store.rb', line 12

def get(username)
  if item_hash = cache_fetch("user_state_#{username}") { collection.find_one('username' => username) }
    UserState.new(item_hash)
  end
end

#set_notifications(username, notifications) ⇒ Object



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

def set_notifications(username, notifications)
  collection.update({'username' => username}, { '$set' => { 'notifications' => notifications }}, { :upsert => true } )
  delete_cached(username)
end