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.3520';
5 165     165   1211 use strict;
  165         390  
  165         4973  
6 165     165   935 use warnings;
  165         442  
  165         3833  
7 165     165   884 use Carp;
  165         404  
  165         8938  
8 165     165   1236 use Dancer::ModuleLoader;
  165         417  
  165         4342  
9 165     165   1027 use Dancer::Config;
  165         413  
  165         7426  
10 165     165   1173 use Dancer::Exception qw(:all);
  165         513  
  165         20520  
11 165     165   1276 use base 'Dancer::Serializer::Abstract';
  165         405  
  165         67960  
12              
13             # helpers
14              
15             sub from_yaml {
16 8     8 0 23 my ($yaml) = @_;
17 8         54 my $s = Dancer::Serializer::YAML->new;
18 8         29 $s->deserialize($yaml);
19             }
20              
21             sub to_yaml {
22 4     4 0 13 my ($data) = @_;
23 4         55 my $s = Dancer::Serializer::YAML->new;
24 4         17 $s->serialize($data);
25             }
26              
27             # class definition
28              
29             sub loaded {
30 16   100 16 0 54 my $module = Dancer::Config::settings->{engines}{YAML}{module} || 'YAML';
31              
32 16 50       102 raise core_serializer => q{Dancer::Serializer::YAML only supports 'YAML' or 'YAML::XS', not $module}
33             unless $module =~ /^YAML(?:::XS)?$/;
34              
35 16 50       73 Dancer::ModuleLoader->load($module)
36             or raise core_serializer => "$module is needed and is not installed";
37             }
38              
39             sub init {
40 16     16 1 40 my ($self) = @_;
41 16         45 $self->loaded;
42             }
43              
44             sub serialize {
45 12     12 1 1976 my ($self, $entity) = @_;
46 12 50       34 return unless $entity;
47 12   100     36 my $module = Dancer::Config::settings->{engines}{YAML}{module} || 'YAML';
48             {
49 165     165   1459 no strict 'refs';
  165         446  
  165         22102  
  12         19  
50 12         23 &{ $module . '::Dump' }($entity);
  12         506  
51             }
52             }
53              
54             sub deserialize {
55 12     12 1 1740 my ($self, $content) = @_;
56 12   100     30 my $module = Dancer::Config::settings->{engines}{YAML}{module} || 'YAML';
57 12 100       45 return unless $content;
58             {
59 165     165   1306 no strict 'refs';
  165         421  
  165         16289  
  10         18  
60 10         18 &{ $module . '::Load' }($content);
  10         415  
61             }
62             }
63              
64 6     6 1 1659 sub content_type {'text/x-yaml'}
65              
66             1;
67              
68             __END__