Class: Xeno::SessionState

Inherits:
Object
  • Object
show all
Defined in:
lib/xeno/session_state.rb

Overview

The per-session KV store: working state for one conversation, not long-term memory. Values round-trip through JSON (symbols become strings; unserializable values are rejected up front). Reads always hit the database, and writes take the session row lock so read-modify-write merges never lose entries. Cleared by reset.

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ SessionState

Returns a new instance of SessionState.



9
10
11
# File 'lib/xeno/session_state.rb', line 9

def initialize(session)
  @session = session
end

Instance Method Details

#delete(key) ⇒ Object



37
38
39
40
41
42
# File 'lib/xeno/session_state.rb', line 37

def delete(key)
  @session.with_lock do
    @session.update!(state: (@session[:state] || {}).except(key.to_s))
  end
  self
end

#get(key) ⇒ Object Also known as: []



13
14
15
# File 'lib/xeno/session_state.rb', line 13

def get(key)
  current[key.to_s]
end

#set(key, value) ⇒ Object Also known as: []=



32
33
34
# File 'lib/xeno/session_state.rb', line 32

def set(key, value)
  update(key => value)
end

#to_hObject



18
19
20
# File 'lib/xeno/session_state.rb', line 18

def to_h
  current
end

#update(entries) ⇒ Object

Merges the entries in one locked write. Keys stringify; values must be JSON-serializable (Xeno::Error otherwise).



24
25
26
27
28
29
30
# File 'lib/xeno/session_state.rb', line 24

def update(entries)
  normalized = normalize(entries)
  @session.with_lock do
    @session.update!(state: (@session[:state] || {}).merge(normalized))
  end
  self
end