File Coverage

blib/lib/MojoX/GlobalEvents.pm
Criterion Covered Total %
statement 41 41 100.0
branch 8 8 100.0
condition 6 6 100.0
subroutine 9 9 100.0
pod 3 3 100.0
total 67 67 100.0


line stmt bran cond sub pod time code
1             package MojoX::GlobalEvents;
2              
3             # ABSTRACT: A module to handle events
4              
5 2     2   96265 use strict;
  2         17  
  2         57  
6 2     2   10 use warnings;
  2         4  
  2         51  
7              
8 2     2   966 use File::Find::Rule;
  2         16043  
  2         14  
9 2     2   108 use File::Spec;
  2         4  
  2         46  
10 2     2   11 use Scalar::Util qw(blessed);
  2         3  
  2         167  
11              
12 2     2   11 use base 'Exporter';
  2         5  
  2         1022  
13             our @EXPORT = qw(on publish);
14              
15             our $VERSION = 0.03;
16              
17             my %subscriber;
18              
19             sub init {
20 1     1 1 83 my ($class, $namespace) = @_;
21              
22 1         5 my @spaces = split /::/, $namespace;
23 1         3 my @dirs = map{ File::Spec->catdir( $_, @spaces ) }@INC;
  10         55  
24 1         30 my @files = File::Find::Rule->file->name( '*.pm' )->in( @dirs );
25              
26 1         2387 for my $file ( @files ) {
27 1         445 require $file;
28             }
29             }
30              
31             sub on {
32 10     10 1 5149 my $object = shift;
33              
34 10         23 my ($event,$sub) = @_;
35              
36 10 100       36 if ( !blessed $object ) {
37 8         14 $sub = $event;
38 8         15 $event = $object;
39 8         13 $object = '';
40             }
41              
42 10 100 100     62 return if ref $event or !ref $sub or ref $sub ne 'CODE';
      100        
43              
44 5         15 my $package = "$object";
45 5 100       28 $package = caller if !$package;
46 5         14 $subscriber{$event}->{$package} = $sub;
47              
48 5         15 return 1;
49             }
50              
51             sub publish {
52 7     7 1 4493 my ($event, @param) = @_;
53              
54 7 100       15 for my $package ( sort keys %{ $subscriber{$event} || {} } ) {
  7         42  
55 9         68 $subscriber{$event}->{$package}->(@param);
56             }
57              
58 7         84 return 1;
59             }
60              
61              
62             1;
63              
64             __END__