Serializer.pm

filter: Statementstmt 100% 21 / 21 Branchbran 62.5% 5 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod 0.0% 0 / 1 totaltotal 87.8% 29 / 33 CC 5 SCAR 13.7
CC
5
Cov
90%
register_column
5.1
BEGIN
1.0
BEGIN
1.0
CRAP 3.9
↑ Summary Serializer/Storable.pm »
1 package DBIx::Class::InflateColumn::Serializer;
2 {
3 4 $DBIx::Class::InflateColumn::Serializer::VERSION = '0.06';
4 }
5  
6 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 sub register_column {
10 27 ▶ my $self = shift;
Subroutine register_column: called CC=5 SCAR=16.3
Pod undocumented
11 27 my ($column, $info, $args) = @_;
12 27 $self->next::method(@_);
13  
14 27 ▶ return unless defined $info->{'serializer_class'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info->{"serializer_class"} 9 18 27
15  
16  
17 18 my $class = "DBIx::Class::InflateColumn::Serializer::$info->{'serializer_class'}";
18 18 eval "require ${class};";
19 18 ▶ $self->throw_exception("Failed to use serializer_class '${class}': $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 18 18
20  
21 18 ▶ defined( my $freezer = eval{ $class->get_freezer($column, $info, $args) }) ||
Branch: 1 branch
BranchTrueFalseTotal
defined(my $freezer = eval { do { $class->get_freezer($column, $info, $args) } }) or $self->throw_exception("Failed to create freezer with class '${class}': $@") 0 18 18
22 $self->throw_exception("Failed to create freezer with class '$class': $@");
23 18 ▶ defined( my $unfreezer = eval{ $class->get_unfreezer($column, $info, $args) }) ||
Branch: 1 branch
BranchTrueFalseTotal
defined(my $unfreezer = eval { do { $class->get_unfreezer($column, $info, $args) } }) or $self->throw_exception("Failed to create unfreezer with class '${class}': $@") 0 18 18
24 $self->throw_exception("Failed to create unfreezer with class '$class': $@");
25  
26 18 $self->inflate_column(
27 $column => {
28 inflate => $unfreezer,
29 deflate => $freezer,
30 }
31 );
32 };
33  
34 =head1 NAME
35  
36 DBIx::Class::InflateColumn::Serializer - Inflators to serialize data structures for DBIx::Class
37  
38 =head1 SYNOPSIS
39  
40 package MySchema::Table;
41 use base 'DBIx::Class';
42  
43 __PACKAGE__->load_components('InflateColumn::Serializer', 'Core');
44 __PACKAGE__->add_columns(
45 'data_column' => {
46 'data_type' => 'VARCHAR',
47 'size' => 255,
48 'serializer_class' => 'JSON'
49 }
50 );
51  
52 Then in your code...
53  
54 my $struct = { 'I' => { 'am' => 'a struct' };
55 $obj->data_column($struct);
56 $obj->update;
57  
58 And you can recover your data structure with:
59  
60 my $obj = ...->find(...);
61 my $struct = $obj->data_column;
62  
63 The data structures you assign to "data_column" will be saved in the database in JSON format.
64  
65 =head1 DESCRIPTION
66  
67 These modules help you store and access serialized data structures in the columns of your DB from your DBIx::Classes. They are inspired from the DBIx::Class::Manual::FAQ and the DBIC test suite, and provide a bit more protection than the inflators proposed in the FAQ. The intention is to provide a suite of well proven and reusable inflators and deflators to complement DBIx::Class.
68  
69 Added features for these inflators are:
70 - throw an exception if the serialization doesn't fit in the field
71 - throw an exception if the deserialization results in an error
72  
73 Right now there are three serializers:
74 - Storable
75 - JSON
76 - YAML
77  
78 =head1 USAGE
79  
80 1. Choose your serializer: JSON, YAML or Storable
81  
82 2. Add 'InflateColumn::Serializer' into the load_components of your table class
83  
84 3. add 'serializer_class' => SERIALIZER to the properties of the column that you want to (de/i)nflate
85 with the SERIALIZER class.
86  
87 =head1 NOTES
88  
89 As stated in the DBIC FAQ: "Be careful not to overuse this capability, however. If you find yourself depending more and more on some data within the inflated column, then it may be time to factor that data out."
90  
91 =head1 AUTHOR
92  
93 Jose Luis Martinez
94 CPAN ID: JLMARTIN
95 CAPSiDE
96 jlmartinez@capside.com
97 http://www.pplusdomain.net
98  
99 =head1 COPYRIGHT
100  
101 This program is free software; you can redistribute
102 it and/or modify it under the same terms as Perl itself.
103  
104 The full text of the license can be found in the
105 LICENSE file included with this module.
106  
107 =head1 THANKS
108  
109 Matt S Trout for his valuable feedback
110  
111 Ask Bjorn Hansen
112  
113 =head1 SEE ALSO
114  
115 DBIx::Class, DBIx::Class::Manual::FAQ
116  
117 =cut
118  
119 #################### main pod documentation end ###################
120  
121 4 1;
122  
↑ Summary Serializer/Storable.pm »