| 1 | package Articulate::Flow::LocationSwitch; | ||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 2 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 2 | with 'Articulate::Role::Flow'; | |||||||||||
| 6 | use Articulate::Syntax | ||||||||||||
| 7 | 2 | ▶ | qw (new_location_specification instantiate instantiate_array); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | =head1 NAME | ||||||||||||
| 10 | |||||||||||||
| 11 | Articulate::Flow::LocationSwitch - case switching for location | ||||||||||||
| 12 | |||||||||||||
| 13 | =head1 CONFIGURATION | ||||||||||||
| 14 | |||||||||||||
| 15 | - class: Articulate::Flow::LocationSwitch | ||||||||||||
| 16 | args: | ||||||||||||
| 17 | where: | ||||||||||||
| 18 | '/assets/*/*/*': | ||||||||||||
| 19 | - Enrich::Asset | ||||||||||||
| 20 | otherwise: | ||||||||||||
| 21 | - Enrich::Content | ||||||||||||
| 22 | |||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||
| 24 | |||||||||||||
| 25 | This provides a convenient interface to a common branching pattern. | ||||||||||||
| 26 | When performing actions like C<enrich> and C<augment>, a developer will | ||||||||||||
| 27 | typically want to make some processes dependant on the location of the | ||||||||||||
| 28 | content being stored. | ||||||||||||
| 29 | |||||||||||||
| 30 | Rather than having to write a 'black box' provider every time, this | ||||||||||||
| 31 | class provides a standard way of doing it. | ||||||||||||
| 32 | |||||||||||||
| 33 | =head1 METHODS | ||||||||||||
| 34 | |||||||||||||
| 35 | =head3 enrich | ||||||||||||
| 36 | |||||||||||||
| 37 | $self->enrich( $item, $request ); | ||||||||||||
| 38 | $self->process_method( enrich => $item, $request ); | ||||||||||||
| 39 | |||||||||||||
| 40 | =head3 augment | ||||||||||||
| 41 | |||||||||||||
| 42 | $self->augment( $item, $request ); | ||||||||||||
| 43 | $self->process_method( augment => $item, $request ); | ||||||||||||
| 44 | |||||||||||||
| 45 | =head3 process_method | ||||||||||||
| 46 | |||||||||||||
| 47 | $self->process_method( $verb, $item, $request ); | ||||||||||||
| 48 | |||||||||||||
| 49 | Goes through each of the keys of C<< $self->where >>; if the location | ||||||||||||
| 50 | of C<$item> matches location specification given (see | ||||||||||||
| 51 | L<Articulate::LocationSpecification>), then instantiates the value of | ||||||||||||
| 52 | that key and performs the same verb on the arguments. | ||||||||||||
| 53 | |||||||||||||
| 54 | If none of the where clauses matched, the otherwise provider, if one is | ||||||||||||
| 55 | specified, will be used. | ||||||||||||
| 56 | |||||||||||||
| 57 | =cut | ||||||||||||
| 58 | |||||||||||||
| 59 | has where => ( | ||||||||||||
| 60 | is => 'rw', | ||||||||||||
| 61 | 0 | ▶ | default => sub { {} }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 62 | 2 | ▶ | coerce => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||
| 63 | 2 | ▶ | my $orig = shift // {}; | ||||||||||
|
Condition50%1/2
shift() // {}
MC/DC0%0/1
shift() // {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 64 | 2 | foreach my $type ( keys %$orig ) { | |||||||||||
| 65 | 2 | $orig->{$type} = instantiate_array( $orig->{$type} ); | |||||||||||
| 66 | } | ||||||||||||
| 67 | 2 | return $orig; | |||||||||||
| 68 | }, | ||||||||||||
| 69 | 2 | ); | |||||||||||
| 70 | |||||||||||||
| 71 | has otherwise => ( | ||||||||||||
| 72 | is => 'rw', | ||||||||||||
| 73 | 0 | ▶ | default => sub { [] }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 74 | 2 | ▶ | coerce => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 75 | 2 | instantiate_array(@_); | |||||||||||
| 76 | }, | ||||||||||||
| 77 | 2 | ); | |||||||||||
| 78 | |||||||||||||
| 79 | 2 | ▶ | sub process_method { | ||||||||||
|
Subroutine
process_method: called CC=5 SCAR=18.3
Pod
documented
| |||||||||||||
| 80 | 2 | my $self = shift; | |||||||||||
| 81 | 2 | my $method = shift; | |||||||||||
| 82 | 2 | my $item = shift; | |||||||||||
| 83 | 2 | my $location = $item->location; | |||||||||||
| 84 | 2 | ▶ | if ( defined $location ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 85 | 2 | foreach my $locspec_string ( keys %{ $self->where } ) { | |||||||||||
| 86 | 2 | my $location_specification = new_location_specification $locspec_string; | |||||||||||
| 87 | 2 | ▶ | if ( $location_specification->matches($location) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | return $self->_delegate( | ||||||||||||
| 89 | 2 | $method => $self->where->{$locspec_string}, | |||||||||||
| 90 | [ $item, @_ ] | ||||||||||||
| 91 | ); | ||||||||||||
| 92 | } | ||||||||||||
| 93 | } | ||||||||||||
| 94 | } | ||||||||||||
| 95 | 0 | ▶ | if ( defined $self->otherwise ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 96 | 0 | return $self->_delegate( $method => $self->otherwise, [ $item, @_ ] ); | |||||||||||
| 97 | } | ||||||||||||
| 98 | 0 | return $item; | |||||||||||
| 99 | } | ||||||||||||
| 100 | |||||||||||||
| 101 | 2 | 1; | |||||||||||