Class: Mana::FileStore
- Inherits:
-
MemoryStore
- Object
- MemoryStore
- Mana::FileStore
- Defined in:
- lib/mana/memory_store.rb
Overview
Default file-based memory store. Persists memories as JSON files. Storage path resolution: explicit base_path > config.memory_path > cwd/.ruby-mana
Instance Method Summary collapse
-
#clear(namespace) ⇒ Object
Delete the memory file for a namespace.
-
#initialize(base_path = nil) ⇒ FileStore
constructor
Optional base_path overrides default storage location.
-
#read(namespace) ⇒ Object
Read all memories for a namespace from disk.
-
#write(namespace, memories) ⇒ Object
Write all memories for a namespace to disk (overwrites existing file).
Constructor Details
#initialize(base_path = nil) ⇒ FileStore
Optional base_path overrides default storage location
31 32 33 |
# File 'lib/mana/memory_store.rb', line 31 def initialize(base_path = nil) @base_path = base_path end |
Instance Method Details
#clear(namespace) ⇒ Object
Delete the memory file for a namespace
55 56 57 58 |
# File 'lib/mana/memory_store.rb', line 55 def clear(namespace) path = file_path(namespace) File.delete(path) if File.exist?(path) end |
#read(namespace) ⇒ Object
Read all memories for a namespace from disk. Returns [] on missing file or parse error.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mana/memory_store.rb', line 36 def read(namespace) path = file_path(namespace) return [] unless File.exist?(path) data = JSON.parse(File.read(path), symbolize_names: true) data.is_a?(Array) ? data : [] # Corrupted JSON file — return empty array rather than crashing rescue JSON::ParserError [] end |
#write(namespace, memories) ⇒ Object
Write all memories for a namespace to disk (overwrites existing file)
48 49 50 51 52 |
# File 'lib/mana/memory_store.rb', line 48 def write(namespace, memories) path = file_path(namespace) FileUtils.mkdir_p(File.dirname(path)) File.write(path, JSON.pretty_generate(memories)) end |