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.0.1';
4 31     31   370335 use Moo::Role;
  31         13005  
  31         279  
5 31     31   23039 use Dancer2::Core::Types;
  31         103  
  31         520  
6 31     31   472506 use Scalar::Util 'blessed';
  31         102  
  31         18201  
7              
8             with 'Dancer2::Core::Role::Engine';
9              
10             sub hook_aliases {
11             {
12 218     218 0 1442 before_serializer => 'engine.serializer.before',
13             after_serializer => 'engine.serializer.after',
14             }
15             }
16              
17 34     34 0 71 sub supported_hooks { values %{ shift->hook_aliases } }
  34         105  
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             blessed $self
53             and $self->log_cb->( core => "Failed to serialize content: $error" );
54             };
55              
56             return $data;
57             };
58              
59             around deserialize => sub {
60             my ( $orig, $self, $content, $options ) = @_;
61              
62             $content && length $content > 0
63             or return $content;
64              
65             my $data;
66             eval {
67             $data = $self->$orig($content, $options);
68             1;
69             } or do {
70             my $error = $@ || 'Zombie Error';
71             $self->log_cb->( core => "Failed to deserialize content: $error" );
72             };
73              
74             return $data;
75             };
76              
77             1;
78              
79             __END__