Callbacks
Callbacks hook into the build lifecycle so a factory can mutate its instance
or trigger side-effects at well-defined points. The built-in events are
after build, before create, after create, and after stub; custom
names can be invoked by hand from other callbacks or custom build
strategies.
1 2 3 4 5 6 7 8 | |
Built-in events
| Event | Fired during | Phase |
|---|---|---|
after build |
build, create |
after |
before create |
create (before persistence) |
before |
after create |
create (after persistence) |
after |
after stub |
build-stubbed |
after |
attributes-for runs no callbacks — it returns the attribute hash without
ever instantiating the model.
1 2 3 4 5 6 7 8 9 | |
Callback signature
The block can take zero, one, or two arguments:
- 0 — for fire-and-forget side effects
- 1 — receives the instance under construction
- 2 — receives the instance and the evaluator, so the callback can read any persisted or transient attribute (including overrides)
1 2 3 | |
Multiple callbacks for one event
A factory may declare several callbacks for the same event. They fire in declaration order:
1 2 3 4 5 6 | |
Inheritance
Parent callbacks run before child callbacks. Each factory contributes its own callbacks at its position in the chain:
1 2 3 4 5 6 7 8 9 10 11 12 | |
Variants
A variant can register its own callbacks. When the variant is applied, its callbacks are appended after the factory's own:
1 2 3 4 5 6 7 8 9 10 11 12 | |
Global callbacks
A before / after at the top of a define block — without a
surrounding factory — applies to every factory. Global callbacks fire
before any factory-specific callbacks for the same event:
1 2 3 4 5 6 7 8 9 10 | |
ORM::Factory.global-callbacks returns the list of registered globals.
ORM::Factory.reload clears them along with everything else.
Custom callbacks
callback 'name' registers a callback under an arbitrary event name. Custom
callbacks do not fire on any built-in event — they only fire when something
asks for them explicitly via evaluator.run-callbacks('name'). The natural
caller is a custom build strategy, but built-in callbacks can
also chain to a custom name:
1 2 3 4 5 6 7 8 9 | |
has_many-style collections
A transient attribute plus an after build callback gives
a count knob and a side-effect that builds the children:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
The transient comments-count is excluded from attributes-for and never
reaches the model constructor — only the callback sees it via the evaluator.
Reset between tests
ORM::Factory.reload clears every factory, alias, sequence, global variant,
and global callback. Per-test reset is identical to the rest of the
library:
1 2 3 4 | |