Class: Socrates::Adapters::Memory

Inherits:
Object
  • Object
show all
Includes:
Adapter, StubUserDirectory
Defined in:
lib/socrates/adapters/memory.rb

Constant Summary collapse

CLIENT_ID =
"MEMORY"
CHANNEL =
"C1"

Instance Attribute Summary collapse

Attributes included from StubUserDirectory

#default_user

Instance Method Summary collapse

Methods included from StubUserDirectory

#add_user, #lookup_email, #user_from, #users

Methods included from Adapter

#flush_session, #lookup_email, #lookup_user, #queue_direct_message, #queue_message, #user_from, #users

Constructor Details

#initializeMemory

Returns a new instance of Memory.



16
17
18
19
# File 'lib/socrates/adapters/memory.rb', line 16

def initialize
  super()
  @history = Hash.new { |hash, key| hash[key] = [] }
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



14
15
16
# File 'lib/socrates/adapters/memory.rb', line 14

def client_id
  @client_id
end

#historyObject (readonly)

Returns the value of attribute history.



13
14
15
# File 'lib/socrates/adapters/memory.rb', line 13

def history
  @history
end

Instance Method Details

#channel_from(context: nil, user: nil) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
# File 'lib/socrates/adapters/memory.rb', line 27

def channel_from(context: nil, user: nil)
  raise ArgumentError, "Must provide one of :context or :user" if context.nil? && user.nil?

  user.nil? ? CHANNEL : users_channel(user)
end

#client_id_from(context: nil, user: nil) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
# File 'lib/socrates/adapters/memory.rb', line 21

def client_id_from(context: nil, user: nil)
  raise ArgumentError, "Must provide one of :context or :user" if context.nil? && user.nil?

  @client_id || CLIENT_ID
end

#dms(user) ⇒ Object



45
46
47
# File 'lib/socrates/adapters/memory.rb', line 45

def dms(user)
  @history[users_channel(user)]
end

#last_dm(user) ⇒ Object



49
50
51
# File 'lib/socrates/adapters/memory.rb', line 49

def last_dm(user)
  dms(user)[-1]
end

#last_msgObject



41
42
43
# File 'lib/socrates/adapters/memory.rb', line 41

def last_msg
  msgs[-1]
end

#msgsObject

Methods for fetching messages and dms in specs…



37
38
39
# File 'lib/socrates/adapters/memory.rb', line 37

def msgs
  @history[CHANNEL]
end