Class: RiveScript::MemorySessionManager

Inherits:
SessionManager show all
Defined in:
lib/rivescript/sessions.rb

Overview

Default in-memory session store for RiveScript.

Instance Method Summary collapse

Methods inherited from SessionManager

#default_session

Constructor Details

#initializeMemorySessionManager

Returns a new instance of MemorySessionManager.



71
72
73
74
75
76
# File 'lib/rivescript/sessions.rb', line 71

def initialize
  super
  @users = {}
  @frozen = {}
  @mutex = Mutex.new
end

Instance Method Details

#freeze(username) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/rivescript/sessions.rb', line 136

def freeze(username)
  @mutex.synchronize do
    if @users[username].nil?
      raise "freeze(#{username}): user not found"
    end

    @frozen[username] = Utils.clone(@users[username])
    nil
  end
end

#get(username, key) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rivescript/sessions.rb', line 96

def get(username, key)
  @mutex.synchronize do
    return nil if @users[username].nil?

    if @users[username].key?(key)
      @users[username][key]
    else
      "undefined"
    end
  end
end

#get_allObject



116
117
118
# File 'lib/rivescript/sessions.rb', line 116

def get_all
  @mutex.synchronize { Utils.clone(@users) }
end

#get_any(username) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/rivescript/sessions.rb', line 108

def get_any(username)
  @mutex.synchronize do
    return nil if @users[username].nil?

    Utils.clone(@users[username])
  end
end

#init(username) ⇒ Object



78
79
80
# File 'lib/rivescript/sessions.rb', line 78

def init(username)
  @users[username] = default_session if @users[username].nil?
end

#reset(username) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/rivescript/sessions.rb', line 120

def reset(username)
  @mutex.synchronize do
    @users.delete(username)
    @frozen.delete(username)
    nil
  end
end

#reset_allObject



128
129
130
131
132
133
134
# File 'lib/rivescript/sessions.rb', line 128

def reset_all
  @mutex.synchronize do
    @users = {}
    @frozen = {}
    nil
  end
end

#set(username, data) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rivescript/sessions.rb', line 82

def set(username, data)
  @mutex.synchronize do
    init(username)
    data.each do |key, value|
      if value.nil?
        @users[username].delete(key)
      else
        @users[username][key] = value
      end
    end
    nil
  end
end

#thaw(username, action = "thaw") ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rivescript/sessions.rb', line 147

def thaw(username, action = "thaw")
  @mutex.synchronize do
    if @frozen[username].nil?
      raise "thaw(#{username}): no frozen variables found"
    end

    case action
    when "thaw"
      @users[username] = Utils.clone(@frozen[username])
      @frozen.delete(username)
    when "discard"
      @frozen.delete(username)
    when "keep"
      @users[username] = Utils.clone(@frozen[username])
    else
      raise "bad thaw action"
    end

    nil
  end
end