Serializer.pm

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