File Coverage

blib/lib/Articulate/Serialisation.pm
Criterion Covered Total %
statement 12 21 57.1
branch 0 6 0.0
condition n/a
subroutine 4 5 80.0
pod 1 1 100.0
total 17 33 51.5


line stmt bran cond sub pod time code
1             package Articulate::Serialisation;
2 6     6   3248 use strict;
  6         8  
  6         209  
3 6     6   26 use warnings;
  6         9  
  6         347  
4              
5 6     6   27 use Moo;
  6         11  
  6         33  
6              
7             with 'Articulate::Role::Component';
8              
9 6     6   1569 use Articulate::Syntax qw(instantiate_array);
  6         12  
  6         45  
10              
11             =head1 NAME
12              
13             Articulate::Serialisation - transform a data structure into user-facing output.
14              
15             =head1 DESCRIPTION
16              
17             use Articulate::Serialisation;
18             my $html = serialisation->serialise($response_object);
19              
20             Go through all the defined serialisers and have them attempt to serialise the response object. The first defined result will be returned. The result be of any data type, although in practice the purpose is to use a string.
21              
22             Provides a serialisation function which creates an instance of this class.
23              
24             =head1 FUNCTION
25              
26             =head3 serialisers
27              
28             This is an arrayref of serialisers, each of whom should provide serialise functions.
29              
30             =cut
31              
32             has serialisers => (
33             is => 'rw',
34             default => sub { [] },
35             coerce => sub { instantiate_array(@_) },
36             );
37              
38             =head1 FUNCTION
39              
40             =head3 serialise
41              
42             my $html = $serialisation->serialise($response_object);
43              
44             Sends to each of the C in turn. If any of them return a defined value, returns that value immediately. Otherwise, returns C.
45              
46             =cut
47              
48             sub serialise {
49 0     0 1   my $self = shift;
50 0           my $response = shift;
51              
52             # If the user has done templating themselves already, all well and good.
53 0 0         return $response unless ref $response;
54 0           my $text;
55 0           foreach my $serialiser ( @{ $self->serialisers } ) {
  0            
56 0 0         $serialiser->app( $self->app )
57             if $serialiser->can('app'); # or does Articulate::Role::Component?
58 0 0         return $text if defined( $text = $serialiser->serialise($response) );
59             }
60 0           return undef;
61             }
62              
63             1;