File Coverage

blib/lib/DBICx/Hooks/Registry.pm
Criterion Covered Total %
statement 35 36 97.2
branch 17 18 94.4
condition 10 11 90.9
subroutine 9 9 100.0
pod 2 2 100.0
total 73 76 96.0


line stmt bran cond sub pod time code
1             package DBICx::Hooks::Registry;
2             BEGIN {
3 2     2   161043 $DBICx::Hooks::Registry::VERSION = '0.003';
4             }
5              
6             # ABSTRACT: Manage the DBICx::Hooks registry of callbacks
7              
8 2     2   19 use strict;
  2         4  
  2         62  
9 2     2   10 use warnings;
  2         4  
  2         51  
10 2     2   9 use Carp 'confess';
  2         9  
  2         102  
11 2     2   10 use Scalar::Util 'blessed';
  2         10  
  2         138  
12 2     2   6077 use parent 'Exporter';
  2         1353  
  2         13  
13              
14             @DBICx::Hooks::Registry::EXPORT = qw( dbic_hooks_register dbic_hooks_for );
15              
16              
17              
18             {
19             my %registry;
20              
21             sub dbic_hooks_register {
22 13     13 1 854449 my ($source, $action, $cb) = @_;
23 13         36 $source = _source_for($source);
24              
25 13 100       681 confess("Missing required first parameter 'source', ")
26             unless $source;
27              
28 12 100       283 confess("Missing required second parameter 'action', ")
29             unless $action;
30 11 100 100     334 confess(
      100        
31             "Action '$action' not supported, only 'create', 'update' or 'delete', ")
32             unless $action eq 'create'
33             or $action eq 'update'
34             or $action eq 'delete';
35              
36 10 100       722 confess("Missing required third parameter 'callback', ")
37             unless $cb;
38 8 100       308 confess("Parameter 'callback' must be a coderef, ")
39             unless ref($cb) eq 'CODE';
40              
41 7   100     48 my $list = $registry{$source}{$action} ||= [];
42 7         15 push @$list, $cb;
43              
44 7         18 return;
45             }
46              
47             sub dbic_hooks_for {
48 14     14 1 2024 my ($source, $action) = @_;
49 14         38 $source = _source_for($source);
50              
51 14         601 my $list = [];
52 14 100 66     127 $list = $registry{$source}{$action}
53             if exists $registry{$source} and exists $registry{$source}{$action};
54              
55 14 100       101 return @$list if wantarray;
56 3         16 return scalar(@$list);
57             }
58              
59             sub _source_for {
60 27     27   44 my $t = $_[0];
61 27 100       160 return $t unless blessed($t);
62 10 50       67 return $t->result_source->result_class if $t->can('result_source');
63 0           confess("Invalid 'source' argument '$t', ");
64             }
65             }
66              
67             1;
68              
69              
70              
71             =pod
72              
73             =head1 NAME
74              
75             DBICx::Hooks::Registry - Manage the DBICx::Hooks registry of callbacks
76              
77             =head1 VERSION
78              
79             version 0.003
80              
81             =head1 SYNOPSIS
82              
83             use DBICx::Hooks::Registry;
84            
85             dbic_hooks_register('My::Schema::Result::MySource', 'create', sub {
86             my ($row) = @_;
87            
88             print "A new row was created, id is ", $row->id, "\n";
89             });
90            
91             dbic_hooks_register('My::Schema::Result::MySource', 'update', sub {
92             my ($row) = @_;
93            
94             print "The row with id is ", $row->id, " was updated\n";
95             });
96              
97             =head1 DESCRIPTION
98              
99             To register a callback with a specific Source/Action pair, you use this
100             registry functions.
101              
102             =head1 FUNCTIONS
103              
104             =head2 dbic_hooks_register
105              
106             dbic_hooks_register('Source', 'Action', sub { my $row = shift; ... });
107             dbic_hooks_register($row_obj, 'Action', sub { my $row = shift; ... });
108             dbic_hooks_register($rs_obj, 'Action', sub { my $row = shift; ... });
109              
110             The C function takes a pair C/C and
111             a callback. The callback will be called after the specified C is
112             performed on C.
113              
114             The following C's are supported: C, C and
115             C.
116              
117             The C action will be called after a new row is created on C.
118              
119             The C action is called when the update() method is called on a
120             L object. Note that if all the fields
121             are updated to the same values as the current ones, no C SQL
122             command is actually sent to the database server, but the callback will
123             be called anyway.
124              
125             The C action is called after the row is deleted.
126              
127             All the callbacks receive a single parameter, the
128             L object that was created or
129             modified.
130              
131             =head2 dbic_hooks_for
132              
133             @list_of_cbs = dbic_hooks_for('Source', 'Action');
134             @list_of_cbs = dbic_hooks_for($row_obj, 'Action');
135             @list_of_cbs = dbic_hooks_for($rs_obj, 'Action');
136              
137             Returns in list context a possibly empty list of callbacks for a pair
138             C/C. In scalar context returns the number of elements
139             in the list.
140              
141             =head1 AUTHOR
142              
143             Pedro Melo
144              
145             =head1 COPYRIGHT AND LICENSE
146              
147             This software is Copyright (c) 2011 by Pedro Melo.
148              
149             This is free software, licensed under:
150              
151             The Artistic License 2.0
152              
153             =cut
154              
155              
156             __END__