Class: Mistri::Stores::JSONL

Inherits:
Object
  • Object
show all
Defined in:
lib/mistri/stores/jsonl.rb

Overview

One JSONL file per session under a directory: durable sessions without a database, and a transcript any tool can read line by line.

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ JSONL

Returns a new instance of JSONL.



11
12
13
14
# File 'lib/mistri/stores/jsonl.rb', line 11

def initialize(dir)
  @dir = dir
  FileUtils.mkdir_p(dir)
end

Instance Method Details

#append(id, entry) ⇒ Object



16
17
18
19
# File 'lib/mistri/stores/jsonl.rb', line 16

def append(id, entry)
  File.open(path(id), "a") { |file| file.puts(JSON.generate(entry)) }
  nil
end

#load(id) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/mistri/stores/jsonl.rb', line 21

def load(id)
  return [] unless File.exist?(path(id))

  File.readlines(path(id)).filter_map do |line|
    JSON.parse(line) unless line.strip.empty?
  end
end