| 1 | package Articulate::Construction::LocationBased; | ||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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</article/foo> or C<zone/public/article/foo> becoming C<Articulate::Item::Article>s but not C<article>, C<zone/article>, or C<zone/public/article>. | ||||||||||||||||||||||||||||
| 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<types> attribute, then calls C<< $class->new($args) >> on the class. Returns C<undef> 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<zone/public> is even and a C<zone> but C<zone/public/article> is odd), returns C<undef>. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =cut | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 4 | ▶ | use Articulate::Syntax; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 4 | ▶ | use Module::Load (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | has types => ( | ||||||||||||||||||||||||||||
| 47 | is => 'rw', | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 49 | 4 | ); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub construct { | ||||||||||||||||||||||||||||
| 52 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
construct: called CC=4 SCAR=15.0
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 8 | my $args = shift; | |||||||||||||||||||||||||||
| 54 | 8 | my $location = loc ($args->{location}); | |||||||||||||||||||||||||||
| 55 | 8 | ▶ | if ( scalar ( @$location ) and 0 == (scalar ( @$location ) % 2) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
scalar @$location and 0 == scalar(@$location) % 2
MC/DC0%0/2
scalar @$location and 0 == scalar(@$location) % 2
scalar @$location
0 == scalar(@$location) % 2
Truth table33%1/3
A and B
A= scalar @$locationB= 0 == scalar(@$location) % 2
| |||||||||||||||||||||||||||||
| 56 | 8 | ▶ | if ( exists $self->types->{ $location->[-2] } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 8 | my $class = $self->types->{ $location->[-2] }; | |||||||||||||||||||||||||||
| 58 | 8 | Module::Load::load($class); | |||||||||||||||||||||||||||
| 59 | 8 | return $class->new($args); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 0 | return undef; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 4 | 1; | |||||||||||||||||||||||||||