Articulate/Flow/MetaSwitch.pm

filter: Statementstmt 93.4% 43 / 46 Branchbran 75.0% 9 / 12 Conditioncond 90.9% 10 / 11 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 77.7% 7 / 9 Podpod 100% 1 / 1 totaltotal 85.7% 72 / 84 SCAR 27.2
CC
15
Cov
90%
process_method
11.1
__ANON__
5.0
__ANON__
2.0
CRAP 15.2
« Articulate/Storage/DBIC/Simple.pm ↑ Summary Articulate/Sortation/Slug.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 2 has field => (
55 is => 'rw',
56 );
57  
58 has where => (
59 is => 'rw',
60 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 coerce => sub {
62 4 ▶ my $orig = shift // [];
Subroutine __ANON__: called CC=5 SCAR=16.1
Condition50%1/2
shift() // []
l!l
40
MC/DC0%0/1
shift() // []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
63 4 ▶ if ( ref $orig eq ref {} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $orig eq ref {}) { } 1 3 4
64 1 foreach my $type ( keys %$orig ) {
65 2 $orig->{$type} = instantiate_array ( $orig->{$type} );
66 }
67 }
68 else {
69 3 foreach my $rule ( @$orig ) {
70 4 $rule->{then} = instantiate_array ( $rule->{then} );
71 }
72 }
73 4 return $orig;
74 },
75 2 );
76  
77 has otherwise => (
78 is => 'rw',
79 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 coerce => sub {
81 4 ▶ instantiate_array(@_)
Subroutine __ANON__: called CC=1 SCAR=0.0
82 },
83 2 );
84  
85 sub process_method {
86 4 ▶ my $self = shift;
Subroutine process_method: called CC=11 SCAR=24.1
Pod documented
87 4 my $method = shift;
88 4 my $item = shift;
89 4 ▶ if ( ref $self->where eq ref {} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->where eq ref {}) 1 3 4
90 1 my $field = $self->field;
91 1 my $actual_value = dpath_get($item->meta, $field);
92 1 foreach my $value ( keys %{$self->where} ) {
93 1 ▶ if ($value eq $actual_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq $actual_value) 1 0 1
94 1 return $self->_delegate( $method => $self->where->{$value}, [$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/DC50%2/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 table42%3/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 ||
104 ( defined $where->{value} and $where->{value} eq $actual_value ) # this is naive!
105 ) {
106 2 return $self->_delegate( $method => $where->{then}, [$item, @_] );
107 }
108 }
109 }
110 1 ▶ if (defined $self->otherwise) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->otherwise) 1 0 1
111 1 return $self->_delegate( $method => $self->otherwise, [$item, @_] );
112 }
113 0 return $item;
114 }
115  
116 2 1;
« Articulate/Storage/DBIC/Simple.pm ↑ Summary Articulate/Sortation/Slug.pm »