File Coverage

blib/lib/DBIx/Class/InflateColumn/Serializer/YAML.pm
Criterion Covered Total %
statement 23 23 100.0
branch 4 4 100.0
condition n/a
subroutine 9 9 100.0
pod 2 2 100.0
total 38 38 100.0


line stmt bran cond sub pod time code
1             package DBIx::Class::InflateColumn::Serializer::YAML;
2             $DBIx::Class::InflateColumn::Serializer::YAML::VERSION = '0.09';
3             =head1 NAME
4              
5             DBIx::Class::InflateColumn::Serializer::YAML - YAML Inflator
6              
7             =head1 SYNOPSIS
8              
9             package MySchema::Table;
10             use base 'DBIx::Class';
11              
12             __PACKAGE__->load_components('InflateColumn::Serializer', 'Core');
13             __PACKAGE__->add_columns(
14             'data_column' => {
15             'data_type' => 'VARCHAR',
16             'size' => 255,
17             'serializer_class' => 'YAML'
18             }
19             );
20              
21             Then in your code...
22              
23             my $struct = { 'I' => { 'am' => 'a struct' };
24             $obj->data_column($struct);
25             $obj->update;
26              
27             And you can recover your data structure with:
28              
29             my $obj = ...->find(...);
30             my $struct = $obj->data_column;
31              
32             The data structures you assign to "data_column" will be saved in the database in YAML format.
33              
34             =cut
35              
36 3     3   25 use strict;
  3         4  
  3         85  
37 3     3   12 use warnings;
  3         4  
  3         72  
38 3     3   1385 use YAML;
  3         18032  
  3         177  
39 3     3   24 use Carp;
  3         3  
  3         546  
40              
41             =over 4
42              
43             =item get_freezer
44              
45             Called by DBIx::Class::InflateColumn::Serializer to get the routine that serializes
46             the data passed to it. Returns a coderef.
47              
48             =cut
49              
50             sub get_freezer{
51 6     6 1 11 my ($class, $column, $info, $args) = @_;
52              
53 6 100       19 if (defined $info->{'size'}){
54 3         6 my $size = $info->{'size'};
55             return sub {
56 2     2   13135 my $s = YAML::Dump(shift);
57 2 100       2104 croak "serialization too big" if (length($s) > $size);
58 1         5 return $s;
59 3         21 };
60             } else {
61             return sub {
62 2     2   172659 return YAML::Dump(shift);
63 3         15 };
64             }
65             }
66              
67             =item get_unfreezer
68              
69             Called by DBIx::Class::InflateColumn::Serializer to get the routine that deserializes
70             the data stored in the column. Returns a coderef.
71              
72             =back
73              
74             =cut
75              
76             sub get_unfreezer {
77             return sub {
78 3     3   26974 return YAML::Load(shift);
79 6     6 1 78 };
80             }
81              
82              
83             1;