Articulate/Flow/MetaSwitch.pm

filter: Statementstmt 93.4% 43 / 46 Branchbran 83.3% 10 / 12 Conditioncond 90.9% 10 / 11 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 77.7% 7 / 9 Podpod 100% 1 / 1 totaltotal 88.1% 74 / 84 CC 15 SCAR 19.1
CC
15
Cov
91%
process_method
11.1
__ANON__
5.0
__ANON__
2.0
CRAP 6.8
« Articulate/Service/List.pm ↑ Summary Articulate/Storage/Local.pm »
1 package Articulate::Flow::MetaSwitch;
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 2 ▶use Articulate::Syntax qw (instantiate instantiate_array dpath_get);
Subroutine BEGIN: called CC=1 SCAR=0.0
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<enrich> and C<augment>, 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  
55 2 has field => ( is => 'rw', );
56  
57 has where => (
58 is => 'rw',
59 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 4 ▶ coerce => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
61 4 ▶ my $orig = shift // [];
Condition50%1/2
shift() // []
l!l
40
MC/DC0%0/1
shift() // []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
62 4 ▶ if ( ref $orig eq ref {} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $orig eq ref {}) { } 1 3 4
63 1 foreach my $type ( keys %$orig ) {
64 2 $orig->{$type} = instantiate_array( $orig->{$type} );
65 }
66 }
67 else {
68 3 foreach my $rule (@$orig) {
69 4 $rule->{then} = instantiate_array( $rule->{then} );
70 }
71 }
72 4 return $orig;
73 },
74 2 );
75  
76 has otherwise => (
77 is => 'rw',
78 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
79 4 ▶ coerce => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
80 4 instantiate_array(@_);
81 },
82 2 );
83  
84 4 ▶sub process_method {
Subroutine process_method: called CC=11 SCAR=24.0
Pod documented
85 4 my $self = shift;
86 4 my $method = shift;
87 4 my $item = shift;
88 4 ▶ if ( ref $self->where eq ref {} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->where eq ref {}) 1 3 4
89 1 my $field = $self->field;
90 1 my $actual_value = dpath_get( $item->meta, $field );
91 1 foreach my $value ( keys %{ $self->where } ) {
92 2 ▶ if ( $value eq $actual_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq $actual_value) 1 1 2
93 1 return $self->_delegate( $method => $self->where->{$value},
94 [ $item, @_ ] );
95 }
96 }
97 }
98 3 ▶ if ( ref $self->where eq ref [] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->where eq ref []) 3 0 3
99 3 foreach my $where ( @{ $self->where } ) {
100 4 my $actual_value = dpath_get( $item->meta, $where->{field} );
101 4 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $where->{"value"} and defined $actual_value or defined $where->{"value"} and $where->{"value"} eq $actual_value) 2 2 4
Condition100%9/9
not defined $where->{"value"} and defined $actual_value or defined $where->{"value"} and $where->{"value"} eq $actual_value
l!l&&r!l&&!r
112
not defined $where->{"value"} and defined $actual_value
!ll&&!rl&&r
211
defined $where->{"value"} and $where->{"value"} eq $actual_value
!ll&&!rl&&r
111
MC/DC75%3/4
not defined $where->{"value"} and defined $actual_value or defined $where->{"value"} and $where->{"value"} eq $actual_value
not defined $where->{"value"} defined $actual_value defined $where->{"value"} $where->{"value"} eq $actual_value
Truth table57%4/7
not(A) and B or C and D
A= not defined $where->{"value"}B= defined $actual_valueC= defined $where->{"value"}D= $where->{"value"} eq $actual_value
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
102 ( !defined $where->{value} and defined $actual_value )
103 || ( defined $where->{value}
104 and $where->{value} eq $actual_value ) # this is naive!
105 )
106 {
107 2 return $self->_delegate( $method => $where->{then}, [ $item, @_ ] );
108 }
109 }
110 }
111 1 ▶ if ( defined $self->otherwise ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->otherwise) 1 0 1
112 1 return $self->_delegate( $method => $self->otherwise, [ $item, @_ ] );
113 }
114 0 return $item;
115 }
116  
117 2 1;
« Articulate/Service/List.pm ↑ Summary Articulate/Storage/Local.pm »