File Coverage

blib/lib/URI/NamespaceMap/ReservedLocalParts.pm
Criterion Covered Total %
statement 17 17 100.0
branch 6 6 100.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 30 30 100.0


line stmt bran cond sub pod time code
1             package URI::NamespaceMap::ReservedLocalParts;
2 7     7   98916 use Moo 1.006000;
  7         11447  
  7         51  
3 7     7   4235 use Types::Standard qw/ArrayRef Str/;
  7         75511  
  7         55  
4 7     7   5827 use List::Util qw/first/;
  7         19  
  7         1781  
5              
6             has allowed => (
7             is => 'ro',
8             isa => ArrayRef [Str],
9             default => sub { [qw/allowed disallowed is_reserved/] }
10             );
11             has disallowed => (is => 'ro', isa => ArrayRef [Str], default => sub { [] });
12              
13             sub is_reserved {
14 60     60 1 4917 my ($self, $keyword) = @_;
15 60 100   177   232 return 0 if first { $_ eq $keyword } @{$self->allowed};
  177         341  
  60         267  
16 57 100   52   243 return 1 if first { $_ eq $keyword } @{$self->disallowed};
  52         146  
  57         167  
17 56 100       615 return $self->can($keyword) ? 1 : 0;
18             }
19              
20              
21             =head1 NAME
22              
23             URI::NamespaceMap::ReservedLocalParts - Permissible local parts for NamespaceMap
24              
25             =head1 VERSION
26              
27             Version 1.09_03
28              
29             =cut
30              
31             our $VERSION = '1.09_03';
32              
33              
34             =head1 SYNOPSIS
35              
36             my $r = URI::NamespaceMap::ReservedLocalParts->new(disallowed => [qw/uri/]);
37              
38             say $r->is_reserved('isa'); # 1
39             say $r->is_reserved('uri'); # 1
40             say $r->is_reserved('foo'); # 0
41              
42             =head1 DESCRIPTION
43              
44             L<URI::NamespaceMap::ReservedLocalParts> is an accompanying distribution to
45             L<URI::NamespaceMap>. It's goal is to check for forbidden names used for local
46             parts.
47              
48             Rather than creating a blacklist that needs to be maintained, it instantiates
49             a new L<Moo> object, and calls C<can> on the invocant. Using this technique, it
50             means that every method on every Perl object (C<isa, can, VERSION>), and Moo
51             objects (C<BUILD, BUILDARGS>) will be automatically black listed.
52              
53             =head1 ATTRIBUTES
54              
55             L<URI::NamespaceMap::ReservedLocalParts> implements the following attributes.
56              
57             =head2 allowed
58              
59             A whitelist of local part names. Defaults to C<allowed>, C<disallowed> and
60             C<is_reserved> so that when C<can> is called on the instance, it doesn't return
61             a false positive for other method names associated with this package.
62              
63             =head2 disallowed
64              
65             A blacklist of local part names. Does not have a default set, but usually
66             defaults to C<uri> when called from L<URI::NamespaceMap>.
67              
68             =head1 METHODS
69              
70             L<URI::NamespaceMap::ReservedLocalParts> implements the following methods.
71              
72             =head2 is_reserved
73              
74             my $r = URI::NamespaceMap::ReservedLocalParts->new(disallowed => [qw/uri/]);
75              
76             say $r->is_reserved('isa'); # 1
77             say $r->is_reserved('uri'); # 1
78             say $r->is_reserved('foo'); # 0
79              
80             Checks if the first argument passed is reserved or not. Returns a C<boolean>.
81              
82             =head1 FURTHER DETAILS
83              
84             See L<URI::NamespaceMap> for further details about authors, license, etc.
85              
86             =cut
87              
88             1;