| 1 | package Articulate::LocationSpecification; | ||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use overload '""' => \&to_file_path, '@{}' => sub{ shift->path }; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use Articulate::Location; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use Exporter::Declare; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 10 | default_exports qw(locspec); | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | Articulate::LocationSpecification - represent a specification | ||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | locspec ['zone', '*', 'article', 'hello-world'] | ||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | locspec 'zone/*/article/hello-world' # same thing | ||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | An object class which represents a specification - like a 'pattern' or 'glob', and provides methods so that it can be compared with locations. It is similar to C<Articulate::Location>, and stringifies to the 'file path' representation. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | The main use of this is to determine whether a user has access to a resource based on rules. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | =head3 locspec | ||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | C<locspec> is a constructor. It takes either a string (in the form of a path) or an arrayref. Either will be stored as an arrayref in the C<path> attribute. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | sub locspec { | ||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 23 | ▶ | if ( 1 == scalar @_ ) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
locspec: called CC=13 SCAR=40.2
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 23 | ▶ | if ( blessed $_[0] and $_[0]->isa('Articulate::LocationSpecification') ) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
Condition33%1/3
builtin::blessed($_[0]) and $_[0]->isa("Articulate::LocationSpecification")
MC/DC0%0/2
builtin::blessed($_[0]) and $_[0]->isa("Articulate::LocationSpecification")
builtin::blessed($_[0])
$_[0]->isa("Articulate::LocationSpecification")
Truth table33%1/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa("Articulate::LocationSpecification")
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | return $_[0]; | |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | ▶ | elsif ( blessed $_[0] and $_[0]->isa('Articulate::Location') ) { | |||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
builtin::blessed($_[0]) and $_[0]->isa("Articulate::Location")
MC/DC0%0/2
builtin::blessed($_[0]) and $_[0]->isa("Articulate::Location")
builtin::blessed($_[0])
$_[0]->isa("Articulate::Location")
Truth table33%1/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa("Articulate::Location")
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | my $path = $_[0]->path; # should this logic be in the coerce? | |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | if (@$path) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | for my $i (1..$#$path) { | |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | if (0 == ($i % 2) ) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | $path->[$i] = '*'; | |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | return __PACKAGE__->new({ path => $path }); | |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | elsif ( ref $_[0] eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 3 | return __PACKAGE__->new({ path => $_[0] }); | |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | elsif ( !defined $_[0] ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | return __PACKAGE__->new; | |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | elsif ( !ref $_[0] ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 20 | return __PACKAGE__->new({ path => [ grep { $_ ne '' } split /\//, $_[0] ] }); | |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | elsif ( ref $_[0] eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | return __PACKAGE__->new($_[0]); | |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | =head3 path | ||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | An arrayref representing the path to the location specification. This is used for overloaded array dereferencing. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | has path => ( | ||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | default => sub { []; }, | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 10 | ); | |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | =head3 location | ||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | $locspec->location->location # same as $locspec | ||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | This method always returns the object itself. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | sub location { | ||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | return shift; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
location: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | =head3 to_file_path | ||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | Joins the contents of C<path> on C</> and returns the result. This is used for overloaded stringification. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | sub to_file_path { | ||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | return join '/', @{ $_[0]->path } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_file_path: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | sub _step_matches { | ||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 438 | ▶ | my ( $left, $right ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_step_matches: called CC=4 SCAR=14.6
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 438 | ▶ | return 1 if ( $left eq '*' ); | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 228 | ▶ | return 1 if ( $right eq '*' ); | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 228 | ▶ | return 1 if ( $left eq $right ); | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | =head3 matches | ||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | locspec('/zone/*')->matches(loc('/zone/public')) # true | ||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | locspec('/zone/*')->matches(loc('/')) # false | ||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | locspec('/zone/*')->matches(loc('/zone/public/article/hello-world')) # false | ||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | Determines if the location given as the first argument matches the locspec. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | sub matches { | ||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 202 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
matches: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 202 | my $location = loc shift; | |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 202 | ▶ | return 0 unless $#$self == $#$location; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 108 | ▶ | return 1 if $#$self == -1; # go no further if both are empty | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 108 | for my $i (0..$#$self) { | |||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 406 | ▶ | return 0 unless _step_matches( $self->[$i], $location->[$i] ); | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 108 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | =head3 matches_ancestor_of | ||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | locspec('/zone/*')->matches_ancestor_of(loc('/zone/public')) # true | ||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | locspec('/zone/*')->matches_ancestor_of(loc('/')) # false | ||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | locspec('/zone/*')->matches_ancestor_of(loc('/zone/public/article/hello-world')) # true | ||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | Determines if the location given as the first argument - or any ancestor thereof - matches the locspec. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | sub matches_ancestor_of { | ||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 7 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
matches_ancestor_of: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 7 | my $location = loc shift; | |||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 7 | ▶ | return 0 unless $#$self <= $#$location; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 5 | ▶ | return 1 if $#$self == -1; # go no further if self is empty | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 5 | for my $i (0..$#$self) { | |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 16 | ▶ | return 0 unless _step_matches( $self->[$i], $location->[$i] ); | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 5 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | =head3 matches_descendant_of | ||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | locspec('/zone/*')->matches_descendant_of(loc('/zone/public')) # true | ||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | locspec('/zone/*')->matches_descendant_of(loc('/')) # true | ||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | locspec('/zone/*')->matches_descendant_of(loc('/zone/public/article/hello-world')) # false | ||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | Determines if the location given as the first argument - or any descendant thereof - matches the locspec. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | sub matches_descendant_of { | ||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 7 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
matches_descendant_of: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 7 | my $location = loc shift; | |||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 7 | ▶ | return 0 unless $#$self >= $#$location; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 5 | ▶ | return 1 if $#$location == -1; # go no further if self is empty | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 5 | for my $i (0..$#$location) { | |||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 16 | ▶ | return 0 unless _step_matches( $self->[$i], $location->[$i] ); | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 5 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 10 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||