Articulate/Location.pm

filter: Statementstmt 91.4% 32 / 35 Branchbran 50.0% 6 / 12 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 83.5% 56 / 67 CC 8 SCAR 18.2
CC
8
Cov
80%
new_location
13.3
BEGIN
1.0
BEGIN
1.0
CRAP 6.2
« Articulate/Syntax.pm ↑ Summary Articulate/Service.pm »
1 package Articulate::Location;
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  
9 12 ▶use Exporter::Declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 default_exports qw(new_location);
11  
12 =head1 NAME
13  
14 Articulate::Location - represent an item's location
15  
16 =cut
17  
18 =head1 DESCRIPTION
19  
20 new_location ['zone', 'public', 'article', 'hello-world']
21 new_location 'zone/public/article/hello-world' # same thing
22  
23 An object class which represents an item's location within the
24 Articulate ecosystem. It contains an array of slugs, and stringifies to
25 the 'file path' representation of them.
26  
27 =cut
28  
29 =head1 FUNCTIONS
30  
31 =head3 new_location
32  
33 C<new_location> is a constructor. It takes either a string (in the form
34 of a path) or an arrayref. Either will be stored as an arrayref in the
35 C<path> attribute.
36  
37 =cut
38  
39 399 ▶sub new_location {
Subroutine new_location: called CC=8 SCAR=25.8
Pod documented
40 399 ▶ if ( 1 == scalar @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == scalar @_) 399 0 399
41 399 ▶ if ( blessed $_[0] and $_[0]->can('location') ) {
Branch: 5 branches
BranchTrueFalseTotal
if (builtin::blessed($_[0]) and $_[0]->can('location')) { } 337 62 399
elsif (ref $_[0] eq 'ARRAY') { } 0 62 62
elsif (not defined $_[0]) { } 0 62 62
elsif (not ref $_[0]) { } 62 0 62
elsif (ref $_[0] eq 'HASH') { } 0 0 0
Condition66%2/3
builtin::blessed($_[0]) and $_[0]->can("location")
!ll&&!rl&&r
620337
MC/DC50%1/2
builtin::blessed($_[0]) and $_[0]->can("location")
builtin::blessed($_[0]) $_[0]->can("location")
Truth table66%2/3
A and B
A= builtin::blessed($_[0])B= $_[0]->can("location")
ABresult
0X0
100
111
42 337 return $_[0];
43 }
44 elsif ( ref $_[0] eq 'ARRAY' ) {
45 0 return __PACKAGE__->new( { path => $_[0] } );
46 }
47 elsif ( !defined $_[0] ) {
48 0 return __PACKAGE__->new;
49 }
50 elsif ( !ref $_[0] ) {
51 return __PACKAGE__->new(
52 62 { path => [ grep { $_ ne '' } split /\//, $_[0] ] } );
53 }
54 elsif ( ref $_[0] eq 'HASH' ) {
55 0 return __PACKAGE__->new( $_[0] );
56 }
57 }
58 }
59  
60 =head1 ATTRIBUTE
61  
62 =head3 path
63  
64 An arrayref representing the path to the location. This is how the
65 location is actually stored and is used for overloaded array
66 dereferencing.
67  
68 =cut
69  
70 has path => (
71 is => 'rw',
72 19 ▶ default => sub { []; },
Subroutine __ANON__: called CC=1 SCAR=0.0
73 12 );
74  
75 =head1 METHODS
76  
77 =head3 location
78  
79 $location->location->location # same as $location
80  
81 This method always returns the object itself. It is useful when you
82 want to allow either an Item or a Location as an argument.
83  
84 =cut
85  
86 76 ▶sub location {
Subroutine location: called CC=1 SCAR=0.0
Pod documented
87 76 return shift;
88 }
89  
90 =head3 to_file_path
91  
92 Joins the contents of C<path> on C</> and returns the result. This is
93 used for overloaded stringification.
94  
95 =cut
96  
97 246 ▶sub to_file_path {
Subroutine to_file_path: called CC=1 SCAR=0.0
Pod documented
98 246 return join '/', @{ $_[0]->path };
99 }
100  
101 =head1 SEE ALSO
102  
103 =over
104  
105 =item * L<Articulate::Item>
106  
107 =item * L<Articulate::LocationSpecification>
108  
109 =item * L<Articulate::Navigation>
110  
111 =back
112  
113 =cut
114  
115 12 1;
« Articulate/Syntax.pm ↑ Summary Articulate/Service.pm »