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.1.0';
4 6     6   339480 use Moo;
  6         11722  
  6         51  
5 6     6   5677 use Carp 'croak';
  6         16  
  6         524  
6 6     6   309 use Encode;
  6         14  
  6         705  
7 6     6   685 use Module::Runtime 'use_module';
  6         2529  
  6         68  
8 6     6   929 use Sub::Defer;
  6         2746  
  6         2468  
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   320 sub { __PACKAGE__->deserialize(@_) };
20             };
21              
22             my $_to_yaml = defer_sub 'Dancer2::Serializer::YAML::to_yaml' => sub {
23             use_module('YAML');
24 11     11   617 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__