Module: Llmemory::ShortTerm::Stores::KeyCodec

Defined in:
lib/llmemory/short_term/stores/key_codec.rb

Overview

Escapes user/session identifiers so composite keys remain unambiguous when ids contain the separator character :.

Constant Summary collapse

ESCAPE =
"%3A"
SEPARATOR =
":"

Class Method Summary collapse

Class Method Details

.composite_key(*parts) ⇒ Object



22
23
24
# File 'lib/llmemory/short_term/stores/key_codec.rb', line 22

def composite_key(*parts)
  parts.map { |part| encode(part) }.join(SEPARATOR)
end

.decode(value) ⇒ Object



18
19
20
# File 'lib/llmemory/short_term/stores/key_codec.rb', line 18

def decode(value)
  value.to_s.gsub(ESCAPE, SEPARATOR).gsub("%25", "%")
end

.encode(value) ⇒ Object



14
15
16
# File 'lib/llmemory/short_term/stores/key_codec.rb', line 14

def encode(value)
  value.to_s.gsub("%", "%25").gsub(SEPARATOR, ESCAPE)
end

.split_composite_key(key, parts: 2) ⇒ Object



26
27
28
29
# File 'lib/llmemory/short_term/stores/key_codec.rb', line 26

def split_composite_key(key, parts: 2)
  encoded = key.to_s.split(SEPARATOR, parts)
  encoded.map { |part| decode(part) }
end