File Coverage

blib/lib/Role/Commons/Authority.pm
Criterion Covered Total %
statement 36 40 90.0
branch 10 16 62.5
condition n/a
subroutine 8 8 100.0
pod 1 1 100.0
total 55 65 84.6


line stmt bran cond sub pod time code
1             package Role::Commons::Authority;
2              
3 6     6   1335 use strict;
  6         11  
  6         209  
4 6     6   35 use warnings;
  6         10  
  6         184  
5 6     6   31 use Moo::Role;
  6         11  
  6         64  
6 6     6   2691 use Carp qw[croak];
  6         14  
  6         449  
7 6     6   7417 use Scalar::Does qw[ does blessed CODE ARRAY HASH REGEXP STRING ];
  6         840057  
  6         99  
8              
9             BEGIN {
10 6     6   7335 $Role::Commons::Authority::AUTHORITY = 'cpan:TOBYINK';
11 6         2161 $Role::Commons::Authority::VERSION = '0.102';
12             }
13              
14             our %ENABLE_SHARED;
15             our %SHARED_AUTHORITIES;
16              
17             our $setup_for_class = sub {
18             my ($role, $package, %args) = @_;
19            
20             if ( exists $args{-authorities} )
21             {
22             $ENABLE_SHARED{ $package } = 1;
23            
24             does($args{-authorities}, ARRAY) and
25             $SHARED_AUTHORITIES{ $package } = $args{-authorities};
26             }
27             };
28              
29             our $_smart_match;
30             $_smart_match = sub
31             {
32             my ($A, $B) = @_;
33            
34             if (not defined $B)
35             { return not defined $A }
36            
37             if (does $B, CODE)
38             { return $B->($A) }
39            
40             if (does $B, ARRAY)
41             { return scalar grep { $_smart_match->($A, $_) } @$B }
42            
43             if (does $B, HASH)
44             { return defined $A && exists $B->{$A} }
45            
46             if (does $B, REGEXP)
47             { return $A =~ $B }
48            
49             if (does $B, STRING)
50             { return $A eq $B }
51            
52             return;
53             };
54              
55             sub AUTHORITY
56             {
57 4     4 1 1833 my ($invocant, $test) = @_;
58 4 50       25 $invocant = ref $invocant if blessed($invocant);
59            
60 4         8 my @authorities = do {
61 6     6   60 no strict 'refs';
  6         16  
  6         1530  
62 4         8 my @a = ${"$invocant\::AUTHORITY"};
  4         20  
63 4 50       19 if (exists $ENABLE_SHARED{ $invocant })
64             {
65 0 0       0 push @a, @{$SHARED_AUTHORITIES{$invocant} || []};
  0         0  
66 0         0 push @a, @{"$invocant\::AUTHORITIES"};
  0         0  
67             }
68 4         12 @a;
69             };
70            
71 4 100       16 if (scalar @_ > 1)
72             {
73 2         5 my $ok = undef;
74 2         6 AUTH: for my $A (@authorities)
75             {
76 2 100       8 if ($_smart_match->($A, $test))
77             {
78 1         3 $ok = $A;
79 1         4 last AUTH;
80             }
81             }
82 2 100       13 return $ok if defined $ok;
83            
84             @authorities
85 1 50       202 ? croak("Invocant ($invocant) has authority '$authorities[0]'")
86             : croak("Invocant ($invocant) has no authority defined");
87             }
88            
89 2 50       19 wantarray ? @authorities : $authorities[0];
90             }
91              
92             1;
93              
94             __END__