File Coverage

blib/lib/DBIx/Class/InflateColumn/Serializer.pm
Criterion Covered Total %
statement 6 18 33.3
branch 0 8 0.0
condition n/a
subroutine 2 3 66.6
pod 0 1 0.0
total 8 30 26.6


line stmt bran cond sub pod time code
1             package DBIx::Class::InflateColumn::Serializer;
2             {
3             $DBIx::Class::InflateColumn::Serializer::VERSION = '0.07';
4             }
5              
6 1     1   14742 use strict;
  1         2  
  1         31  
7 1     1   3 use warnings;
  1         1  
  1         184  
8              
9             sub register_column {
10 0     0 0   my $self = shift;
11 0           my ($column, $info, $args) = @_;
12 0           $self->next::method(@_);
13              
14 0 0         return unless defined $info->{'serializer_class'};
15              
16              
17 0           my $class = "DBIx::Class::InflateColumn::Serializer::$info->{'serializer_class'}";
18 0           eval "require ${class};";
19 0 0         $self->throw_exception("Failed to use serializer_class '${class}': $@") if $@;
20              
21 0 0         defined( my $freezer = eval{ $class->get_freezer($column, $info, $args) }) ||
  0            
22             $self->throw_exception("Failed to create freezer with class '$class': $@");
23 0 0         defined( my $unfreezer = eval{ $class->get_unfreezer($column, $info, $args) }) ||
  0            
24             $self->throw_exception("Failed to create unfreezer with class '$class': $@");
25              
26 0           $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             Karen Etheridge
114              
115             =head1 SEE ALSO
116              
117             DBIx::Class, DBIx::Class::Manual::FAQ
118              
119             =cut
120              
121             #################### main pod documentation end ###################
122              
123             1;
124