line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Articulate::Flow::MetaSwitch; |
2
|
2
|
|
|
2
|
|
1106
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
68
|
|
3
|
2
|
|
|
2
|
|
7
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
39
|
|
4
|
2
|
|
|
2
|
|
6
|
use Moo; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
9
|
|
5
|
|
|
|
|
|
|
with 'Articulate::Role::Flow'; |
6
|
2
|
|
|
2
|
|
845
|
use Articulate::Syntax qw (instantiate instantiate_array dpath_get); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
10
|
|
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
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
has field => ( is => 'rw', ); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
has where => ( |
58
|
|
|
|
|
|
|
is => 'rw', |
59
|
|
|
|
|
|
|
default => sub { [] }, |
60
|
|
|
|
|
|
|
coerce => sub { |
61
|
|
|
|
|
|
|
my $orig = shift // []; |
62
|
|
|
|
|
|
|
if ( ref $orig eq ref {} ) { |
63
|
|
|
|
|
|
|
foreach my $type ( keys %$orig ) { |
64
|
|
|
|
|
|
|
$orig->{$type} = instantiate_array( $orig->{$type} ); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
else { |
68
|
|
|
|
|
|
|
foreach my $rule (@$orig) { |
69
|
|
|
|
|
|
|
$rule->{then} = instantiate_array( $rule->{then} ); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
return $orig; |
73
|
|
|
|
|
|
|
}, |
74
|
|
|
|
|
|
|
); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
has otherwise => ( |
77
|
|
|
|
|
|
|
is => 'rw', |
78
|
|
|
|
|
|
|
default => sub { [] }, |
79
|
|
|
|
|
|
|
coerce => sub { |
80
|
|
|
|
|
|
|
instantiate_array(@_); |
81
|
|
|
|
|
|
|
}, |
82
|
|
|
|
|
|
|
); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub process_method { |
85
|
4
|
|
|
4
|
1
|
4
|
my $self = shift; |
86
|
4
|
|
|
|
|
2
|
my $method = shift; |
87
|
4
|
|
|
|
|
5
|
my $item = shift; |
88
|
4
|
100
|
|
|
|
48
|
if ( ref $self->where eq ref {} ) { |
89
|
1
|
|
|
|
|
9
|
my $field = $self->field; |
90
|
1
|
|
|
|
|
5
|
my $actual_value = dpath_get( $item->meta, $field ); |
91
|
1
|
|
|
|
|
2
|
foreach my $value ( keys %{ $self->where } ) { |
|
1
|
|
|
|
|
22
|
|
92
|
1
|
50
|
|
|
|
10
|
if ( $value eq $actual_value ) { |
93
|
1
|
|
|
|
|
16
|
return $self->_delegate( $method => $self->where->{$value}, |
94
|
|
|
|
|
|
|
[ $item, @_ ] ); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
} |
98
|
3
|
50
|
|
|
|
468
|
if ( ref $self->where eq ref [] ) { |
99
|
3
|
|
|
|
|
17
|
foreach my $where ( @{ $self->where } ) { |
|
3
|
|
|
|
|
46
|
|
100
|
4
|
|
|
|
|
23
|
my $actual_value = dpath_get( $item->meta, $where->{field} ); |
101
|
4
|
100
|
100
|
|
|
31
|
if ( |
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
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
|
|
|
|
|
9
|
return $self->_delegate( $method => $where->{then}, [ $item, @_ ] ); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
} |
111
|
1
|
50
|
|
|
|
3
|
if ( defined $self->otherwise ) { |
112
|
1
|
|
|
|
|
420
|
return $self->_delegate( $method => $self->otherwise, [ $item, @_ ] ); |
113
|
|
|
|
|
|
|
} |
114
|
0
|
|
|
|
|
|
return $item; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
1; |