Class: Chatbox::MemcachedStore
- Inherits:
-
Object
- Object
- Chatbox::MemcachedStore
- Defined in:
- lib/chatbox/memcached_store.rb
Defined Under Namespace
Classes: Record
Instance Method Summary collapse
- #add_message(attrs) ⇒ Object
- #find_message(id) ⇒ Object
- #find_messages_by_from_id(id) ⇒ Object
- #find_messages_by_to_id(id) ⇒ Object
-
#initialize(client) ⇒ MemcachedStore
constructor
A new instance of MemcachedStore.
- #mark_message_read!(id) ⇒ Object
- #mark_message_unread!(id) ⇒ Object
Constructor Details
#initialize(client) ⇒ MemcachedStore
Returns a new instance of MemcachedStore.
5 6 7 |
# File 'lib/chatbox/memcached_store.rb', line 5 def initialize(client) @client = client end |
Instance Method Details
#add_message(attrs) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/chatbox/memcached_store.rb', line 9 def (attrs) client.set "messages/#{attrs['id']}", JSON.generate( 'from_id' => attrs['from_id'], 'to_id' => attrs['to_id'], 'body' => attrs['body'], 'read' => false, ) from_list = JSON.parse(client.get("from/#{attrs['from_id']}") || '[]') from_list << {'from_id' => attrs['from_id'], 'message_id' => attrs['id']} client.set "from/#{attrs['from_id']}", JSON.generate(from_list) to_list = JSON.parse(client.get("to/#{attrs['to_id']}") || '[]') to_list << {'to_id' => attrs['to_id'], 'message_id' => attrs['id']} client.set "to/#{attrs['to_id']}", JSON.generate(to_list) end |
#find_message(id) ⇒ Object
42 43 44 45 46 |
# File 'lib/chatbox/memcached_store.rb', line 42 def (id) if json = client.get("messages/#{id}") Record.new id, JSON.parse(json) end end |
#find_messages_by_from_id(id) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/chatbox/memcached_store.rb', line 58 def (id) if json = client.get("from/#{id}") JSON.parse(json).map do |attrs| attrs['message_id'] end else [] end end |
#find_messages_by_to_id(id) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/chatbox/memcached_store.rb', line 48 def (id) if json = client.get("to/#{id}") JSON.parse(json).map do |attrs| attrs['message_id'] end else [] end end |
#mark_message_read!(id) ⇒ Object
28 29 30 31 32 |
# File 'lib/chatbox/memcached_store.rb', line 28 def (id) attrs = JSON.parse client.get("messages/#{id}") attrs['read'] = true client.set "messages/#{id}", JSON.generate(attrs) end |
#mark_message_unread!(id) ⇒ Object
34 35 36 37 38 |
# File 'lib/chatbox/memcached_store.rb', line 34 def (id) attrs = JSON.parse client.get("messages/#{id}") attrs['read'] = false client.set "messages/#{id}", JSON.generate(attrs) end |