File Coverage

blib/lib/KiokuDB/Backend/Serialize.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package KiokuDB::Backend::Serialize;
4 23     23   11120 use Moose::Role;
  23         40  
  23         158  
5              
6 23     23   92419 use Moose::Util::TypeConstraints;
  23         47  
  23         163  
7              
8 23     23   35562 use namespace::clean -except => 'meta';
  23         46  
  23         201  
9              
10             requires qw(serialize deserialize);
11              
12             my %types = (
13             storable => "KiokuDB::Serializer::Storable",
14             json => "KiokuDB::Serializer::JSON",
15             yaml => "KiokuDB::Serializer::YAML",
16             memory => "KiokuDB::Serializer::Memory",
17             );
18              
19             coerce( __PACKAGE__,
20             from Str => via {
21             my $class = $types{lc($_)};
22             Class::MOP::load_class($class);
23             $class->new;
24             },
25             from HashRef => via {
26             my %args = %$_;
27             my $class = $types{lc(delete $args{format})};
28             Class::MOP::load_class($class);
29             $class->new(%args);
30             },
31             );
32              
33             __PACKAGE__
34              
35             __END__
36              
37             =pod
38              
39             =head1 NAME
40              
41             KiokuDB::Backend::Serialize - Serialization role for backends
42              
43             =head1 SYNOPSIS
44              
45             package KiokuDB::Backend::Serialize::Foo;
46             use Moose::Role;
47              
48             use Foo;
49              
50             use namespace::clean -except => 'meta';
51              
52             with qw(KiokuDB::Backend::Serialize);
53              
54             sub serialize {
55             my ( $self, $entry ) = @_;
56              
57             Foo::serialize($entry)
58             }
59              
60             sub deserialize {
61             my ( $self, $blob ) = @_;
62              
63             Foo::deserialize($blob);
64             }
65              
66             =head1 DESCRIPTION
67              
68             This role provides provides a consistent way to use serialization modules to
69             handle backend serialization.
70              
71             See L<KiokuDB::Backend::Serialize::Storable>,
72             L<KiokuDB::Backend::Serialize::YAML> and L<KiokuDB::Backend::Serialize::JSON>
73             for examples.
74              
75             =head1 REQUIRED METHODS
76              
77             =over 4
78              
79             =item serializate $entry
80              
81             Takes a L<KiokuDB::Entry> as an argument. Should return a value suitable for
82             storage by the backend.
83              
84             =item deserialize $blob
85              
86             Takes whatever C<serializate> returned and should inflate and return a
87             L<KiokuDB::Entry>.
88              
89             =back