pub struct BehaviorRegistry { /* private fields */ }Expand description
Behavior 注册中心 — 管理多个 Behavior 的注册与分发
线程安全:内部使用 RwLock,可在多线程环境下共享。
§示例
use sz_orm_model::behaviors::{BehaviorRegistry, TimestampBehavior, BlameableBehavior, Behavior};
use sz_orm_model::hooks::HookContext;
use sz_orm_model::Value;
use std::collections::HashMap;
let mut registry = BehaviorRegistry::new();
registry.register(Box::new(TimestampBehavior::default_fields()));
registry.register(Box::new(BlameableBehavior::default_fields()));
let ctx = HookContext::default().with_operator(100).with_timestamp(1700000000);
let mut attrs = HashMap::new();
registry.before_insert(&ctx, &mut attrs).unwrap();
assert_eq!(attrs.get("created_at"), Some(&Value::I64(1700000000)));
assert_eq!(attrs.get("created_by"), Some(&Value::I64(100)));Implementations§
Source§impl BehaviorRegistry
impl BehaviorRegistry
Sourcepub fn unregister(&self, name: &str) -> bool
pub fn unregister(&self, name: &str) -> bool
按 name 移除已注册的 Behavior
Sourcepub fn before_insert(
&self,
ctx: &HookContext,
attrs: &mut HashMap<String, Value>,
) -> BehaviorResult<()>
pub fn before_insert( &self, ctx: &HookContext, attrs: &mut HashMap<String, Value>, ) -> BehaviorResult<()>
分发 before_insert 事件
Sourcepub fn before_update(
&self,
ctx: &HookContext,
attrs: &mut HashMap<String, Value>,
) -> BehaviorResult<()>
pub fn before_update( &self, ctx: &HookContext, attrs: &mut HashMap<String, Value>, ) -> BehaviorResult<()>
分发 before_update 事件
Sourcepub fn before_delete(
&self,
ctx: &HookContext,
attrs: &mut HashMap<String, Value>,
) -> BehaviorResult<()>
pub fn before_delete( &self, ctx: &HookContext, attrs: &mut HashMap<String, Value>, ) -> BehaviorResult<()>
分发 before_delete 事件
Sourcepub fn after_find(
&self,
ctx: &HookContext,
attrs: &mut HashMap<String, Value>,
) -> BehaviorResult<()>
pub fn after_find( &self, ctx: &HookContext, attrs: &mut HashMap<String, Value>, ) -> BehaviorResult<()>
分发 after_find 事件
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for BehaviorRegistry
impl !RefUnwindSafe for BehaviorRegistry
impl !UnwindSafe for BehaviorRegistry
impl Send for BehaviorRegistry
impl Sync for BehaviorRegistry
impl Unpin for BehaviorRegistry
impl UnsafeUnpin for BehaviorRegistry
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more