Class: Ralph::Storage::Context
- Inherits:
-
Object
- Object
- Ralph::Storage::Context
- Defined in:
- lib/ralph/storage/context.rb
Overview
Represents the persistent AI context file for conversational continuity.
Always instantiate with Context.new — it represents the file on disk. Content is read lazily; the file is created on first write/append.
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #append(text) ⇒ Object
- #clear ⇒ Object
- #content ⇒ Object
- #dir ⇒ Object
-
#initialize ⇒ Context
constructor
A new instance of Context.
- #present? ⇒ Boolean
- #write(text) ⇒ Object
Constructor Details
#initialize ⇒ Context
Returns a new instance of Context.
14 15 16 |
# File 'lib/ralph/storage/context.rb', line 14 def initialize FileUtils.mkdir_p(dir) end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
12 13 14 |
# File 'lib/ralph/storage/context.rb', line 12 def path @path end |
Class Method Details
.dir ⇒ Object
18 19 20 |
# File 'lib/ralph/storage/context.rb', line 18 def self.dir @dir ||= File.join(Dir.pwd, ".ralph") end |
Instance Method Details
#append(text) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/ralph/storage/context.rb', line 39 def append(text) if File.exist?(path) write(File.read(path) + text) else write("# Ralph Loop Context\n#{text}") end end |
#clear ⇒ Object
51 52 53 54 55 |
# File 'lib/ralph/storage/context.rb', line 51 def clear if File.exist?(path) File.delete(path) end end |
#content ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/ralph/storage/context.rb', line 28 def content if File.exist?(path) text = File.read(path).strip text.empty? ? nil : text end rescue StandardError nil end |
#dir ⇒ Object
22 |
# File 'lib/ralph/storage/context.rb', line 22 def dir = self.class.dir |
#present? ⇒ Boolean
37 |
# File 'lib/ralph/storage/context.rb', line 37 def present? = !content.nil? |
#write(text) ⇒ Object
47 48 49 |
# File 'lib/ralph/storage/context.rb', line 47 def write(text) File.write(path, text) end |