blib/lib/DBIx/Class/InflateColumn/Serializer/CompressJSON.pm

filter: Statementstmt 95.1% 39 / 41 Branchbran 71.4% 10 / 14 Conditioncond 100% 7 / 7 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 92.2% 71 / 77 CC 11 SCAR 12.5
CC
11
Cov
90%
__ANON__
6.1
__ANON__
4.2
get_unfreezer
2.0
CRAP 3.5
↑ Summary
1 #
2 # This file is part of DBIx-Class-InflateColumn-Serializer-CompressJSON
3 #
4 # This software is copyright (c) 2012 by Weborama. No
5 # license is granted to other entities.
6 #
7 package DBIx::Class::InflateColumn::Serializer::CompressJSON;
8 {
9 4 $DBIx::Class::InflateColumn::Serializer::CompressJSON::VERSION = '0.004';
10 }
11  
12 # ABSTRACT: DBIx::Class::InflateColumn::Serializer::CompressJSON - JSON compressed Inflator
13  
14 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use JSON qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Compress::Zlib qw/compress uncompress/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 ▶sub get_freezer {
Subroutine get_freezer: called CC=2 SCAR=7.0
Pod documented
21 4 my ( $class, $column, $info, $args ) = @_;
22  
23 4 my $size = $info->{'size'};
24 4 ▶ my $compress_method = $info->{compress_method} || 'zlib';
Condition100%2/2
$info->{'compress_method'} || 'zlib'
l!l
22
MC/DC100%1/1
$info->{'compress_method'} || 'zlib'
$info->{'compress_method'}
Truth table100%2/2
A
A= $info->{'compress_method'}
Aresult
11
00
25  
26 6 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=18.1
27 6 my $b;
28  
29 6 ▶ if ( $compress_method eq 'zlib' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($compress_method eq 'zlib') { } 5 1 6
elsif ($compress_method eq 'mysql') { } 1 0 1
30 5 $b = compress( JSON::to_json(shift) );
31 }
32 elsif ( $compress_method eq 'mysql' ) {
33 1 my $json = JSON::to_json(shift);
34 1 $b = pack( 'L', length($json) ) . compress($json);
35 }
36 else {
37 0 croak "Unknown compress method: '$compress_method'";
38 }
39  
40 # check for known errors
41 6 ▶ croak "could not get a compressed binary" unless defined $b;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 0 6 6
42 6 ▶ croak "serialization too big"
Branch: 1 branch
BranchTrueFalseTotal
if defined $size and length $b > $size 1 5 6
Condition100%3/3
defined $size and length $b > $size
!ll&&!rl&&r
231
MC/DC100%2/2
defined $size and length $b > $size
defined $size length $b > $size
Truth table100%3/3
A and B
A= defined $sizeB= length $b > $size
ABresult
0X0
100
111
43 if defined $size && ( length($b) > $size );
44 5 return $b;
45 4 };
46 }
47  
48 4 ▶sub get_unfreezer {
Subroutine get_unfreezer: called CC=2 SCAR=7.0
Pod documented
49 4 my ( $class, $column, $info, $args ) = @_;
50  
51 4 ▶ my $compress_method = $info->{compress_method} || 'zlib';
Condition100%2/2
$info->{'compress_method'} || 'zlib'
l!l
22
MC/DC100%1/1
$info->{'compress_method'} || 'zlib'
$info->{'compress_method'}
Truth table100%2/2
A
A= $info->{'compress_method'}
Aresult
11
00
52  
53 4 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=14.3
54 4 my $j;
55
56 4 ▶ if ($compress_method eq 'zlib') {
Branch: 2 branches
BranchTrueFalseTotal
if ($compress_method eq 'zlib') { } 3 1 4
elsif ($compress_method eq 'mysql') { } 1 0 1
57 3 $j = uncompress(shift);
58 }
59 elsif ( $compress_method eq 'mysql' ) {
60 1 $j = uncompress(substr(shift,4));
61 }
62 else {
63 0 croak "Unknown compress method: '$compress_method'";
64 }
65
66 4 ▶ croak "could not get an uncompressed scalar" unless defined( $j );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $j 0 4 4
67 4 return JSON::from_json($j);
68 4 };
69 }
70  
71  
72 4 1;
73  
74  
75 =pod
76  
77 =head1 NAME
78  
79 DBIx::Class::InflateColumn::Serializer::CompressJSON - DBIx::Class::InflateColumn::Serializer::CompressJSON - JSON compressed Inflator
80  
81 =head1 VERSION
82  
83 version 0.001
84  
85 =head1 NAME
86  
87 DBIx::Class::InflateColumn::Serializer::JSON - CompressJSON Inflator
88 =head1 SYNOPSIS
89  
90 package MySchema::Table;
91 use base 'DBIx::Class';
92  
93 __PACKAGE__->load_components('InflateColumn::Serializer', 'Core');
94 __PACKAGE__->add_columns(
95 'data_column' => {
96 'data_type' => 'VARCHAR',
97 'size' => 255,
98 'serializer_class' => 'CompressJSON'
99 }
100 );
101  
102 Then in your code...
103  
104 my $struct = { 'I' => { 'am' => 'a struct' };
105 $obj->data_column($struct);
106 $obj->update;
107  
108 And you can recover your data structure with:
109  
110 my $obj = ...->find(...);
111 my $struct = $obj->data_column;
112  
113 The data structures you assign to "data_column" will be saved in the database in JSON format.
114  
115 =over 4
116  
117 =item get_freezer
118  
119 Called by DBIx::Class::InflateColumn::Serializer to get the routine that serializes
120 the data passed to it. Returns a coderef.
121  
122 =item get_unfreezer
123  
124 Called by DBIx::Class::InflateColumn::Serializer to get the routine that deserializes
125 the data stored in the column. Returns a coderef.
126  
127 =back
128  
129 =head1 AUTHOR
130  
131 Baptiste FOSSÉ <baptiste@weborama.com>
132  
133 =head1 COPYRIGHT AND LICENSE
134  
135 This software is copyright (c) 2012 by Weborama. No
136 license is granted to other entities.
137  
138 =cut
139  
140  
141 __END__
↑ Summary