File Coverage

blib/lib/Dancer/Serializer/YAML.pm
Criterion Covered Total %
statement 51 51 100.0
branch 5 8 62.5
condition 6 6 100.0
subroutine 16 16 100.0
pod 4 7 57.1
total 82 88 93.1


line stmt bran cond sub pod time code
1             package Dancer::Serializer::YAML;
2             our $AUTHORITY = 'cpan:SUKRIA';
3             #ABSTRACT: serializer for handling YAML data
4             $Dancer::Serializer::YAML::VERSION = '1.3514_04'; # TRIAL
5             $Dancer::Serializer::YAML::VERSION = '1.351404';
6 166     166   1353 use strict;
  166         338  
  166         4030  
7 166     166   743 use warnings;
  166         300  
  166         3323  
8 166     166   807 use Carp;
  166         309  
  166         8195  
9 166     166   1006 use Dancer::ModuleLoader;
  166         388  
  166         3570  
10 166     166   802 use Dancer::Config;
  166         378  
  166         6082  
11 166     166   996 use Dancer::Exception qw(:all);
  166         358  
  166         21922  
12 166     166   1033 use base 'Dancer::Serializer::Abstract';
  166         316  
  166         54843  
13              
14             # helpers
15              
16             sub from_yaml {
17 8     8 0 20 my ($yaml) = @_;
18 8         42 my $s = Dancer::Serializer::YAML->new;
19 8         28 $s->deserialize($yaml);
20             }
21              
22             sub to_yaml {
23 4     4 0 11 my ($data) = @_;
24 4         39 my $s = Dancer::Serializer::YAML->new;
25 4         34 $s->serialize($data);
26             }
27              
28             # class definition
29              
30             sub loaded {
31 16   100 16 0 51 my $module = Dancer::Config::settings->{engines}{YAML}{module} || 'YAML';
32              
33 16 50       90 raise core_serializer => q{Dancer::Serializer::YAML only supports 'YAML' or 'YAML::XS', not $module}
34             unless $module =~ /^YAML(?:::XS)?$/;
35              
36 16 50       80 Dancer::ModuleLoader->load($module)
37             or raise core_serializer => "$module is needed and is not installed";
38             }
39              
40             sub init {
41 16     16 1 40 my ($self) = @_;
42 16         43 $self->loaded;
43             }
44              
45             sub serialize {
46 12     12 1 1613 my ($self, $entity) = @_;
47 12 50       33 return unless $entity;
48 12   100     37 my $module = Dancer::Config::settings->{engines}{YAML}{module} || 'YAML';
49             {
50 166     166   1221 no strict 'refs';
  166         357  
  166         17632  
  12         24  
51 12         18 &{ $module . '::Dump' }($entity);
  12         576  
52             }
53             }
54              
55             sub deserialize {
56 12     12 1 1152 my ($self, $content) = @_;
57 12   100     37 my $module = Dancer::Config::settings->{engines}{YAML}{module} || 'YAML';
58 12 100       39 return unless $content;
59             {
60 166     166   1049 no strict 'refs';
  166         376  
  166         12689  
  10         14  
61 10         27 &{ $module . '::Load' }($content);
  10         405  
62             }
63             }
64              
65 6     6 1 1343 sub content_type {'text/x-yaml'}
66              
67             1;
68              
69             __END__