Articulate/LocationSpecification.pm

filter: Statementstmt 82.8% 58 / 70 Branchbran 54.7% 23 / 42 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 88.2% 15 / 17 Podpod 100% 6 / 6 totaltotal 71.7% 104 / 145 CC 28 SCAR 29.4
CC
28
Cov
70%
new_location_specification
55.6
matches
5.1
matches_ancestor_of
5.1
CRAP 18.9
« Articulate/Serialisation/Asset.pm ↑ Summary Articulate/Serialisation.pm »
1 package Articulate::LocationSpecification;
2 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 12 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use overload '""' => sub { shift->to_file_path }, '@{}' => sub { shift->path };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
8 12 ▶use Articulate::Location;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 12 ▶use Exporter::Declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 default_exports qw(new_location_specification);
12  
13 =head1 NAME
14  
15 Articulate::LocationSpecification - represent a specification
16  
17 =cut
18  
19 =head1 DESCRIPTION
20  
21 new_location_specification ['zone', '*', 'article', 'hello-world']
22 new_location_specification '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 new_location_specification
38  
39 C<new_location_specification> is a constructor. It takes either a
40 string (in the form of a path) or an arrayref. Either will be stored as
41 an arrayref in the C<path> attribute.
42  
43 =cut
44  
45 33 ▶sub new_location_specification {
Subroutine new_location_specification: called CC=13 SCAR=40.2
Pod documented
46 33 ▶ if ( 1 == scalar @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == scalar @_) 33 0 33
47 33 ▶ if ( blessed $_[0] and $_[0]->isa('Articulate::LocationSpecification') ) {
Branch: 6 branches
BranchTrueFalseTotal
if (builtin::blessed($_[0]) and $_[0]->isa('Articulate::LocationSpecification')) { } 0 33 33
elsif (builtin::blessed($_[0]) and $_[0]->isa('Articulate::Location')) { } 0 33 33
elsif (ref $_[0] eq 'ARRAY') { } 5 28 33
elsif (not defined $_[0]) { } 0 28 28
elsif (not ref $_[0]) { } 28 0 28
elsif (ref $_[0] eq 'HASH') { } 0 0 0
Condition33%1/3
builtin::blessed($_[0]) and $_[0]->isa("Articulate::LocationSpecification")
!ll&&!rl&&r
3300
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
3300
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 5 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 28 { 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 12 );
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 0 ▶sub location {
Subroutine location: not called CC=1 SCAR=6.9
Pod documented
102 0 return shift;
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 2 ▶sub to_file_path {
Subroutine to_file_path: called CC=1 SCAR=0.0
Pod documented
113 2 return join '/', @{ $_[0]->path };
114 }
115  
116 440 ▶sub _step_matches {
Subroutine _step_matches: called CC=4 SCAR=14.6
117 440 my ( $left, $right ) = @_;
118 440 ▶ return 1 if ( $left eq '*' );
Branch: 1 branch
BranchTrueFalseTotal
if $left eq "*" 211 229 440
119 229 ▶ return 1 if ( $right eq '*' );
Branch: 1 branch
BranchTrueFalseTotal
if $right eq "*" 0 229 229
120 229 ▶ return 1 if ( $left eq $right );
Branch: 1 branch
BranchTrueFalseTotal
if $left eq $right 229 0 229
121 0 return 0;
122  
123 }
124  
125 =head3 matches
126  
127 new_location_specification('/zone/*')->matches(new_location('/zone/public')) # true
128 new_location_specification('/zone/*')->matches(new_location('/')) # false
129 new_location_specification('/zone/*')->matches(new_location('/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 203 ▶sub matches {
Subroutine matches: called CC=5 SCAR=16.3
Pod documented
137 203 my $self = shift;
138 203 my $location = new_location shift;
139 203 ▶ return 0 unless $#$self == $#$location;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$#* == $location->$#* 94 109 203
140 109 ▶ return 1 if $#$self == -1; # go no further if both are empty
Branch: 1 branch
BranchTrueFalseTotal
if $self->$#* == -1 0 109 109
141 109 for my $i ( 0 .. $#$self ) {
142 408 ▶ return 0 unless _step_matches( $self->[$i], $location->[$i] );
Branch: 1 branch
BranchTrueFalseTotal
unless _step_matches($self->[$i], $location->[$i]) 0 408 408
143 }
144 109 return 1;
145 }
146  
147 =head3 matches_ancestor_of
148  
149 new_location_specification('/zone/*')->matches_ancestor_of(new_location('/zone/public')) # true
150 new_location_specification('/zone/*')->matches_ancestor_of(new_location('/')) # false
151 new_location_specification('/zone/*')->matches_ancestor_of(new_location('/zone/public/article/hello-world')) # true
152  
153 Determines if the location given as the first argument - or any
154 ancestor thereof - matches the new_location_specification.
155  
156 =cut
157  
158 7 ▶sub matches_ancestor_of {
Subroutine matches_ancestor_of: called CC=5 SCAR=16.3
Pod documented
159 7 my $self = shift;
160 7 my $location = new_location 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 new_location_specification('/zone/*')->matches_descendant_of(new_location('/zone/public')) # true
172 new_location_specification('/zone/*')->matches_descendant_of(new_location('/')) # true
173 new_location_specification('/zone/*')->matches_descendant_of(new_location('/zone/public/article/hello-world')) # false
174  
175 Determines if the location given as the first argument - or any
176 descendant thereof - matches the new_location_specification.
177  
178 =cut
179  
180 7 ▶sub matches_descendant_of {
Subroutine matches_descendant_of: called CC=5 SCAR=16.3
Pod documented
181 7 my $self = shift;
182 7 my $location = new_location 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::Navigation>
198  
199 =item * L<Articulate::Permission>
200  
201 =back
202  
203 =cut
204  
205 12 1;
« Articulate/Serialisation/Asset.pm ↑ Summary Articulate/Serialisation.pm »