Persistence adapters
ORM::Factory is ORM-agnostic at its core. The build strategies (build,
create, attributes-for, build-stubbed) target a small protocol —
ORM::Factory::Persistence — never a specific ORM directly.
The protocol
1 2 3 4 5 6 7 8 | |
instantiatebuilds an unsaved instance from a class and an attribute hash.buildcalls this and stops here.persistsaves the instance and returns it (raising if the ORM rejects it).createcallsinstantiatethenpersist.is-valid/errorsexpose validation state without committing.primary-keyis the name of the model's primary key column.stubreturns a frozen, no-DB-access stand-in.build-stubbedruns through this.
attributes-for deliberately bypasses the adapter entirely — it returns a
plain attribute hash without ever instantiating, persisting, or stubbing.
The generic default
ORM::Factory::Persistence::Generic is the fallback adapter used when no
ORM-specific adapter is registered. It:
- instantiates via
$class.new(|%attrs); - persists by calling
.save-bangif present, else.save, else raisingX::ORM::Factory::Persistence::NoPersistencewith a clear hint; - delegates
is-valid,errors,primary-keyto identically named methods on the instance / class if they exist, and falls back to sane defaults otherwise.
1 2 3 4 5 | |
Detection and selection
On first access of ORM::Factory.persistence, the library:
- checks for an explicit adapter set via
ORM::Factory.set-persistence; - tries to
require ORM::Factory::Persistence::ActiveRecord— if it loads, uses that adapter (this is how theORM::ActiveRecordadapter auto-registers, with nousein your specs); - falls back to
ORM::Factory::Persistence::Generic.
Once resolved, the adapter is cached for the rest of the process. Use
ORM::Factory.reset-persistence in tests to force re-detection.
1 2 3 4 5 6 7 8 | |
Writing a custom adapter
Implement the ORM::Factory::Persistence role and call set-persistence:
1 2 3 4 5 6 7 8 9 10 11 12 | |
Per-factory and global to-create / initialize-with / skip-create hooks
sit on top of this seam: they intercept before the adapter is consulted, and
fall through to the adapter when not present. See
Construction for the full hook protocol and resolution
order.
For the bundled ORM::ActiveRecord adapter, see
ORM::ActiveRecord adapter.