Articulate/LocationSpecification.pm

filter: Statementstmt 79.7% 55 / 69 Branchbran 54.7% 23 / 42 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 81.2% 13 / 16 Podpod 100% 6 / 6 totaltotal 69.2% 99 / 143 CC 28 SCAR 29.6
CC
28
Cov
68%
locspec
55.6
matches
5.1
matches_ancestor_of
5.1
CRAP 19.3
« Articulate/Serialisation/Asset.pm ↑ Summary Articulate/Serialisation.pm »
1 package Articulate::LocationSpecification;
2 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 11 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶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 11 ▶use Articulate::Location;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 ▶use Exporter::Declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 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
25 'glob', and provides methods so that it can be compared with locations.
26 It is similar to C<Articulate::Location>, and stringifies to the 'file
27 path' representation.
28  
29 The main use of this is to determine whether a user has access to a
30 resource based on rules (e.g.
31 L<Articulate::Authorisation::LocationBased>).
32  
33 =cut
34  
35 =head1 FUNCTIONS
36  
37 =head3 locspec
38  
39 C<locspec> is a constructor. It takes either a string (in the form of a
40 path) or an arrayref. Either will be stored as an arrayref in the
41 C<path> attribute.
42  
43 =cut
44  
45 sub locspec {
46 27 ▶ if ( 1 == scalar @_ ) {
Subroutine locspec: called CC=13 SCAR=40.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (1 == scalar @_) 27 0 27
47 27 ▶ if ( blessed $_[0] and $_[0]->isa('Articulate::LocationSpecification') ) {
Branch: 6 branches
BranchTrueFalseTotal
if (builtin::blessed($_[0]) and $_[0]->isa('Articulate::LocationSpecification')) { } 0 27 27
elsif (builtin::blessed($_[0]) and $_[0]->isa('Articulate::Location')) { } 0 27 27
elsif (ref $_[0] eq 'ARRAY') { } 4 23 27
elsif (not defined $_[0]) { } 0 23 23
elsif (not ref $_[0]) { } 23 0 23
elsif (ref $_[0] eq 'HASH') { } 0 0 0
Condition33%1/3
builtin::blessed($_[0]) and $_[0]->isa("Articulate::LocationSpecification")
!ll&&!rl&&r
2700
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")
ABresult
0X0
100
111
48 0 return $_[0];
49 }
50 ▶ elsif ( blessed $_[0] and $_[0]->isa('Articulate::Location') ) {
Condition33%1/3
builtin::blessed($_[0]) and $_[0]->isa("Articulate::Location")
!ll&&!rl&&r
2700
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")
ABresult
0X0
100
111
51 0 my $path = $_[0]->path; # should this logic be in the coerce?
52 0 ▶ if (@$path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$path) 0 0 0
53 0 for my $i ( 1 .. $#$path ) {
54 0 ▶ if ( 0 == ( $i % 2 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == $i % 2) 0 0 0
55 0 $path->[$i] = '*';
56 }
57 }
58 }
59 0 return __PACKAGE__->new( { path => $path } );
60 }
61 elsif ( ref $_[0] eq 'ARRAY' ) {
62 4 return __PACKAGE__->new( { path => $_[0] } );
63 }
64 elsif ( !defined $_[0] ) {
65 0 return __PACKAGE__->new;
66 }
67 elsif ( !ref $_[0] ) {
68 return __PACKAGE__->new(
69 23 { path => [ grep { $_ ne '' } split /\//, $_[0] ] } );
70 }
71 elsif ( ref $_[0] eq 'HASH' ) {
72 0 return __PACKAGE__->new( $_[0] );
73 }
74 }
75 }
76  
77 =head1 ATTRIBUTE
78  
79 =head3 path
80  
81 An arrayref representing the path to the location specification. This
82 is used for overloaded array dereferencing.
83  
84 =cut
85  
86 has path => (
87 is => 'rw',
88 0 ▶ default => sub { []; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 11 );
90  
91 =head1 METHODS
92  
93 =head3 location
94  
95 $locspec->location->location # same as $locspec
96  
97 This method always returns the object itself.
98  
99 =cut
100  
101 sub location {
102 0 ▶ return shift;
Subroutine location: not called CC=1 SCAR=6.9
Pod documented
103 }
104  
105 =head3 to_file_path
106  
107 Joins the contents of C<path> on C</> and returns the result. This is
108 used for overloaded stringification.
109  
110 =cut
111  
112 sub to_file_path {
113 0 ▶ return join '/', @{ $_[0]->path };
Subroutine to_file_path: not called CC=1 SCAR=6.9
Pod documented
114 }
115  
116 sub _step_matches {
117 438 ▶ my ( $left, $right ) = @_;
Subroutine _step_matches: called CC=4 SCAR=14.6
118 438 ▶ return 1 if ( $left eq '*' );
Branch: 1 branch
BranchTrueFalseTotal
if $left eq "*" 210 228 438
119 228 ▶ return 1 if ( $right eq '*' );
Branch: 1 branch
BranchTrueFalseTotal
if $right eq "*" 0 228 228
120 228 ▶ return 1 if ( $left eq $right );
Branch: 1 branch
BranchTrueFalseTotal
if $left eq $right 228 0 228
121 0 return 0;
122  
123 }
124  
125 =head3 matches
126  
127 locspec('/zone/*')->matches(loc('/zone/public')) # true
128 locspec('/zone/*')->matches(loc('/')) # false
129 locspec('/zone/*')->matches(loc('/zone/public/article/hello-world')) # false
130  
131 Determines if the location given as the first argument matches the
132 locspec.
133  
134 =cut
135  
136 sub matches {
137 202 ▶ my $self = shift;
Subroutine matches: called CC=5 SCAR=16.3
Pod documented
138 202 my $location = loc shift;
139 202 ▶ return 0 unless $#$self == $#$location;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$#* == $location->$#* 94 108 202
140 108 ▶ return 1 if $#$self == -1; # go no further if both are empty
Branch: 1 branch
BranchTrueFalseTotal
if $self->$#* == -1 0 108 108
141 108 for my $i ( 0 .. $#$self ) {
142 406 ▶ return 0 unless _step_matches( $self->[$i], $location->[$i] );
Branch: 1 branch
BranchTrueFalseTotal
unless _step_matches($self->[$i], $location->[$i]) 0 406 406
143 }
144 108 return 1;
145 }
146  
147 =head3 matches_ancestor_of
148  
149 locspec('/zone/*')->matches_ancestor_of(loc('/zone/public')) # true
150 locspec('/zone/*')->matches_ancestor_of(loc('/')) # false
151 locspec('/zone/*')->matches_ancestor_of(loc('/zone/public/article/hello-world')) # true
152  
153 Determines if the location given as the first argument - or any
154 ancestor thereof - matches the locspec.
155  
156 =cut
157  
158 sub matches_ancestor_of {
159 7 ▶ my $self = shift;
Subroutine matches_ancestor_of: called CC=5 SCAR=16.3
Pod documented
160 7 my $location = loc shift;
161 7 ▶ return 0 unless $#$self <= $#$location;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$#* <= $location->$#* 2 5 7
162 5 ▶ return 1 if $#$self == -1; # go no further if self is empty
Branch: 1 branch
BranchTrueFalseTotal
if $self->$#* == -1 0 5 5
163 5 for my $i ( 0 .. $#$self ) {
164 16 ▶ return 0 unless _step_matches( $self->[$i], $location->[$i] );
Branch: 1 branch
BranchTrueFalseTotal
unless _step_matches($self->[$i], $location->[$i]) 0 16 16
165 }
166 5 return 1;
167 }
168  
169 =head3 matches_descendant_of
170  
171 locspec('/zone/*')->matches_descendant_of(loc('/zone/public')) # true
172 locspec('/zone/*')->matches_descendant_of(loc('/')) # true
173 locspec('/zone/*')->matches_descendant_of(loc('/zone/public/article/hello-world')) # false
174  
175 Determines if the location given as the first argument - or any
176 descendant thereof - matches the locspec.
177  
178 =cut
179  
180 sub matches_descendant_of {
181 7 ▶ my $self = shift;
Subroutine matches_descendant_of: called CC=5 SCAR=16.3
Pod documented
182 7 my $location = loc shift;
183 7 ▶ return 0 unless $#$self >= $#$location;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$#* >= $location->$#* 2 5 7
184 5 ▶ return 1 if $#$location == -1; # go no further if self is empty
Branch: 1 branch
BranchTrueFalseTotal
if $location->$#* == -1 0 5 5
185 5 for my $i ( 0 .. $#$location ) {
186 16 ▶ return 0 unless _step_matches( $self->[$i], $location->[$i] );
Branch: 1 branch
BranchTrueFalseTotal
unless _step_matches($self->[$i], $location->[$i]) 0 16 16
187 }
188 5 return 1;
189 }
190  
191 =head1 SEE ALSO
192  
193 =over
194  
195 =item * L<Articulate::Location>
196  
197 =item * L<Articulate::Permission>
198  
199 =back
200  
201 =cut
202  
203 11 1;
« Articulate/Serialisation/Asset.pm ↑ Summary Articulate/Serialisation.pm »