class Lustra::Model::EventManager

Overview

This class manages the storage and execution of model lifecycle callbacks. It acts as a singleton that stores callbacks for all models and triggers them during appropriate lifecycle events.

Key concepts:

Usage: This is used internally by Lustra::Model::HasHooks Global storage for model lifecycle event management

Defined in:

lustra/model/event_manager.cr

Constant Summary

EVENT_CALLBACKS = {} of EventKey => Array(HookFunction)
INHERITANCE_MAP = {} of String => String

Class Method Summary

Class Method Detail

def self.add_inheritance(parent, child) #

Map the inheritance between models. Events which belongs to parent model are triggered when child model lifecycle actions occurs


[View source]
def self.attach(klass, direction : Symbol, event : Symbol, block : HookFunction) #

Register a callback for a specific model class, direction, and event

klass - The model class direction - :before or :after event - The lifecycle event (:create, :update, :validate, etc.) block - The callback function to execute

This is called internally by Lustra::Model::HasHooks when you define before or after callbacks in your model.


[View source]
def self.trigger(klass, direction : Symbol, event : Symbol, mdl : Lustra::Model) #

Trigger all callbacks for a specific model, direction, and event

klass - the model class direction - :before or :after event - the lifecycle event (:create, :update, :validate, etc.) mdl - the model instance

Callback execution order:

  • :before callbacks: Last defined → First defined (reverse order)
  • :after callbacks: First defined → Last defined (normal order)

This ensures that the most recently defined callbacks run first for :before and last for :after, allowing for proper layering of functionality.


[View source]