Class: BotPlatform::Storage::MemoryStorage

Inherits:
Object
  • Object
show all
Includes:
Storageable, Singleton
Defined in:
lib/bot_platform/storage/memory_storage.rb

Instance Attribute Summary collapse

Attributes included from Storageable

#_user_state, #coversation_state, #dialog_state

Instance Method Summary collapse

Methods included from Storageable

#del, #get, #set, #user_state, #user_state=

Constructor Details

#initializeMemoryStorage

Returns a new instance of MemoryStorage.



13
14
15
16
# File 'lib/bot_platform/storage/memory_storage.rb', line 13

def initialize
  @locker = Mutex::new
  @data = {}
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



11
12
13
# File 'lib/bot_platform/storage/memory_storage.rb', line 11

def data
  @data
end

#lockerObject

Returns the value of attribute locker.



11
12
13
# File 'lib/bot_platform/storage/memory_storage.rb', line 11

def locker
  @locker
end

Instance Method Details

#getState(context) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/bot_platform/storage/memory_storage.rb', line 39

def getState(context)
  user_id = context.from[:user_id]
  room_id = context.from[:room_id] || ""
  # FIXME: room_id appear and disappear
  #key = "#{user_id}@#{room_id}"
  key = "#{user_id}".intern
  @data[key] = ConversationState.new(key) if @data[key].nil?
  return @data[key]
end

#read(keys) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bot_platform/storage/memory_storage.rb', line 18

def read(keys)
  new_hash = {}
  @locker.synchronize do
    keys.each do |key|
      if @data.key?(key)
        k = key.to_sym
        new_hash[k] = @data[k]
      end
    end
  end
  return new_hash
end

#write(changes) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/bot_platform/storage/memory_storage.rb', line 31

def write(changes)
  @locker.synchronize do
    changes.each do |change|
      @data[change.key.to_sym] = change.value
    end
  end
end