class Sepia::InMemoryStorage

Overview

In-memory storage backend for Sepia objects.

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

Data Structure

The storage uses three main hashes:

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

Defined in:

sepia/in_memory_storage.cr

Instance Method Summary

Instance methods inherited from class Sepia::StorageBackend

clear clear, count(object_class : Class) : Int32 count, delete(class_name : String, id : String)
delete(object : Serializable | Container)
delete
, exists?(object_class : Class, id : String) : Bool exists?, export_data : Hash(String, Array(Hash(String, String))) export_data, import_data(data : Hash(String, Array(Hash(String, String)))) import_data, list_all(object_class : Class) : Array(String) list_all, list_all_objects : Hash(String, Array(String)) list_all_objects, load(object_class : Class, id : String, path : String | Nil = nil) : Object load, save(object : Serializable, path : String | Nil = nil)
save(object : Container, path : String | Nil = nil)
save

Instance Method Detail

def 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

[View source]
def count(object_class : Class) : Int32 #
Description copied from class Sepia::StorageBackend

Count objects of a given class


[View source]
def delete(class_name : String, id : String) #
Description copied from class Sepia::StorageBackend

Delete an object by its class name and ID


[View source]
def delete(object : Serializable | Container) #
Description copied from class Sepia::StorageBackend

Delete an object


[View source]
def exists?(object_class : Class, id : String) : Bool #
Description copied from class Sepia::StorageBackend

Check if an object exists


[View source]
def export_data : Hash(String, Array(Hash(String, String))) #
Description copied from class Sepia::StorageBackend

Export all data as a hash structure


[View source]
def get_container_references(container_path : String) : Hash(String, String) #

[View source]
def get_reference(container_path : String, ref_name : String) : String | Nil #

[View source]
def import_data(data : Hash(String, Array(Hash(String, String)))) #
Description copied from class Sepia::StorageBackend

Import data from a hash structure


[View source]
def list_all(object_class : Class) : Array(String) #
Description copied from class Sepia::StorageBackend

List all object IDs of a given class


[View source]
def list_all_objects : Hash(String, Array(String)) #
Description copied from class Sepia::StorageBackend

List all objects, grouped by class name


[View source]
def 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")

[View source]
def 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

[View source]
def 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

[View source]
def store_container_reference(container_path : String, ref_name : String, target_container : Container) #

[View source]
def 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)


[View source]