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 10     10   28101 use strict;
  10         15  
  10         350  
3 10     10   42 use warnings;
  10         13  
  10         242  
4              
5 10     10   803 use Moo;
  10         16743  
  10         46  
6 10     10   3514 use Scalar::Util qw(blessed);
  10         14  
  10         916  
7 10     10   1292 use overload '""' => \&to_file_path, '@{}' => sub{ shift->path };
  10     701   805  
  10         101  
  701         2009  
8              
9 10     10   1098 use Exporter::Declare;
  10         18086  
  10         68  
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 Articulate ecosystem. It contains an array of slugs, and stringifies to the 'file path' representation of them.
24              
25             =cut
26              
27             =head1 FUNCTIONS
28              
29             =head3 loc
30              
31             C 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 attribute.
32              
33             =cut
34              
35             sub loc {
36 393 50   393 1 7316 if ( 1 == scalar @_ ) {
37 393 100 66     2579 if ( blessed $_[0] and $_[0]->can('location') ) {
    50          
    50          
    50          
    0          
38 333         888 return $_[0];
39             }
40             elsif ( ref $_[0] eq 'ARRAY' ) {
41 0         0 return __PACKAGE__->new({ path => $_[0] });
42             }
43             elsif ( !defined $_[0] ) {
44 0         0 return __PACKAGE__->new;
45             }
46             elsif ( !ref $_[0] ) {
47 60         273 return __PACKAGE__->new({ path => [ grep { $_ ne '' } split /\//, $_[0] ] });
  218         1838  
48             }
49             elsif ( ref $_[0] eq 'HASH' ) {
50 0         0 return __PACKAGE__->new($_[0]);
51             }
52             }
53             };
54              
55             =head1 METHODS
56              
57             =head3 path
58              
59             An arrayref representing the path to the location. This is used for overloaded array dereferencing.
60              
61             =cut
62              
63             has path => (
64             is => 'rw',
65             default => sub { []; },
66             );
67              
68             =head3 location
69              
70             $location->location->location # same as $location
71              
72             This method always returns the object itself.
73              
74             =cut
75              
76              
77             sub location {
78 70     70 1 138 return shift;
79             }
80              
81             =head3 to_file_path
82              
83             Joins the contents of C on C and returns the result. This is used for overloaded stringification.
84              
85             =cut
86              
87             sub to_file_path {
88 331     331 1 34916 return join '/', @{ $_[0]->path }
  331         1600  
89             };
90              
91             1;