File Coverage

blib/lib/Articulate/Flow/ContentType.pm
Criterion Covered Total %
statement 12 24 50.0
branch 0 6 0.0
condition n/a
subroutine 4 5 80.0
pod 1 1 100.0
total 17 36 47.2


line stmt bran cond sub pod time code
1             package Articulate::Flow::ContentType;
2 1     1   8039 use strict;
  1         4  
  1         49  
3 1     1   6 use warnings;
  1         2  
  1         41  
4 1     1   6 use Moo;
  1         2  
  1         10  
5             with 'Articulate::Role::Flow';
6 1     1   396 use Articulate::Syntax qw (instantiate instantiate_array);
  1         3  
  1         9  
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 and C, 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 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 is retrieved from C.
49              
50             =cut
51              
52             has where => (
53             is => 'rw',
54             default => sub { {} },
55             coerce => sub {
56             my $orig = shift // {};
57             foreach my $type ( keys %$orig ) {
58             $orig->{$type} = instantiate_array( $orig->{$type} );
59             }
60             return $orig;
61             },
62             );
63              
64             has otherwise => (
65             is => 'rw',
66             default => sub { [] },
67             coerce => sub {
68             instantiate_array(@_);
69             },
70             );
71              
72             sub process_method {
73 0     0 1   my $self = shift;
74 0           my $method = shift;
75 0           my $item = shift;
76 0           my $content_type = $item->meta->{schema}->{core}->{content_type};
77 0 0         if ( defined $content_type ) {
78 0           foreach my $type ( keys %{ $self->where } ) {
  0            
79 0 0         if ( $type eq $content_type ) {
80 0           return $self->_delegate( $method => $self->where->{$type},
81             [ $item, @_ ] );
82             }
83             }
84             }
85 0 0         if ( defined $self->otherwise ) {
86 0           return $self->_delegate( $method => $self->otherwise, [ $item, @_ ] );
87             }
88 0           return $item;
89             }
90              
91             1;