DBI/Filter/YAML.pm

filter: Statementstmt 89.6% 26 / 29 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 90.7% 39 / 43 CC 3 SCAR 4.1
CC
3
Cov
89%
encode
2.0
decode
2.0
BEGIN
1.0
CRAP 1.5
« DBI/Filter/SaltHash.pm ↑ Summary DBI/Filter/Storable.pm »
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Jifty::DBI::Filter::YAML;
5 2 ▶use base qw/ Jifty::DBI::Filter /;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 my ($Dump, $Load);
8  
9 2 ▶eval "use YAML::Syck ()";
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 2 2
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 0 require YAML;
15 0 $Dump = \&YAML::Dump;
16 0 $Load = \&YAML::Load;
17 }
18  
19 else {
20 2 $Dump = \&YAML::Syck::Dump;
21 2 $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<YAML>. This is very similar to the L<Jifty::DBI::Filter::Storable> filter except that the L<YAML> 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 ▶ my $self = shift;
Subroutine encode: called CC=2 SCAR=6.9
Pod documented
60  
61 3 my $value_ref = $self->value_ref;
62 3 ▶ return unless defined $$value_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$value_ref 1 2 3
63  
64 2 $$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 ▶ my $self = shift;
Subroutine decode: called CC=2 SCAR=6.9
Pod documented
75  
76 3 my $value_ref = $self->value_ref;
77 3 ▶ return unless defined $$value_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$value_ref 1 2 3
78  
79 2 $$value_ref = $Load->($$value_ref);
80 }
81  
82 =head1 IMPLEMENTATION
83  
84 This class will attempt to use L<YAML::Syck> if it is available and then fall back upon L<YAML>. This has been done because the Syck library is written in C and is considerably faster.
85  
86 =head1 SEE ALSO
87  
88 L<Jifty::DBI::Filter>, L<YAML>, L<YAML::Syck>
89  
90 =head1 AUTHOR
91  
92 Andrew Sterling Hanenkamp E<lt>hanenkamp@cpan.orgE<gt>
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 2 1
« DBI/Filter/SaltHash.pm ↑ Summary DBI/Filter/Storable.pm »