Class: Phronomy::StateStore::File
- Defined in:
- lib/phronomy/state_store/file.rb
Overview
This store is suitable for single-process use (development, CLI tools, tests). It is not safe for concurrent access across multiple processes without external locking.
File-system-backed state store. Persists graph state as a JSON file under a configurable directory. No additional server or database migration is required — it works with the local file system out of the box.
Each thread_id is stored as a separate file named "
Instance Method Summary collapse
-
#clear(thread_id) ⇒ self
Removes the saved state file for the given thread_id.
-
#clear_all ⇒ self
Removes all state files managed by this store instance.
-
#directory ⇒ String
The directory used by this store.
-
#initialize(dir: ::File.join(::Dir.tmpdir, "phronomy_states")) ⇒ File
constructor
A new instance of File.
-
#load(thread_id) ⇒ Object?
State instance or nil if not found.
- #save(state) ⇒ self
Constructor Details
#initialize(dir: ::File.join(::Dir.tmpdir, "phronomy_states")) ⇒ File
Returns a new instance of File.
30 31 32 33 |
# File 'lib/phronomy/state_store/file.rb', line 30 def initialize(dir: ::File.join(::Dir.tmpdir, "phronomy_states")) @dir = ::File.(dir) ::FileUtils.mkdir_p(@dir) end |
Instance Method Details
#clear(thread_id) ⇒ self
Removes the saved state file for the given thread_id.
54 55 56 57 58 |
# File 'lib/phronomy/state_store/file.rb', line 54 def clear(thread_id) file = path(thread_id) ::File.delete(file) if ::File.exist?(file) self end |
#clear_all ⇒ self
Removes all state files managed by this store instance.
62 63 64 65 |
# File 'lib/phronomy/state_store/file.rb', line 62 def clear_all ::Dir.glob(::File.join(@dir, "*.json")).each { |f| ::File.delete(f) } self end |
#directory ⇒ String
Returns the directory used by this store.
68 69 70 |
# File 'lib/phronomy/state_store/file.rb', line 68 def directory @dir end |
#load(thread_id) ⇒ Object?
Returns state instance or nil if not found.
44 45 46 47 48 49 |
# File 'lib/phronomy/state_store/file.rb', line 44 def load(thread_id) file = path(thread_id) return nil unless ::File.exist?(file) deserialize_state(::File.read(file)) end |
#save(state) ⇒ self
37 38 39 40 |
# File 'lib/phronomy/state_store/file.rb', line 37 def save(state) ::File.write(path(state.thread_id), serialize_state(state)) self end |