Data-ObjectDriver

 view release on metacpan or  search on metacpan

MANIFEST  view on Meta::CPAN

t/09-resultset.t
t/10-resultset-peek.t
t/11-sql.t
t/12-windows.t
t/20-driver-sqlite.t
t/31-cached.t
t/32-partitioned.t
t/33-views.t
t/34-both.t
t/35-multiplexed.t
t/41-callbacks.t
t/42-callbacks-multi-pk.t
t/50-profiling.t
t/60-fork.t
t/61-escape.t
t/lib/Cache/Memory.pm
t/lib/DodTestUtil.pm
t/lib/ErrorTest.pm
t/lib/PkLess.pm
t/lib/Wine.pm
t/lib/both/Ingredient.pm
t/lib/both/Recipe.pm

README.md  view on Meta::CPAN

## Class->add\_trigger($trigger, \\&callback)

Adds a trigger to all objects of class _Class_, such that when the event
_$trigger_ occurs to any of the objects, subroutine `&callback` is run. Note
that triggers will not occur for instances of _subclasses_ of _Class_, only
of _Class_ itself. See TRIGGERS for the available triggers.

## Class->call\_trigger($trigger, \[@callback\_params\])

Invokes the triggers watching class _Class_. The parameters to send to the
callbacks (in addition to _Class_) are specified in _@callback\_params_. See
TRIGGERS for the available triggers.

## $obj->save

Saves the object _$obj_ to the database.

If the object is not yet in the database, _save_ will automatically
generate a primary key and insert the record into the database table.
Otherwise, it will update the existing record.

README.md  view on Meta::CPAN


## $obj->call\_trigger($trigger, \[@callback\_params\])

Invokes the triggers watching all objects of _$obj_'s class and the object
_$obj_ specifically for trigger event _$trigger_. The additional parameters
besides _$obj_, if any, are passed as _@callback\_params_. See TRIGGERS for
the available triggers.

# TRIGGERS

_Data::ObjectDriver_ provides a trigger mechanism by which callbacks can be
called at certain points in the life cycle of an object. These can be set on a
class as a whole or individual objects (see USAGE).

Triggers can be added and called for these events:

- pre\_save -> ($obj, $orig\_obj)

    Callbacks on the _pre\_save_ trigger are called when the object is about to be
    saved to the database. For example, use this callback to translate special code
    strings into numbers for storage in an integer column in the database. Note that this hook is also called when you `remove` the object.

README.md  view on Meta::CPAN

    For example, use this callback to translate the entry in _$terms_ for your
    code string field to its appropriate integer value.

    Modifications to _$terms_ and _$args_ will affect the parameters to
    subsequent triggers and what objects are loaded, but not the original hash
    references used in the _search_ query.

    Note _pre\_search_ should only be used as a trigger on a class, as _search_ is
    never invoked on specific objects.

    >     The return values from your callbacks are ignored.
    >
    >     Note that the invocation of callbacks is the responsibility of the object
    >     driver. If you implement a driver that does not delegate to
    >     _Data::ObjectDriver::Driver::DBI_, it is _your_ responsibility to invoke the
    >     appropriate callbacks with the _call\_trigger_ method.

# PROFILING

For performance tuning, you can turn on query profiling by setting
_$Data::ObjectDriver::PROFILE_ to a true value. Or, alternatively, you can
set the _DOD\_PROFILE_ environment variable to a true value before starting
your application.

To obtain the profile statistics, get the global
_Data::ObjectDriver::Profiler_ instance:

lib/Data/ObjectDriver.pm  view on Meta::CPAN

=head2 Class->add_trigger($trigger, \&callback)

Adds a trigger to all objects of class I<Class>, such that when the event
I<$trigger> occurs to any of the objects, subroutine C<&callback> is run. Note
that triggers will not occur for instances of I<subclasses> of I<Class>, only
of I<Class> itself. See TRIGGERS for the available triggers.

=head2 Class->call_trigger($trigger, [@callback_params])

Invokes the triggers watching class I<Class>. The parameters to send to the
callbacks (in addition to I<Class>) are specified in I<@callback_params>. See
TRIGGERS for the available triggers.

=head2 $obj->save

Saves the object I<$obj> to the database.

If the object is not yet in the database, I<save> will automatically
generate a primary key and insert the record into the database table.
Otherwise, it will update the existing record.

lib/Data/ObjectDriver.pm  view on Meta::CPAN


=head2 $obj->call_trigger($trigger, [@callback_params])

Invokes the triggers watching all objects of I<$obj>'s class and the object
I<$obj> specifically for trigger event I<$trigger>. The additional parameters
besides I<$obj>, if any, are passed as I<@callback_params>. See TRIGGERS for
the available triggers.

=head1 TRIGGERS

I<Data::ObjectDriver> provides a trigger mechanism by which callbacks can be
called at certain points in the life cycle of an object. These can be set on a
class as a whole or individual objects (see USAGE).

Triggers can be added and called for these events:

=over 4

=item * pre_save -> ($obj, $orig_obj)

Callbacks on the I<pre_save> trigger are called when the object is about to be

lib/Data/ObjectDriver.pm  view on Meta::CPAN


Modifications to I<$terms> and I<$args> will affect the parameters to
subsequent triggers and what objects are loaded, but not the original hash
references used in the I<search> query.

Note I<pre_search> should only be used as a trigger on a class, as I<search> is
never invoked on specific objects.

=over

The return values from your callbacks are ignored.

Note that the invocation of callbacks is the responsibility of the object
driver. If you implement a driver that does not delegate to
I<Data::ObjectDriver::Driver::DBI>, it is I<your> responsibility to invoke the
appropriate callbacks with the I<call_trigger> method.

=back

=back

=head1 PROFILING

For performance tuning, you can turn on query profiling by setting
I<$Data::ObjectDriver::PROFILE> to a true value. Or, alternatively, you can
set the I<DOD_PROFILE> environment variable to a true value before starting

t/41-callbacks.t  view on Meta::CPAN

plan tests => 25;

setup_dbs({
    global => [ qw( wines ) ],
});


use Wine;


## can add callbacks
{
    ok(Data::ObjectDriver::BaseObject->can('add_trigger'), 'can add triggers to BaseObject class');
    ok(My::BaseObject->can('add_trigger'), 'can add triggers to directly derived class');
    ok(Wine->can('add_trigger'), 'can add triggers to doubly derived class');
};


sub clear_triggers {
    my ($obj, $when) = @_;
    my $triggers = Class::Trigger::__fetch_triggers($obj);

t/42-callbacks-multi-pk.t  view on Meta::CPAN

# $Id: 41-callbacks.t 1037 2005-11-25 14:51:09Z ykerherve $

use strict;

use lib 't/lib';
use lib 't/lib/partitioned';

use Test::More;
use DodTestUtil;

BEGIN { DodTestUtil->check_driver }



( run in 0.593 second using v1.01-cache-2.11-cpan-9b1e4054eb1 )