PObject/Driver.pm

filter: Statementstmt 66.1% 80 / 121 Branchbran 62.5% 20 / 32 Conditioncond 45.8% 11 / 24 MC/DCmcdc 20.0% 3 / 15 Subroutinesub 61.9% 13 / 21 Podpod 84.6% 11 / 13 totaltotal 61.0% 138 / 226 CC 27 SCAR 15.9
CC
27
Cov
63%
stash
12.0
thaw
8.6
_filter_by_args
7.1
CRAP 4.9
« PObject/Template.pm ↑ Summary PObject/Test/HAS_A.pm »
1 package Class::PObject::Driver;
2  
3 # Driver.pm,v 1.19 2003/11/07 00:36:21 sherzodr Exp
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #use diagnostics;
7 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Log::Agent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use vars ('$VERSION');
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 $VERSION = '2.00';
12  
13 # Preloaded methods go here.
14  
15 3 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
16 3 my $class = shift;
17 3 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
18  
19 3 logtrc 3, "%s->new()", $class;
20  
21 3 my $self = {
22 _stash => { },
23 };
24 3 return bless($self, $class)
25 }
26  
27  
28 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
29  
30  
31 1 ▶sub errstr {
Subroutine errstr: called CC=3 SCAR=11.2
Pod documented
32 1 my ($self, $errstr) = @_;
33 1 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
34  
35 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶ if ( defined $errstr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $errstr) 1 0 1
37 1 ${ "$class\::errstr" } = $errstr
38 }
39 1 return ${ "$class\::errstr" }
40 }
41  
42  
43  
44  
45 0 ▶sub stash {
Subroutine stash: not called CC=3 SCAR=24.8
Pod documented
46 0 my ($self, $key, $value) = @_;
47  
48 0 ▶ if ( defined($key) && defined($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key and defined $value) 0 0 0
Condition0%0/3
defined $key and defined $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $key and defined $value
defined $key defined $value
Truth table0%0/3
A and B
A= defined $keyB= defined $value
ABresult
0X0
100
111
49 0 $self->{_stash}->{$key} = $value
50 }
51 0 return $self->{_stash}->{$key}
52 }
53  
54  
55  
56  
57 0 ▶sub dump {
Subroutine dump: not called CC=1 SCAR=6.9
Pod undocumented
58 0 my $self = shift;
59  
60 0 require Data::Dumper;
61 0 my $d = new Data::Dumper([$self], [ref $self]);
62 0 return $d->Dump()
63 }
64  
65  
66 0 ▶sub save {
Subroutine save: not called CC=1 SCAR=6.9
Pod documented
67 0 my $self = shift;
68 0 my ($object_name, $props, $columns) = @_;
69  
70 0 croak "'$object_name' object doesn't support 'save()' method"
71 }
72  
73 0 ▶sub load {
Subroutine load: not called CC=1 SCAR=6.9
Pod documented
74 0 my $self = shift;
75 0 my ($object_name, $props, $id) = @_;
76  
77 0 croak "'$object_name' doesn't support 'load()' method"
78 }
79  
80  
81  
82 0 ▶sub load_ids {
Subroutine load_ids: not called CC=1 SCAR=6.9
Pod documented
83 0 my $self = shift;
84 0 my ($object_name, $props, $terms, $args) = @_;
85  
86 0 croak "'$object_name' doesn't support 'load()' method"
87 }
88  
89  
90  
91 0 ▶sub remove {
Subroutine remove: not called CC=1 SCAR=6.9
Pod documented
92 0 my $self = shift;
93 0 my ($object_name, $props, $id) = @_;
94  
95 0 croak "'$object_name' doesn't support 'remove()' method"
96 }
97  
98 0 ▶sub drop_datasource {
Subroutine drop_datasource: not called CC=1 SCAR=6.9
Pod documented
99 0 my $self = shift;
100 0 my ($object_name, $props) = @_;
101  
102 0 croak "'$object_name' doesn't support 'drop_datasource()' method"
103 }
104  
105  
106 5 ▶sub remove_all {
Subroutine remove_all: called CC=3 SCAR=11.2
Pod documented
107 5 my $self = shift;
108 5 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
500
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
109 5 my ($object_name, $props, $terms) = @_;
110  
111 5 logtrc 3, "%s->remove_all(%s)", $class, join ", ", @_;
112  
113 5 my $data_set = $self->load_ids($object_name, $props, $terms);
114 5 for ( @$data_set ) {
115 5 $self->remove($object_name, $props, $_)
116 }
117 5 return 1
118 }
119  
120  
121  
122  
123 15 ▶sub count {
Subroutine count: called CC=3 SCAR=11.2
Pod documented
124 15 my $self = shift;
125 15 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
1500
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
126 15 my ($object_name, $props, $terms) = @_;
127  
128 15 logtrc 3, "%s->count(%s)", $class, join ", ", @_;
129  
130 15 my $data_set = $self->load_ids($object_name, $props, $terms);
131 15 ▶ return scalar( @$data_set ) || 0
Condition100%2/2
scalar @$data_set || 0
l!l
105
MC/DC100%1/1
scalar @$data_set || 0
scalar @$data_set
Truth table100%2/2
A
A= scalar @$data_set
Aresult
11
00
132 }
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143 17 ▶sub _filter_by_args {
Subroutine _filter_by_args: called CC=7 SCAR=19.6
144 17 my ($self, $data_set, $args) = @_;
145  
146 17 ▶ unless ( keys %$args ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %$args) 0 17 17
147 0 return $data_set
148 }
149 # if sorting column was defined
150 17 ▶ if ( defined $args->{'sort'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"sort"}) 17 0 17
151 # default to 'asc' sorting direction if it was not specified
152 17 ▶ $args->{direction} ||= 'asc';
Condition100%2/2
$args->{"direction"} ||= "asc"
l!l
98
MC/DC100%1/1
$args->{"direction"} ||= "asc"
$args->{"direction"}
Truth table100%2/2
A
A= $args->{"direction"}
Aresult
11
00
153 # and sort the data set
154 17 ▶ if ( $args->{direction} eq 'desc' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'direction'} eq 'desc') { } 6 11 17
155 6 $data_set = [ sort {$b->{$args->{'sort'}} cmp $a->{$args->{'sort'}} } @$data_set]
156 } else {
157 11 $data_set = [ sort {$a->{$args->{'sort'}} cmp $b->{$args->{'sort'}} } @$data_set]
158 }
159 }
160 # if 'limit' was defined
161 17 ▶ if ( defined $args->{limit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"limit"}) 9 8 17
162 # default to 0 for 'offset' if 'offset' was not set
163 9 ▶ $args->{offset} ||= 0;
Condition100%2/2
$args->{"offset"} ||= 0
l!l
27
MC/DC100%1/1
$args->{"offset"} ||= 0
$args->{"offset"}
Truth table100%2/2
A
A= $args->{"offset"}
Aresult
11
00
164 # and splice the data set
165 9 return [splice(@$data_set, $args->{offset}, $args->{limit})]
166 }
167 8 return $data_set
168 }
169  
170  
171  
172  
173  
174  
175 93 ▶sub _matches_terms {
Subroutine _matches_terms: called CC=5 SCAR=16.1
176 93 my $self = shift;
177 93 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
9300
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
178 93 my ($data, $terms) = @_;
179  
180 93 logtrc 3, "%s->_matches_terms(@_)", $class;
181 93 ▶ unless ( keys %$terms ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %$terms) 48 45 93
182 48 return 1
183 }
184 # otherwise check this data set against all the terms
185 # provided. If even one of those terms are not satisfied,
186 # return false
187 45 ▶ while ( my ($column, $value) = each %$terms ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$terms) 48 21 69
188 48 $^W = 0;
189 48 ▶ if ( $data->{$column} ne $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{$column} ne $value) 24 24 48
190 24 return 0
191 }
192 }
193 21 return 1
194 }
195  
196  
197  
198  
199 11 ▶sub freeze {
Subroutine freeze: called CC=4 SCAR=19.4
Pod documented
200 11 my ($self, $object_name, $props, $data) = @_;
201  
202 11 my $rv = undef;
203 11 ▶ if ( $props->{serializer} eq "xml" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($props->{'serializer'} eq 'xml') { } 0 11 11
elsif ($props->{'serializer'} eq 'dumper') { } 0 11 11
elsif ($props->{'serializer'} eq 'freezethaw') { } 0 11 11
204 0 require Data::DumpXML;
205 0 $rv = Data::DumpXML::dump_xml($data)
206 } elsif ( $props->{serializer} eq "dumper" ) {
207 0 require Data::Dumper;
208 0 my $d = Data::Dumper->new([$data]);
209 0 $d->Terse(1);
210 0 $d->Indent(0);
211 0 $rv = $d->Dump();
212 } elsif ( $props->{serializer} eq 'freezethaw' ) {
213 0 require FreezeThaw;
214 0 $rv = FreezeThaw::freeze($data)
215 } else {
216 11 require Storable;
217 11 $rv = Storable::freeze( $data )
218 }
219 11 return $rv
220 }
221  
222  
223  
224  
225 142 ▶sub thaw {
Subroutine thaw: called CC=5 SCAR=21.5
Pod documented
226 142 my ($self, $object_name, $props, $datastr) = @_;
227  
228 142 ▶ unless ( $datastr ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($datastr) 0 142 142
229 return undef
230 0 }
231  
232 142 my $rv = undef;
233 142 ▶ if ( $props->{serializer} eq "xml" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($props->{'serializer'} eq 'xml') { } 0 142 142
elsif ($props->{'serializer'} eq 'dumper') { } 0 142 142
elsif ($props->{'serializer'} eq 'freezethaw') { } 0 142 142
234 0 require Data::DumpXML::Parser;
235 0 my $p = Data::DumpXML::Parser->new();
236 0 warn "parsing '$datastr'";
237 0 $rv = $p->parse($datastr)
238 } elsif ( $props->{serializer} eq "dumper" ) {
239 0 $rv = eval $datastr;
240 } elsif ( $props->{serializer} eq 'freezethaw' ) {
241 0 require FreezeThaw;
242 0 $rv = (FreezeThaw::thaw($datastr))[0]
243 } else {
244 142 require Storable;
245 142 $rv = Storable::thaw( $datastr );
246 }
247 142 return $rv
248 }
249  
250  
251  
252  
253  
254  
255  
256 3 1;
257 __END__
« PObject/Template.pm ↑ Summary PObject/Test/HAS_A.pm »