File Coverage

blib/lib/Dancer2/Core/Role/Serializer.pm
Criterion Covered Total %
statement 12 13 92.3
branch n/a
condition n/a
subroutine 5 6 83.3
pod 0 2 0.0
total 17 21 80.9


line stmt bran cond sub pod time code
1             package Dancer2::Core::Role::Serializer;
2             # ABSTRACT: Role for Serializer engines
3             $Dancer2::Core::Role::Serializer::VERSION = '2.1.0';
4 32     32   488415 use Moo::Role;
  32         20397  
  32         301  
5 32     32   22775 use Dancer2::Core::Types;
  32         372  
  32         670  
6 32     32   471261 use Scalar::Util 'blessed';
  32         78  
  32         19619  
7              
8             with 'Dancer2::Core::Role::Engine';
9              
10             sub hook_aliases {
11             {
12 226     226 0 1552 before_serializer => 'engine.serializer.before',
13             after_serializer => 'engine.serializer.after',
14             }
15             }
16              
17 37     37 0 82 sub supported_hooks { values %{ shift->hook_aliases } }
  37         135  
18              
19 0     0     sub _build_type {'Serializer'}
20              
21             requires 'serialize';
22             requires 'deserialize';
23              
24             has log_cb => (
25             is => 'ro',
26             isa => CodeRef,
27             default => sub { sub {1} },
28             );
29              
30             has content_type => (
31             is => 'ro',
32             isa => Str,
33             required => 1,
34             writer => 'set_content_type'
35             );
36              
37             around serialize => sub {
38             my ( $orig, $self, $content, $options ) = @_;
39              
40             blessed $self && $self->execute_hook( 'engine.serializer.before', $content );
41              
42             $content or return $content;
43              
44             my $data;
45             eval {
46             $data = $self->$orig( $content, $options );
47             blessed $self
48             and $self->execute_hook( 'engine.serializer.after', $data );
49             1;
50             } or do {
51             my $error = $@ || 'Zombie Error';
52             if ( blessed($self) && $self->config->{strict_utf8} ) {
53             die $error;
54             }
55             blessed $self
56             and $self->log_cb->( core => "Failed to serialize content: $error" );
57             };
58              
59             return $data;
60             };
61              
62             around deserialize => sub {
63             my ( $orig, $self, $content, $options ) = @_;
64              
65             $content && length $content > 0
66             or return $content;
67              
68             my $data;
69             eval {
70             $data = $self->$orig($content, $options);
71             1;
72             } or do {
73             my $error = $@ || 'Zombie Error';
74             $self->log_cb->( core => "Failed to deserialize content: $error" );
75             };
76              
77             return $data;
78             };
79              
80             1;
81              
82             __END__