| 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() // []
MC/DC0%0/1
shift() // []
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 4 | ▶ | if ( ref $orig eq ref {} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | return $self->_delegate( $method => $self->where->{$value}, [$item, @_] ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 3 | ▶ | if ( ref $self->where eq ref [] ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 3 | foreach my $where ( @{ $self->where } ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 4 | my $actual_value = dpath_get($item->meta, $where->{field}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 4 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
not defined $where->{"value"} and defined $actual_value or defined $where->{"value"} and $where->{"value"} eq $actual_value
not defined $where->{"value"} and defined $actual_value
defined $where->{"value"} and $where->{"value"} eq $actual_value
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 1 | return $self->_delegate( $method => $self->otherwise, [$item, @_] ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | return $item; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||