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   1250 use strict;
  2         3  
  2         57  
3 2     2   7 use warnings;
  2         3  
  2         36  
4 2     2   8 use Moo;
  2         2  
  2         8  
5             with 'Articulate::Role::Flow';
6             use Articulate::Syntax
7 2     2   804 qw (new_location_specification instantiate instantiate_array);
  2         4  
  2         9  
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 and C, 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), 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             default => sub { {} },
62             coerce => sub {
63             my $orig = shift // {};
64             foreach my $type ( keys %$orig ) {
65             $orig->{$type} = instantiate_array( $orig->{$type} );
66             }
67             return $orig;
68             },
69             );
70              
71             has otherwise => (
72             is => 'rw',
73             default => sub { [] },
74             coerce => sub {
75             instantiate_array(@_);
76             },
77             );
78              
79             sub process_method {
80 2     2 1 1 my $self = shift;
81 2         4 my $method = shift;
82 2         3 my $item = shift;
83 2         16 my $location = $item->location;
84 2 50       458 if ( defined $location ) {
85 2         3 foreach my $locspec_string ( keys %{ $self->where } ) {
  2         18  
86 2         424 my $location_specification = new_location_specification $locspec_string;
87 2 50       810 if ( $location_specification->matches($location) ) {
88 2         39 return $self->_delegate(
89             $method => $self->where->{$locspec_string},
90             [ $item, @_ ]
91             );
92             }
93             }
94             }
95 0 0         if ( defined $self->otherwise ) {
96 0           return $self->_delegate( $method => $self->otherwise, [ $item, @_ ] );
97             }
98 0           return $item;
99             }
100              
101             1;