File Coverage

blib/lib/Role/Commons/Authority.pm
Criterion Covered Total %
statement 42 46 91.3
branch 10 16 62.5
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 63 73 86.3


line stmt bran cond sub pod time code
1 5     5   541 use 5.008;
  5         11  
  5         161  
2 5     5   16 use strict;
  5         8  
  5         122  
3 5     5   17 use warnings;
  5         11  
  5         170  
4              
5             package Role::Commons::Authority;
6              
7 5     5   23 use Carp qw[croak];
  5         5  
  5         288  
8 5     5   2286 use match::simple qw[match];
  5         19380  
  5         35  
9 5     5   856 use Scalar::Util qw[blessed];
  5         7  
  5         224  
10              
11             BEGIN {
12 5     5   18 use Moo::Role;
  5         7  
  5         32  
13 5     5   1686 $Role::Commons::Authority::AUTHORITY = 'cpan:TOBYINK';
14 5         693 $Role::Commons::Authority::VERSION = '0.104';
15             }
16              
17             our %ENABLE_SHARED;
18             our %SHARED_AUTHORITIES;
19              
20             our $setup_for_class = sub {
21             my ($role, $package, %args) = @_;
22            
23             if ( exists $args{-authorities} )
24             {
25             $ENABLE_SHARED{ $package } = 1;
26            
27             ref($args{-authorities}) eq 'ARRAY' and
28             $SHARED_AUTHORITIES{ $package } = $args{-authorities};
29             }
30             };
31              
32             sub AUTHORITY
33             {
34 11     11 1 5103 my ($invocant, $test) = @_;
35 11 50       42 $invocant = ref $invocant if blessed($invocant);
36            
37 11         13 my @authorities = do {
38 5     5   28 no strict 'refs';
  5         5  
  5         1146  
39 11         9 my @a = ${"$invocant\::AUTHORITY"};
  11         39  
40 11 50       30 if (exists $ENABLE_SHARED{ $invocant })
41             {
42 0 0       0 push @a, @{$SHARED_AUTHORITIES{$invocant} || []};
  0         0  
43 0         0 push @a, @{"$invocant\::AUTHORITIES"};
  0         0  
44             }
45 11         19 @a;
46             };
47            
48 11 100       28 if (scalar @_ > 1)
49             {
50 9         8 my $ok = undef;
51 9         14 AUTH: for my $A (@authorities)
52             {
53 9 100       42 if (match($A, $test))
54             {
55 4         18 $ok = $A;
56 4         8 last AUTH;
57             }
58             }
59 9 100       78 return $ok if defined $ok;
60            
61             @authorities
62 5 50       238 ? croak("Invocant ($invocant) has authority '$authorities[0]'")
63             : croak("Invocant ($invocant) has no authority defined");
64             }
65            
66 2 50       15 wantarray ? @authorities : $authorities[0];
67             }
68              
69             1;
70              
71             __END__