File Coverage

blib/lib/Jifty/DBI/Filter/YAML.pm
Criterion Covered Total %
statement 20 20 100.0
branch 4 4 100.0
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 32 32 100.0


line stmt bran cond sub pod time code
1 2     2   13 use strict;
  2         4  
  2         79  
2 2     2   9 use warnings;
  2         4  
  2         181  
3              
4             package Jifty::DBI::Filter::YAML;
5 2     2   10 use base qw/ Jifty::DBI::Filter /;
  2         3  
  2         683  
6              
7             my ($Dump, $Load);
8              
9 2     2   1089 eval "use YAML::Syck ()";
  2         2593  
  2         26  
10             if ($@) {
11             # We don't actually need to "use", which is checked at compile
12             # time and would cause error when YAML is not installed.
13             # Or, eval this, too?
14             require YAML;
15             $Dump = \&YAML::Dump;
16             $Load = \&YAML::Load;
17             }
18              
19             else {
20             $Dump = \&YAML::Syck::Dump;
21             $Load = \&YAML::Syck::Load;
22             }
23              
24             =head1 NAME
25              
26             Jifty::DBI::Filter::YAML - This filter stores arbitrary Perl via YAML
27              
28             =head1 SYNOPSIS
29              
30             use Jifty::DBI::Record schema {
31             column my_data =>
32             type is 'text',
33             filters are qw/ Jifty::DBI::Filter::YAML /;
34             };
35              
36             my $thing = __PACKAGE__->new;
37             $thing->create( my_data => { foo => 'bar', baz => [ 1, 2, 3 ] } );
38              
39             my $my_data = $thing->my_data;
40             while (my ($key, $value) = %$my_data) {
41             # do something...
42             }
43              
44             =head1 DESCRIPTION
45              
46             This filter provides the ability to store arbitrary data structures into a database column using L. This is very similar to the L filter except that the L format remains human-readable in the database. You can store virtually any Perl data, scalar, hash, array, or object into the database using this filter.
47              
48             In addition, YAML (at least the storage of scalars, hashes, and arrays) is compatible with data structures written in other languages, so you may store or read data between applications written in different languages.
49              
50             =head1 METHODS
51              
52             =head2 encode
53              
54             This method is used to encode the Perl data structure into YAML formatted text.
55              
56             =cut
57              
58             sub encode {
59 3     3 1 8 my $self = shift;
60              
61 3         22 my $value_ref = $self->value_ref;
62 3 100       46 return unless defined $$value_ref;
63              
64 2         11 $$value_ref = $Dump->($$value_ref);
65             }
66              
67             =head2 decode
68              
69             This method is used to decode the YAML formatted text from the database into the Perl data structure.
70              
71             =cut
72              
73             sub decode {
74 3     3 1 6 my $self = shift;
75              
76 3         14 my $value_ref = $self->value_ref;
77 3 100       25 return unless defined $$value_ref;
78              
79 2         14 $$value_ref = $Load->($$value_ref);
80             }
81              
82             =head1 IMPLEMENTATION
83              
84             This class will attempt to use L if it is available and then fall back upon L. This has been done because the Syck library is written in C and is considerably faster.
85              
86             =head1 SEE ALSO
87              
88             L, L, L
89              
90             =head1 AUTHOR
91              
92             Andrew Sterling Hanenkamp Ehanenkamp@cpan.orgE
93              
94             =head1 LICENSE
95              
96             This program is free software and may be modified or distributed under the same terms as Perl itself.
97              
98             =cut
99              
100             1