class

Sepia::InMemoryStorage

Inherits Sepia::StorageBackend < Reference < Object

In-memory storage backend for Sepia objects.

This backend stores all data in memory hashes without any filesystem operations. It's primarily useful for:

  • Testing: Fast operations without disk I/O
  • Demos: Self-contained examples that don't persist data
  • Temporary data: Caching or session storage

Data Structure

The storage uses three main hashes:

  • @serializable_storage: Maps class_name/id to serialized content
  • @container_storage: Maps class_name/id to property data and references
  • @container_references: Maps container paths to reference mappings

Example

# Configure Sepia to use in-memory storage
Sepia::Storage.configure(:memory)

# Objects will be stored in memory only
doc = MyDocument.new("Hello")
doc.save # Stored in @serializable_storage

Instance methods

clear

Clears all data from memory storage.

Empties all internal hashes, effectively resetting the storage to its initial empty state.

storage.clear # All data is now gone
Source
count(object_class : Class) : Int32

Count objects of a given class

Source
delete(class_name : String, id : String)

Delete an object by its class name and ID

Source
delete(object : Serializable | Container)

Delete an object

Source
exists?(object_class : Class, id : String) : Bool

Check if an object exists

Source
export_data

Export all data as a hash structure

Source
get_container_references(container_path : String) : Hash(String, String)
Source
get_reference(container_path : String, ref_name : String) : String | Nil
Source
import_data(data : Hash(String, Array(Hash(String, String))))

Import data from a hash structure

Source
list_all(object_class : Class) : Array(String)

List all object IDs of a given class

Source
list_all_objects

List all objects, grouped by class name

Source
load(object_class : Class, id : String, path : String | Nil = nil) : Object

Loads an object from memory storage.

Retrieves and deserializes an object of the specified class. For Serializable objects, uses the class's from_sepia method. For Container objects, restores primitive properties from JSON and loads references if a path is provided.

Raises an exception if the object is not found.

# Load a Serializable object
doc = storage.load(MyDocument, "doc-uuid")

# Load a Container object
board = storage.load(MyBoard, "board-uuid")
Source
save(object : Container, path : String | Nil = nil)

Saves a Container object to memory storage.

Stores the container's metadata and primitive properties in the @container_storage hash. Primitive properties are serialized to JSON and stored under the "_data" key.

board = Board.new("My Board")
storage = InMemoryStorage.new
storage.save(board) # Stored in @container_storage
Source
save(object : Serializable, path : String | Nil = nil)

Saves a Serializable object to memory storage.

Stores the object's serialized content in the @serializable_storage hash. The key is the full path (including the base path) to maintain compatibility with FileStorage.

doc = MyDocument.new("Hello")
storage = InMemoryStorage.new
storage.save(doc) # Stored in @serializable_storage
Source
store_container_reference(container_path : String, ref_name : String, target_container : Container)
Source
store_reference(container_path : String, ref_name : String, target_class : String, target_id : String)

Methods to support container references (for internal use by Container module)

Source