strategy

Strategies

A strategy is the object that decides what build, create, attributes-for, and build-stubbed actually do. Each public method on ORM::Factory is a thin wrapper that selects a strategy and delegates to its result method.

The role contract

ORM::Factory::Strategy is a role with three required methods:

methodpurpose
to-sym(--> Str)one of build, create, attributes-for, build-stubbed
result(Evaluator $eval)produces the value the public method returns
association(Str $name, @variants, %opts)cascade an association to the chosen strategy

Every strategy carries a Persistence adapter so it can instantiate, persist, or stub through the protocol rather than poking the class directly.

The built-in strategies

strategyto-symresult returns
BuildStrategybuilda new instance, unsaved
CreateStrategycreatea new instance, persisted via the adapter
AttributesForStrategyattributes-fora Hash of resolved (non-transient, non-assoc) attrs
BuildStubbedStrategybuild-stubbeda stubbed instance (adapter-faked id, no DB)

ORM::Factory.strategy-for($name) returns a fresh instance of the strategy keyed by symbol — handy when wiring per-association overrides (see Associations).

Association cascade

association(...) is the strategy hook that handles cascading. The default implementations forward to the matching public method:

strategycascade target
BuildStrategyORM::Factory.build
CreateStrategyORM::Factory.create
BuildStubbedStrategyORM::Factory.build-stubbed
AttributesForStrategyreturns Nil (associations excluded)

This is why build('post') builds the author, create('post') creates it, and attributes-for('post') simply omits the column.

The cascade is also controlled by the use-parent-strategy configuration flag — when False, associations default to create no matter what the surrounding call was.

Registering custom strategies

The four built-ins are pre-populated in the strategy registry. You can add your own (or replace a built-in) with register-strategy:

ORM::Factory.register-strategy('json', JsonStrategy);
ORM::Factory.json('user');  # dispatched via FALLBACK

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.