File Coverage

blib/lib/Articulate/Flow/LocationSwitch.pm
Criterion Covered Total %
statement 22 25 88.0
branch 2 6 33.3
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 30 37 81.0


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