DBICx-Hooks

 view release on metacpan or  search on metacpan

lib/DBICx/Hooks.pm  view on Meta::CPAN

    });

=head1 DESCRIPTION

This modules provides a way to hook into the create(), update(), and
delete() calls on your sources.

This can be used to trigger bussiness processes after one of this
operations.

You register callbacks (even multiple callbacks) with a pair
C<Source>/C<Action>. Each callback receives a single parameter, the row
object just created/updated/just deleted.

See L<DBICx::Hooks::Registry> for extra details on the
C<dbic_hooks_registry()> function.

=begin pod_coverage

=head2 insert

lib/DBICx/Hooks/Registry.pm  view on Meta::CPAN

package DBICx::Hooks::Registry;
BEGIN {
  $DBICx::Hooks::Registry::VERSION = '0.003';
}

# ABSTRACT: Manage the DBICx::Hooks registry of callbacks

use strict;
use warnings;
use Carp 'confess';
use Scalar::Util 'blessed';
use parent 'Exporter';

@DBICx::Hooks::Registry::EXPORT = qw( dbic_hooks_register dbic_hooks_for );


lib/DBICx/Hooks/Registry.pm  view on Meta::CPAN

}

1;



=pod

=head1 NAME

DBICx::Hooks::Registry - Manage the DBICx::Hooks registry of callbacks

=head1 VERSION

version 0.003

=head1 SYNOPSIS

    use DBICx::Hooks::Registry;
    
    dbic_hooks_register('My::Schema::Result::MySource', 'create', sub {

lib/DBICx/Hooks/Registry.pm  view on Meta::CPAN

The C<create> action will be called after a new row is created on C<Source>.

The C<update> action is called when the update() method is called on a
L<DBIx::Class::Row|DBIx::Class::Row> object. Note that if all the fields
are updated to the same values as the current ones, no C<UPDATE> SQL
command is actually sent to the database server, but the callback will
be called anyway.

The C<delete> action is called after the row is deleted.

All the callbacks receive a single parameter, the
L<DBIx::Class::Row|DBIx::Class::Row> object that was created or
modified.

=head2 dbic_hooks_for

    @list_of_cbs = dbic_hooks_for('Source', 'Action');
    @list_of_cbs = dbic_hooks_for($row_obj, 'Action');
    @list_of_cbs = dbic_hooks_for($rs_obj,  'Action');

Returns in list context a possibly empty list of callbacks for a pair
C<Source>/C<Action>. In scalar context returns the number of elements
in the list.

=head1 AUTHOR

Pedro Melo <melo@simplicidade.org>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2011 by Pedro Melo.

t/10-registry.t  view on Meta::CPAN

use Test::Deep;
use Test::MockObject;
use DBICx::Hooks::Registry;


subtest 'good usage' => sub {
  my $cb1 = sub { };
  my $cb2 = sub { };

  is(scalar(dbic_hooks_for('Source', 'create')),
    0, 'No callbacks for Source/create');

  is(exception { dbic_hooks_register('Source', 'create', $cb1) },
    undef, 'Added Source/create cb ok');
  is(scalar(dbic_hooks_for('Source', 'create')),
    1, 'We have one callback for Source/create');

  is(exception { dbic_hooks_register('Source', 'create', $cb2) },
    undef, 'Added Source/create cb ok');
  is(scalar(dbic_hooks_for('Source', 'create')),
    2, 'We have two callbacks for Source/create');

  cmp_deeply(
    [dbic_hooks_for('Source', 'create')],
    [$cb1, $cb2],
    'Expected callbacks for Source/create',
  );
};


subtest 'obj for sources' => sub {
  my $row =
    Test::MockObject->new->set_always('result_source' =>
      Test::MockObject->new->set_always('result_class' => 'Row'));
  my $set =
    Test::MockObject->new->set_always('result_source' =>
      Test::MockObject->new->set_always('result_class' => 'Set'));

  dbic_hooks_register($row, 'create', sub { });
  dbic_hooks_register($set, 'update', sub { });

  cmp_deeply(
    [dbic_hooks_for('Row', 'create')],
    [dbic_hooks_for($row,  'create')],
    'Proper callbacks for Row',
  );
  cmp_deeply(
    [dbic_hooks_for('Set', 'update')],
    [dbic_hooks_for($set,  'update')],
    'Proper callbacks for Set',
  );
};


subtest 'bad usage' => sub {
  like(
    exception { dbic_hooks_register() },
    qr/Missing required first parameter 'source',/,
    'Bad boy forgot the source',
  );



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