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