File Coverage

blib/lib/Articulate/Construction/LocationBased.pm
Criterion Covered Total %
statement 23 24 95.8
branch 2 4 50.0
condition 1 3 33.3
subroutine 6 6 100.0
pod 1 1 100.0
total 33 38 86.8


line stmt bran cond sub pod time code
1             package Articulate::Construction::LocationBased;
2 6     6   3296 use strict;
  6         14  
  6         195  
3 6     6   26 use warnings;
  6         9  
  6         158  
4              
5             =head1 NAME
6              
7             Articulate::Construction::LocationBased - Create an item based on its
8             location
9              
10             =head1 ATTRIBUTES
11              
12             =head3 types
13              
14             This should be a hashref mapping types to class names to be used in
15             constructors, where a type in this case is the penultimate endpoint of
16             locations with an even number of parts
17              
18             So:
19              
20             article: Articulate::Item::Article
21              
22             ...would result in C or C
23             becoming Cs but not C
,
24             C, or C.
25              
26             =head1 METHODS
27              
28             =head3 construct
29              
30             $self->construct( {
31             location => 'zone/public/article/hello-world',
32             meta => { ... }
33             content => " ... "
34             } );
35              
36             Attempts to construct the item. Determines the desired class based on
37             the mapping in the C attribute, then calls C<<
38             $class->new($args) >> on the class. Returns C if no appropriate
39             class found.
40              
41             In the above example, C<< $self->types->{article} >> would be
42             consulted.
43              
44             If the location is root or not a multiple of 2 (e.g. C is
45             even and a C but C is odd), returns
46             C.
47              
48             =cut
49              
50 6     6   27 use Moo;
  6         8  
  6         28  
51              
52 6     6   1381 use Articulate::Syntax;
  6         17  
  6         41  
53              
54 6     6   7497 use Module::Load ();
  6         13  
  6         882  
55              
56             has types => (
57             is => 'rw',
58             default => sub { {} },
59             );
60              
61             sub construct {
62 5     5 1 10 my $self = shift;
63 5         10 my $args = shift;
64 5         22 my $location = new_location( $args->{location} );
65 5 50 33     1312 if ( scalar(@$location) and 0 == ( scalar(@$location) % 2 ) ) {
66 5 50       27 if ( exists $self->types->{ $location->[-2] } ) {
67 5         14 my $class = $self->types->{ $location->[-2] };
68 5         31 Module::Load::load($class);
69 5         425 return $class->new($args);
70             }
71             }
72 0           return undef;
73             }
74              
75             1;