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             # ABSTRACT: Serializer for handling YAML data
2             $Dancer2::Serializer::YAML::VERSION = '0.400000';
3             use Moo;
4 5     5   3743 use Carp 'croak';
  5         12  
  5         38  
5 5     5   1735 use Encode;
  5         12  
  5         290  
6 5     5   25 use Module::Runtime 'use_module';
  5         10  
  5         358  
7 5     5   29 use Sub::Defer;
  5         9  
  5         36  
8 5     5   247  
  5         9  
  5         1454  
9             with 'Dancer2::Core::Role::Serializer';
10              
11             has '+content_type' => ( default => sub {'text/x-yaml'} );
12              
13             # deferred helpers. These are called as class methods, but need to
14             # ensure YAML is loaded.
15              
16             my $_from_yaml = defer_sub 'Dancer2::Serializer::YAML::from_yaml' => sub {
17             use_module('YAML');
18             sub { __PACKAGE__->deserialize(@_) };
19 4     4   248 };
20              
21             my $_to_yaml = defer_sub 'Dancer2::Serializer::YAML::to_yaml' => sub {
22             use_module('YAML');
23             sub { __PACKAGE__->serialize(@_) };
24 11     11   360 };
25              
26             # class definition
27              
28             my ( $self, $entity ) = @_;
29             encode('UTF-8', YAML::Dump($entity));
30             }
31              
32             my ( $self, $content ) = @_;
33             YAML::Load(decode('UTF-8', $content));
34             }
35              
36             1;
37              
38              
39             =pod
40              
41             =encoding UTF-8
42              
43             =head1 NAME
44              
45             Dancer2::Serializer::YAML - Serializer for handling YAML data
46              
47             =head1 VERSION
48              
49             version 0.400000
50              
51             =head1 DESCRIPTION
52              
53             This is a serializer engine that allows you to turn Perl data structures into
54             YAML output and vice-versa.
55              
56             =head1 ATTRIBUTES
57              
58             =head2 content_type
59              
60             Returns 'text/x-yaml'
61              
62             =head1 METHODS
63              
64             =head2 serialize($content)
65              
66             Serializes a data structure to a YAML structure.
67              
68             =head2 deserialize($content)
69              
70             Deserializes a YAML structure to a data structure.
71              
72             =head1 FUNCTIONS
73              
74             =head2 fom_yaml($content)
75              
76             This is an helper available to transform a YAML data structure to a Perl data structures.
77              
78             =head2 to_yaml($content)
79              
80             This is an helper available to transform a Perl data structure to YAML.
81              
82             Calling this function will B<not> trigger the serialization's hooks.
83              
84             =head1 AUTHOR
85              
86             Dancer Core Developers
87              
88             =head1 COPYRIGHT AND LICENSE
89              
90             This software is copyright (c) 2022 by Alexis Sukrieh.
91              
92             This is free software; you can redistribute it and/or modify it under
93             the same terms as the Perl 5 programming language system itself.
94              
95             =cut