File Coverage

blib/lib/Articulate/Location.pm
Criterion Covered Total %
statement 27 30 90.0
branch 6 12 50.0
condition 2 3 66.6
subroutine 10 10 100.0
pod 3 3 100.0
total 48 58 82.7


line stmt bran cond sub pod time code
1             package Articulate::Location;
2 11     11   27263 use strict;
  11         15  
  11         377  
3 11     11   52 use warnings;
  11         17  
  11         266  
4              
5 11     11   556 use Moo;
  11         12114  
  11         71  
6 11     11   3959 use Scalar::Util qw(blessed);
  11         21  
  11         982  
7 11     11   1089 use overload '""' => \&to_file_path, '@{}' => sub { shift->path };
  11     701   896  
  11         102  
  701         2080  
8              
9 11     11   1162 use Exporter::Declare;
  11         19039  
  11         79  
10             default_exports qw(loc);
11              
12             =head1 NAME
13              
14             Articulate::Location - represent an item's location
15              
16             =cut
17              
18             =head1 DESCRIPTION
19              
20             loc ['zone', 'public', 'article', 'hello-world']
21             loc '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 loc
32              
33             C is a constructor. It takes either a string (in the form of a
34             path) or an arrayref. Either will be stored as an arrayref in the
35             C attribute.
36              
37             =cut
38              
39             sub loc {
40 396 50   396 1 11793 if ( 1 == scalar @_ ) {
41 396 100 66     2696 if ( blessed $_[0] and $_[0]->can('location') ) {
    50          
    50          
    50          
    0          
42 336         959 return $_[0];
43             }
44             elsif ( ref $_[0] eq 'ARRAY' ) {
45 0         0 return __PACKAGE__->new( { path => $_[0] } );
46             }
47             elsif ( !defined $_[0] ) {
48 0         0 return __PACKAGE__->new;
49             }
50             elsif ( !ref $_[0] ) {
51 218         1741 return __PACKAGE__->new(
52 60         273 { path => [ grep { $_ ne '' } split /\//, $_[0] ] } );
53             }
54             elsif ( ref $_[0] eq 'HASH' ) {
55 0         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             default => sub { []; },
73             );
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             sub location {
87 76     76 1 127 return shift;
88             }
89              
90             =head3 to_file_path
91              
92             Joins the contents of C on C and returns the result. This is
93             used for overloaded stringification.
94              
95             =cut
96              
97             sub to_file_path {
98 348     348 1 38454 return join '/', @{ $_[0]->path };
  348         1710  
99             }
100              
101             =head1 SEE ALSO
102              
103             =over
104              
105             =item * L
106              
107             =item * L
108              
109             =back
110              
111             =cut
112              
113             1;