Class: Chatbox::MemoryStore

Inherits:
Object
  • Object
show all
Defined in:
lib/chatbox/memory_store.rb

Defined Under Namespace

Classes: Record

Instance Method Summary collapse

Constructor Details

#initialize(id_generator: -> { SecureRandom.uuid }) ⇒ MemoryStore

Returns a new instance of MemoryStore.



5
6
7
# File 'lib/chatbox/memory_store.rb', line 5

def initialize(id_generator: -> { SecureRandom.uuid })
  @id_generator = id_generator
end

Instance Method Details

#add_message(attrs) ⇒ Object



9
10
11
12
# File 'lib/chatbox/memory_store.rb', line 9

def add_message(attrs)
  attrs = attrs.merge id: id_generator.(), read: false
  attrs_list << attrs
end

#find_message(id) ⇒ Object



26
27
28
29
30
# File 'lib/chatbox/memory_store.rb', line 26

def find_message(id)
  if attrs = attrs_list.detect { |attrs| attrs[:id] == id }
    Record.new attrs
  end
end

#find_messages_by_from_id(id) ⇒ Object



36
37
38
# File 'lib/chatbox/memory_store.rb', line 36

def find_messages_by_from_id(id)
  attrs_list.select { |attrs| attrs[:from_id] == id }.map { |attrs| Record.new attrs }
end

#find_messages_by_to_id(id) ⇒ Object



32
33
34
# File 'lib/chatbox/memory_store.rb', line 32

def find_messages_by_to_id(id)
  attrs_list.select { |attrs| attrs[:to_id] == id }.map { |attrs| Record.new attrs }
end

#mark_message_read!(id) ⇒ Object



16
17
18
# File 'lib/chatbox/memory_store.rb', line 16

def mark_message_read!(id)
  attrs_list.detect { |attrs| attrs[:id] == id }.merge! read: true
end

#mark_message_unread!(id) ⇒ Object



20
21
22
# File 'lib/chatbox/memory_store.rb', line 20

def mark_message_unread!(id)
  attrs_list.detect { |attrs| attrs[:id] == id }.merge! read: false
end