File Coverage

blib/lib/Dancer2/Serializer/YAML.pm
Criterion Covered Total %
statement 17 17 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 24 24 100.0


line stmt bran cond sub pod time code
1             package Dancer2::Serializer::YAML;
2             # ABSTRACT: Serializer for handling YAML data
3             $Dancer2::Serializer::YAML::VERSION = '2.0.1';
4 6     6   199657 use Moo;
  6         7663  
  6         63  
5 6     6   4654 use Carp 'croak';
  6         15  
  6         602  
6 6     6   48 use Encode;
  6         29  
  6         625  
7 6     6   469 use Module::Runtime 'use_module';
  6         1568  
  6         65  
8 6     6   754 use Sub::Defer;
  6         1693  
  6         2235  
9              
10             with 'Dancer2::Core::Role::Serializer';
11              
12             has '+content_type' => ( default => sub {'text/x-yaml'} );
13              
14             # deferred helpers. These are called as class methods, but need to
15             # ensure YAML is loaded.
16              
17             my $_from_yaml = defer_sub 'Dancer2::Serializer::YAML::from_yaml' => sub {
18             use_module('YAML');
19 4     4   294 sub { __PACKAGE__->deserialize(@_) };
20             };
21              
22             my $_to_yaml = defer_sub 'Dancer2::Serializer::YAML::to_yaml' => sub {
23             use_module('YAML');
24 11     11   533 sub { __PACKAGE__->serialize(@_) };
25             };
26              
27             # class definition
28             sub BUILD { use_module('YAML') }
29              
30             sub serialize {
31             my ( $self, $entity ) = @_;
32             encode('UTF-8', YAML::Dump($entity));
33             }
34              
35             sub deserialize {
36             my ( $self, $content ) = @_;
37             YAML::Load(decode('UTF-8', $content));
38             }
39              
40             1;
41              
42             __END__