File Coverage

blib/lib/DBIx/Class/InflateColumn/Serializer.pm
Criterion Covered Total %
statement 18 18 100.0
branch 5 8 62.5
condition n/a
subroutine 3 3 100.0
pod 0 1 0.0
total 26 30 86.6


line stmt bran cond sub pod time code
1             package DBIx::Class::InflateColumn::Serializer;
2             $DBIx::Class::InflateColumn::Serializer::VERSION = '0.09';
3 4     4   319403 use strict;
  4         8  
  4         99  
4 4     4   18 use warnings;
  4         6  
  4         871  
5              
6             sub register_column {
7 30     30 0 284207 my $self = shift;
8 30         43 my ($column, $info, $args) = @_;
9 30         111 $self->next::method(@_);
10              
11 30 100       6181 return unless defined $info->{'serializer_class'};
12              
13              
14 21         45 my $class = "DBIx::Class::InflateColumn::Serializer::$info->{'serializer_class'}";
15 21         1297 eval "require ${class};";
16 21 50       93 $self->throw_exception("Failed to use serializer_class '${class}': $@") if $@;
17              
18 21 50       24 defined( my $freezer = eval{ $class->get_freezer($column, $info, $args) }) ||
  21         88  
19             $self->throw_exception("Failed to create freezer with class '$class': $@");
20 21 50       27 defined( my $unfreezer = eval{ $class->get_unfreezer($column, $info, $args) }) ||
  21         58  
21             $self->throw_exception("Failed to create unfreezer with class '$class': $@");
22              
23 21         447 $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             1;
121