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 5     5   2367 use strict;
  5         10  
  5         163  
3 5     5   20 use warnings;
  5         6  
  5         129  
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 5     5   19 use Moo;
  5         6  
  5         21  
40              
41 5     5   1227 use Articulate::Syntax;
  5         10  
  5         25  
42              
43 5     5   6074 use Module::Load ();
  5         62  
  5         740  
44              
45             has types => (
46             is => 'rw',
47             default => sub { {} },
48             );
49              
50             sub construct {
51 8     8 1 14 my $self = shift;
52 8         11 my $args = shift;
53 8         31 my $location = loc( $args->{location} );
54 8 50 33     826 if ( scalar(@$location) and 0 == ( scalar(@$location) % 2 ) ) {
55 8 50       30 if ( exists $self->types->{ $location->[-2] } ) {
56 8         24 my $class = $self->types->{ $location->[-2] };
57 8         32 Module::Load::load($class);
58 8         649 return $class->new($args);
59             }
60             }
61 0           return undef;
62             }
63              
64             1;