| 1 | package Articulate::Flow::ContentType; | ||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | with 'Articulate::Role::Flow'; | |||||||||||
| 6 | 1 | ▶ | use Articulate::Syntax qw (instantiate instantiate_array); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | =head1 NAME | ||||||||||||
| 9 | |||||||||||||
| 10 | Articulate::Flow::ContentType - case switching for content_type | ||||||||||||
| 11 | |||||||||||||
| 12 | =head1 CONFIGURATION | ||||||||||||
| 13 | |||||||||||||
| 14 | - class: Articulate::Flow::ContentType | ||||||||||||
| 15 | args: | ||||||||||||
| 16 | where: | ||||||||||||
| 17 | 'text/markdown': | ||||||||||||
| 18 | - Articulate::Enrichment::Markdown | ||||||||||||
| 19 | otherwise: | ||||||||||||
| 20 | - SomeOther::Enrichment | ||||||||||||
| 21 | |||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||
| 23 | |||||||||||||
| 24 | 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 what type of content is stored in the item. | ||||||||||||
| 25 | |||||||||||||
| 26 | Rather than having to write a 'black box' provider every time, this class provides a standard way of doing it. | ||||||||||||
| 27 | |||||||||||||
| 28 | =head1 METHODS | ||||||||||||
| 29 | |||||||||||||
| 30 | =head3 enrich | ||||||||||||
| 31 | |||||||||||||
| 32 | $self->enrich( $item, $request ); | ||||||||||||
| 33 | $self->process_method( enrich => $item, $request ); | ||||||||||||
| 34 | |||||||||||||
| 35 | =head3 augment | ||||||||||||
| 36 | |||||||||||||
| 37 | $self->augment( $item, $request ); | ||||||||||||
| 38 | $self->process_method( augment => $item, $request ); | ||||||||||||
| 39 | |||||||||||||
| 40 | =head3 process_method | ||||||||||||
| 41 | |||||||||||||
| 42 | $self->process_method( $verb, $item, $request ); | ||||||||||||
| 43 | |||||||||||||
| 44 | Goes through each of the keys of C<< $self->where >>; if the key is equal to the C<content_type> of C<$item>, then instantiates the value of that key and performs the same verb on the arguments. | ||||||||||||
| 45 | |||||||||||||
| 46 | If none of the where clauses matched, the otherwise provider, if one is specified, will be used. | ||||||||||||
| 47 | |||||||||||||
| 48 | An item's C<content_type> is retrieved from C<meta.schema.core.content_type>. | ||||||||||||
| 49 | |||||||||||||
| 50 | =cut | ||||||||||||
| 51 | |||||||||||||
| 52 | has where => ( | ||||||||||||
| 53 | is => 'rw', | ||||||||||||
| 54 | 0 | ▶ | default => sub { {} }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 55 | coerce => sub { | ||||||||||||
| 56 | 0 | ▶ | my $orig = shift // {}; | ||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Condition0%0/2
shift() // {}
MC/DC0%0/1
shift() // {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 57 | 0 | foreach my $type ( keys %$orig ){ | |||||||||||
| 58 | 0 | $orig->{$type} = instantiate_array ( $orig->{$type} ); | |||||||||||
| 59 | } | ||||||||||||
| 60 | 0 | return $orig; | |||||||||||
| 61 | }, | ||||||||||||
| 62 | 1 | ); | |||||||||||
| 63 | |||||||||||||
| 64 | has otherwise => ( | ||||||||||||
| 65 | is => 'rw', | ||||||||||||
| 66 | 0 | ▶ | default => sub { [] }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 67 | coerce => sub { | ||||||||||||
| 68 | 0 | ▶ | instantiate_array(@_) | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 69 | }, | ||||||||||||
| 70 | 1 | ); | |||||||||||
| 71 | |||||||||||||
| 72 | sub process_method { | ||||||||||||
| 73 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
process_method: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||
| 74 | 0 | my $method = shift; | |||||||||||
| 75 | 0 | my $item = shift; | |||||||||||
| 76 | 0 | my $content_type = $item->meta->{schema}->{core}->{content_type}; | |||||||||||
| 77 | 0 | ▶ | if (defined $content_type) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | 0 | foreach my $type ( keys %{$self->where} ) { | |||||||||||
| 79 | 0 | ▶ | if ($type eq $content_type) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 80 | 0 | return $self->_delegate( $method => $self->where->{$type}, [$item, @_] ); | |||||||||||
| 81 | } | ||||||||||||
| 82 | } | ||||||||||||
| 83 | } | ||||||||||||
| 84 | 0 | ▶ | if (defined $self->otherwise) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 85 | 0 | return $self->_delegate( $method => $self->otherwise, [$item, @_] ); | |||||||||||
| 86 | } | ||||||||||||
| 87 | 0 | return $item; | |||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | 1 | 1; | |||||||||||