Serializer/Storable.pm

filter: Statementstmt 100% 30 / 30 Branchbran 75.0% 6 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 95.9% 47 / 49 CC 5 SCAR 5.8
CC
5
Cov
95%
__ANON__
3.1
get_freezer
2.0
__ANON__
2.0
CRAP 1.8
« Serializer.pm ↑ Summary Serializer/JSON.pm »
1 package DBIx::Class::InflateColumn::Serializer::Storable;
2 {
3 3 $DBIx::Class::InflateColumn::Serializer::Storable::VERSION = '0.06';
4 }
5  
6 =head1 NAME
7  
8 DBIx::Class::InflateColumn::Serializer::Storable - Storable Inflator
9  
10 =head1 SYNOPSIS
11  
12 package MySchema::Table;
13 use base 'DBIx::Class';
14  
15 __PACKAGE__->load_components('InflateColumn::Serializer', 'Core');
16 __PACKAGE__->add_columns(
17 'data_column' => {
18 'data_type' => 'VARCHAR',
19 'size' => 255,
20 'serializer_class' => 'Storable'
21 }
22 );
23  
24 Then in your code...
25  
26 my $struct = { 'I' => { 'am' => 'a struct' };
27 $obj->data_column($struct);
28 $obj->update;
29  
30 And you can recover your data structure with:
31  
32 my $obj = ...->find(...);
33 my $struct = $obj->data_column;
34  
35 The data structures you assign to "data_column" will be saved in the database in Storable format.
36  
37 =cut
38  
39 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 3 ▶use Storable qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 =over 4
45  
46 =item get_freezer
47  
48 Called by DBIx::Class::InflateColumn::Serializer to get the routine that serializes
49 the data passed to it. Returns a coderef.
50  
51 =cut
52  
53 sub get_freezer{
54 6 ▶ my ($class, $column, $info, $args) = @_;
Subroutine get_freezer: called CC=2 SCAR=6.9
Pod documented
55  
56 6 ▶ if (defined $info->{'size'}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info->{'size'}) { } 3 3 6
57 3 my $size = $info->{'size'};
58 return sub {
59 2 ▶ my $s = Storable::nfreeze(shift);
Subroutine __ANON__: called CC=2 SCAR=6.9
60 2 ▶ croak "serialization too big" if (length($s) > $size);
Branch: 1 branch
BranchTrueFalseTotal
if length $s > $size 1 1 2
61 1 return $s;
62 3 };
63 } else {
64 return sub {
65 2 ▶ return Storable::nfreeze(shift);
Subroutine __ANON__: called CC=1 SCAR=0.0
66 3 };
67 }
68 }
69  
70 =item get_unfreezer
71  
72 Called by DBIx::Class::InflateColumn::Serializer to get the routine that deserializes
73 the data stored in the column. Returns a coderef.
74  
75 =back
76  
77 =cut
78  
79  
80 sub get_unfreezer {
81 return sub {
82 3 ▶ my $value = shift;
Subroutine __ANON__: called CC=3 SCAR=11.3
83 # Storable returns undef if the datastructure couldn't be thawed.
84 # Other deserializers throw exceptions, so we'll do the same.
85 # If the column had a NULL value, then we return it (don't want to die)
86 3 ▶ return undef if (not defined $value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 3 3
87 3 my $s = Storable::thaw($value);
88 3 ▶ croak "Storable couldn't thaw the value" if (not defined $s);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 3 3
89 3 return $s;
90 6 ▶ };
Subroutine get_unfreezer: called CC=1 SCAR=0.0
Pod documented
91 }
92  
93  
94 3 1;
« Serializer.pm ↑ Summary Serializer/JSON.pm »