File Coverage

blib/lib/Amon2/Trigger.pm
Criterion Covered Total %
statement 50 50 100.0
branch 7 8 87.5
condition 1 3 33.3
subroutine 11 11 100.0
pod 3 3 100.0
total 72 75 96.0


line stmt bran cond sub pod time code
1             package Amon2::Trigger;
2 21     21   99116 use strict;
  21         48  
  21         587  
3 21     21   102 use warnings;
  21         38  
  21         543  
4 21     21   547 use parent qw/Exporter/;
  21         321  
  21         122  
5 21     21   1149 use Scalar::Util ();
  21         47  
  21         670  
6 21     21   13789 use if $] >= 5.009_005, 'mro';
  21         278  
  21         140  
7 21     21   1535 use if $] < 5.009_005, 'MRO::Compat';
  21         42  
  21         99  
8              
9             our @EXPORT = qw/add_trigger call_trigger get_trigger_code/;
10              
11             sub add_trigger {
12 8     8 1 4450 my ($class, %args) = @_;
13              
14 8 100       21 if (ref $class) {
15 2         9 while (my ($hook, $code) = each %args) {
16 2         3 push @{$class->{_trigger}->{$hook}}, $code;
  2         12  
17             }
18             } else {
19 21     21   1914 no strict 'refs';
  21         46  
  21         4590  
20 6         32 while (my ($hook, $code) = each %args) {
21 7         14 push @{${"${class}::_trigger"}->{$hook}}, $code;
  7         9  
  7         67  
22             }
23             }
24             }
25              
26             sub call_trigger {
27 26     26 1 2410 my ($class, $hook, @args) = @_;
28 26         64 my @code = $class->get_trigger_code($hook);
29 26         74 for my $code (@code) {
30 6         16 $code->($class, @args);
31             }
32             }
33              
34             sub get_trigger_code {
35 48     48 1 100 my ($class, $hook) = @_;
36 48         67 my @code;
37 48 50       170 if (Scalar::Util::blessed($class)) {
38 48 100       94 push @code, @{ $class->{_trigger}->{$hook} || [] };
  48         263  
39 48         108 $class = ref $class;
40             }
41 21     21   198 no strict 'refs';
  21         59  
  21         2709  
42 48   33     179 my $klass = ref $class || $class;
43 48         74 for (@{mro::get_linear_isa($class)}) {
  48         175  
44 173 100       267 push @code, @{${"${_}::_trigger"}->{$hook} || []};
  173         221  
  173         734  
45             }
46 48         143 return @code;
47             }
48              
49             1;
50             __END__
51              
52             =head1 NAME
53              
54             Amon2::Trigger - Trigger system for Amon2
55              
56             =head1 SYNOPSIS
57              
58             package MyClass;
59             use parent qw/Amon2::Trigger/;
60              
61             __PACKAGE__->add_trigger('Foo');
62             __PACKAGE__->call_trigger('Foo');
63              
64             =head1 DESCRIPTION
65              
66             This is a trigger system for Amon2. You can use this class for your class using trigger system.
67              
68             =head1 METHODS
69              
70             =over 4
71              
72             =item C<< __PACKAGE__->add_trigger($name:Str, \&code:CodeRef) >>
73              
74             =item C<< $obj->add_trigger($name:Str, \&code:CodeRef) >>
75              
76             You can register the callback function for the class or object.
77              
78             When you register callback code on object, the callback is only registered to object, not for class.
79              
80             I<Return Value>: Not defined.
81              
82             =item C<< __PACKAGE__->call_trigger($name:Str); >>
83              
84             =item C<< $obj->call_trigger($name:Str); >>
85              
86             This method calls all callback code for $name.
87              
88             I<Return Value>: Not defined.
89              
90             =item C<< __PACKAGE__->get_trigger_code($name:Str) >>
91              
92             =item C<< $obj->get_trigger_code($name:Str) >>
93              
94             You can get all of trigger code from the class and ancestors.
95              
96             =back
97              
98             =head1 FAQ
99              
100             =over 4
101              
102             =item WHY DON'T YOU USE L<Class::Trigger>?
103              
104             L<Class::Trigger> does not support get_trigger_code.
105              
106             =back