KiokuDB/Backend/Serialize.pm

filter: Statementstmt 80.0% 16 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 4 / 5 Podpod n/a totaltotal 80.0% 20 / 25 CC 1 SCAR 1.8
CC
1
Cov
80%
__ANON__
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
« KiokuDB/TypeMap/Entry/Ref.pm ↑ Summary KiokuDB/Error/UnknownObjects.pm »
1 #!/usr/bin/perl
2  
3 package KiokuDB::Backend::Serialize;
4 23 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 23 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 23 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 23 requires qw(serialize deserialize);
11  
12 23 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 99 ▶ my $class = $types{lc($_)};
Subroutine __ANON__: called CC=1 SCAR=0.0
22 99 Class::MOP::load_class($class);
23 99 $class->new;
24 },
25 from HashRef => via {
26 0 ▶ my %args = %$_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 0 my $class = $types{lc(delete $args{format})};
28 0 Class::MOP::load_class($class);
29 0 $class->new(%args);
30 },
31 23 );
32  
33 23 __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
« KiokuDB/TypeMap/Entry/Ref.pm ↑ Summary KiokuDB/Error/UnknownObjects.pm »