Class: Xeno::SessionState
- Inherits:
-
Object
- Object
- Xeno::SessionState
- 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
- #delete(key) ⇒ Object
- #get(key) ⇒ Object (also: #[])
-
#initialize(session) ⇒ SessionState
constructor
A new instance of SessionState.
- #set(key, value) ⇒ Object (also: #[]=)
- #to_h ⇒ Object
-
#update(entries) ⇒ Object
Merges the entries in one locked write.
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_h ⇒ Object
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 |