blib/lib/Class/DBI/AsXML.pm

filter: Statementstmt 98.0% 49 / 50 Branchbran 90.0% 18 / 20 Conditioncond 88.8% 16 / 18 MC/DCmcdc 70.0% 7 / 10 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 92.5% 100 / 108 CC 19 SCAR 19.5
CC
19
Cov
94%
to_xml
10.0
_to_xml_stringify
8.7
create_from_xml
3.0
CRAP 7.0
↑ Summary
1 package Class::DBI::AsXML;
2 # $Id: AsXML.pm,v 1.2 2005/01/15 15:32:32 cwest Exp $
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 Class::DBI::AsXML - Format CDBI Objects as XML
8  
9 =head1 SYNOPSIS
10  
11 # As you do...
12 package MyApp::User;
13 use base qw[Class::DBI];
14
15 __PACKAGE__->connection('dbi:SQLite:dbfile', '', '');
16 __PACKAGE__->table(q[users]);
17 __PACKAGE__->columns(Primary => 'id');
18 __PACKAGE__->columns(Essential => qw[username password]);
19 __PACKAGE__->columns(Others => qw[email zip_code phone]);
20 __PACKAGE__->has_a(pref => 'MyApp::Pref');
21  
22 # Enter XML Support!
23 use Class::DBI::AsXML;
24 __PACKAGE__->to_xml_columns([qw[username email zip_code]]);
25  
26 # Elsewhere...
27 my $user = MyApp::User->retrieve(shift);
28 my $user_and_prefs_xml = $user->to_xml(depth => 1);
29  
30 # Or... override defaults
31 my $uname_pwd_xml = $user->to_xml( columns => {
32 ref($user) => [qw[username password]],
33 });
34
35 # Create from XML
36 my $new_user = MyApp::User->create_from_xml(<<__XML__);
37 <user>
38 <username>new_user</username>
39 <password>new_pass</password>
40 <email>&lt;casey@geeknest.com%gt;</email>
41 </user>
42 __XML__
43  
44 =cut
45  
46 1 ▶use base qw[Exporter];
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use vars qw[@EXPORT $VERSION];
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 $VERSION = sprintf "%d.%02d", split m/\./, (qw$Revision: 1.2 $)[1];
49 1 @EXPORT = qw[to_xml create_from_xml _to_xml_stringify];
50  
51 1 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use overload;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 =head1 DESCRIPTION
55  
56 This software adds XML output support to C<Class::DBI> based objects.
57  
58 =head2 to_xml_columns
59  
60 Class->to_xml_columns([qw[columns to dump with xml]]);
61  
62 This class method sets the default columns this class should dump
63 when calling C<to_xml()> on an object. The single parameter is a
64 list reference with column names listed.
65  
66 =head2 to_xml
67  
68 my $xml = $object->to_xml(
69 columns => {
70 MyApp::User => [ qw[username email zip_code] ],
71 MyApp::File => [ qw[user filename size] ],
72 MyApp::Pref => [ MyApp::Pref->columns ],
73 },
74 depth => 10,
75 xml => {
76 NoAttr => 0,
77 },
78 );
79  
80 All arguments are optional.
81  
82 C<columns> - A hash reference containing key/value pairs associating
83 class names to a list of columns to dump as XML when the class is
84 serialized. They keys are class names and values are list references
85 containing column names, just as they'd be sent to C<to_xml_columns()>.
86 Passing a C<columns> parameter to this instance method will override
87 any defaults associated with this object. Failing that, C<to_xml_colunms()>
88 is checked and failing that, the C<Primary> and C<Essential> columns
89 are dumped by default.
90  
91 Each column requested for XML output will go through an attempt to
92 be stringified. If the column value is an object with stringification
93 overloaded (using C<overload>) then it is stringified in that manner.
94 If the column is an object and its interface supports either C<as_string()>
95 or C<as_text()> methods, those method will be called and the results
96 returned. Finally, if the value is defined then it is stringified and
97 returned (this means references will become ugly). If the value is
98 undefined then an empty string is used in its place.
99  
100 C<depth> - Depth to dump to. Depth of zero, the default, will not
101 recurse. Column values are interogated to determine if they should
102 be recursed down. If the column value is an object whose API supports
103 the C<to_xml()> method, then that method will be called and the resulting
104 XML will be parsed via C<XMLin()> from C<XML::Simple>. The root node
105 will not be kept when converting the XML back into a data structure.
106  
107 C<xml> - Hash reference of XML::Simple options. Change these only
108 if you really know what you're doing. By default the following
109 options are set.
110  
111 NoAttr => 1,
112 RootName => $self->moniker,
113 XMLDecl => 0,
114  
115 =head2 create_from_xml
116  
117 my $new_user = MyApp::User->create_from_xml($xml);
118  
119 Creates a new user from an XML document. The document is parsed with
120 C<XMLin> and the root node is thrown away. All information passed in
121 to this method is ignored except the tags that match column names.
122  
123 =head1 EXPORTS
124  
125 This module is implemented as a mixin and therefore exports the
126 functions C<to_xml>, C<create_from_xml>, and C<_to_xml_stringify> into
127 the caller's namespace. If you don't want these to be exported, then
128 load this module using C<require>.
129  
130 =cut
131  
132 1 Class::DBI->mk_classdata('to_xml_columns');
133 1 Class::DBI->to_xml_columns([]);
134  
135 11 ▶sub to_xml {
Subroutine to_xml: called CC=10 SCAR=23.0
Pod documented
136 11 my ($self, %args) = @_;
137  
138 my @keys = ($args{columns} && $args{columns}->{ref($self)})
139 6 ▶ ? @{$args{columns}->{ref($self)}}
Branch: 1 branch
BranchTrueFalseTotal
@{$self->to_xml_columns;} ? : 1 4 5
140 5 : @{$self->to_xml_columns}
141 11 ▶ ? @{$self->to_xml_columns}
Branch: 1 branch
BranchTrueFalseTotal
$args{'columns'} && $args{'columns'}{ref $self} ? : 6 5 11
Condition100%3/3
$args{"columns"} and $args{"columns"}{ref $self}
!ll&&!rl&&r
416
MC/DC100%2/2
$args{"columns"} and $args{"columns"}{ref $self}
$args{"columns"} $args{"columns"}{ref $self}
Truth table100%3/3
A and B
A= $args{"columns"}B= $args{"columns"}{ref $self}
ABresult
0X0
100
111
142 : (map $self->columns($_), qw[Primary Essential]);
143 11 my @vals = $self->get(@keys);
144
145 11 my %hash;
146 11 foreach my $col ( @keys ) {
147 45 my $val = $self->$col;
148 45 ▶ if ( $args{depth} && $val && ref($val) && $val->can('to_xml')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'depth'} and $val and ref $val and $val->can('to_xml')) { } 4 41 45
Condition100%9/9
$args{"depth"} and $val and ref $val and $val->can("to_xml")
!ll&&!rl&&r
3924
$args{"depth"} and $val and ref $val
!ll&&!rl&&r
26136
$args{"depth"} and $val
!ll&&!rl&&r
26217
MC/DC75%3/4
$args{"depth"} and $val and ref $val and $val->can("to_xml")
$args{"depth"} $val ref $val $val->can("to_xml")
Truth table80%4/5
A and B and C and D
A= $args{"depth"}B= $valC= ref $valD= $val->can("to_xml")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
149 4 $hash{$col} = XMLin $val->to_xml(%args, depth => $args{depth} - 1);
150 } else {
151 41 $hash{$col} = $self->_to_xml_stringify($val);
152 }
153 }
154  
155 11 ▶ my %xml_simple = $args{xml} ? %{$args{xml}} : ();
Branch: 1 branch
BranchTrueFalseTotal
$args{'xml'} ? : 1 10 11
156 11 my $xml = XMLout \%hash,
157 NoAttr => 1,
158 RootName => $self->moniker,
159 XMLDecl => 0,
160 %xml_simple;
161
162 11 return $xml;
163 }
164  
165 1 ▶sub create_from_xml {
Subroutine create_from_xml: called CC=3 SCAR=11.0
Pod documented
166 1 my ($class, $xml) = @_;
167
168 1 my $data = XMLin $xml;
169  
170 1 my %args;
171 1 foreach ( $class->columns ) {
172 5 ▶ next unless exists $data->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{$_} 3 2 5
173 2 $args{$_} = $data->{$_};
174 }
175 1 return $class->create(\%args);
176 }
177  
178 41 ▶sub _to_xml_stringify {
Subroutine _to_xml_stringify: called CC=8 SCAR=21.6
179 41 my ($self, $val) = @_;
180  
181 41 ▶ if ($val && ref($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val and ref $val) 5 36 41
Condition66%2/3
$val and ref $val
!ll&&!rl&&r
0365
MC/DC50%1/2
$val and ref $val
$val ref $val
Truth table66%2/3
A and B
A= $valB= ref $val
ABresult
0X0
100
111
182 5 ▶ return "$val" if overload::Overloaded($val)
Branch: 1 branch
BranchTrueFalseTotal
if overload::Overloaded($val) and overload::Method($val, "\"\"") 1 4 5
Condition66%2/3
overload::Overloaded($val) and overload::Method($val, "\"\"")
!ll&&!rl&&r
401
MC/DC50%1/2
overload::Overloaded($val) and overload::Method($val, "\"\"")
overload::Overloaded($val) overload::Method($val, "\"\"")
Truth table66%2/3
A and B
A= overload::Overloaded($val)B= overload::Method($val, "\"\"")
ABresult
0X0
100
111
183 && overload::Method($val, '""');
184 4 ▶ return $val->as_string if $val->can('as_string');
Branch: 1 branch
BranchTrueFalseTotal
if $val->can("as_string") 1 3 4
185 3 ▶ return $val->as_text if $val->can('as_text');
Branch: 1 branch
BranchTrueFalseTotal
if $val->can("as_text") 0 3 3
186 }
187  
188 39 ▶ return "$val" if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 39 0 39
189 0 return '';
190 }
191  
192 1 1;
193  
194 __END__
↑ Summary