File Coverage

blib/lib/Articulate/Flow/MetaSwitch.pm
Criterion Covered Total %
statement 30 31 96.7
branch 8 10 80.0
condition 8 9 88.8
subroutine 5 5 100.0
pod 1 1 100.0
total 52 56 92.8


line stmt bran cond sub pod time code
1             package Articulate::Flow::MetaSwitch;
2 2     2   1170 use strict;
  2         2  
  2         60  
3 2     2   6 use warnings;
  2         2  
  2         38  
4 2     2   6 use Moo;
  2         3  
  2         8  
5             with 'Articulate::Role::Flow';
6 2     2   920 use Articulate::Syntax qw (instantiate instantiate_array dpath_get);
  2         5  
  2         14  
7            
8             =head1 NAME
9            
10             Articulate::Flow::MetaSwitch - case switching on metadata
11            
12             =head1 CONFIGURATION
13            
14             - class: Articulate::Flow::MetaSwitch
15             args:
16             field: schema/core/content_type
17             where:
18             'text/markdown':
19             - Articulate::Enrichment::Markdown
20             otherwise:
21             - SomeOther::Enrichment
22             - class: Articulate::Flow::MetaSwitch
23             args:
24             where:
25             - field: schema/core/file
26             then:
27             - Handle::File
28             otherwise:
29             - Assuming::Text
30            
31             =head1 DESCRIPTION
32            
33             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 metadata of that content.
34            
35             Rather than having to write a 'black box' provider every time, this class provides a standard way of doing it.
36            
37             =head1 METHODS
38            
39             =head3 enrich
40            
41             $self->enrich( $item, $request );
42             $self->process_method( enrich => $item, $request );
43            
44             =head3 augment
45            
46             $self->augment( $item, $request );
47             $self->process_method( augment => $item, $request );
48            
49             =head3 process_method
50            
51             $self->process_method( $verb, $item, $request );
52            
53             =cut
54             has field => (
55             is => 'rw',
56             );
57            
58             has where => (
59             is => 'rw',
60             default => sub { [] },
61             coerce => sub {
62             my $orig = shift // [];
63             if ( ref $orig eq ref {} ) {
64             foreach my $type ( keys %$orig ) {
65             $orig->{$type} = instantiate_array ( $orig->{$type} );
66             }
67             }
68             else {
69             foreach my $rule ( @$orig ) {
70             $rule->{then} = instantiate_array ( $rule->{then} );
71             }
72             }
73             return $orig;
74             },
75             );
76            
77             has otherwise => (
78             is => 'rw',
79             default => sub { [] },
80             coerce => sub {
81             instantiate_array(@_)
82             },
83             );
84            
85             sub process_method {
86 4     4 1 5 my $self = shift;
87 4         5 my $method = shift;
88 4         4 my $item = shift;
89 4 100       66 if ( ref $self->where eq ref {} ) {
90 1         14 my $field = $self->field;
91 1         7 my $actual_value = dpath_get($item->meta, $field);
92 1         3 foreach my $value ( keys %{$self->where} ) {
  1         33  
93 2 100       15 if ($value eq $actual_value) {
94 1         25 return $self->_delegate( $method => $self->where->{$value}, [$item, @_] );
95             }
96             }
97             }
98 3 50       516 if ( ref $self->where eq ref [] ) {
99 3         22 foreach my $where ( @{ $self->where } ) {
  3         59  
100 4         37 my $actual_value = dpath_get($item->meta, $where->{field});
101 4 100 100     43 if (
      100        
      66        
102             ( !defined $where->{value} and defined $actual_value )
103             ||
104             ( defined $where->{value} and $where->{value} eq $actual_value ) # this is naive!
105             ) {
106 2         15 return $self->_delegate( $method => $where->{then}, [$item, @_] );
107             }
108             }
109             }
110 1 50       4 if (defined $self->otherwise) {
111 1         470 return $self->_delegate( $method => $self->otherwise, [$item, @_] );
112             }
113 0           return $item;
114             }
115            
116             1;