PObject.pm

filter: Statementstmt 84.1% 69 / 82 Branchbran 57.6% 15 / 26 Conditioncond 58.3% 7 / 12 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 1 totaltotal 75.0% 102 / 136 CC 23 SCAR 30.8
CC
23
Cov
76%
pobject
30.5
import
2.0
__ANON__
2.0
CRAP 21.8
« PObject/Driver/mysql.pm ↑ Summary PObject/Driver/sqlite.pm »
1 package Class::PObject;
2  
3 # PObject.pm,v 1.57 2005/02/20 18:05:00 sherzodr Exp
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #use diagnostics;
7 3 ▶use Log::Agent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use vars ('$VERSION', '$revision');
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 $VERSION = '2.17';
11 3 $revision = '1.57';
12  
13 # configuring Log::Agent
14 3 ▶logconfig(-level=>$ENV{POBJECT_DEBUG} || 0);
Condition50%1/2
$ENV{'POBJECT_DEBUG'} || 0
l!l
03
MC/DC0%0/1
$ENV{'POBJECT_DEBUG'} || 0
$ENV{'POBJECT_DEBUG'}
Truth table50%1/2
A
A= $ENV{'POBJECT_DEBUG'}
Aresult
11
00
15  
16 7 ▶sub import {
Subroutine import: called CC=2 SCAR=7.1
17 7 my $class = shift;
18 7 my $caller_pkg = (caller)[0];
19  
20 7 ▶ unless ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 7 0 7
21 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 7 *{ "$caller_pkg\::pobject" } = \&{ "$class\::pobject" };
23 7 return 1
24 }
25 0 require Exporter;
26 0 return $class->Exporter::import( @_ )
27 }
28  
29 5 ▶sub pobject {
Subroutine pobject: called CC=21 SCAR=34.2
Pod undocumented
30 5 my ($class, $props);
31  
32 # are we given explicit class name to be created in?
33 5 ▶ if ( @_ == 2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 2) { } 5 0 5
elsif ($_[0] and ref $_[0] eq 'HASH') { } 0 0 0
34 5 ($class, $props) = @_;
35 5 logtrc 1, "pobject %s => %s", $class, $props
36 }
37 # Is class name assumed to be the current caller's package?
38 ▶ elsif ( $_[0] && (ref($_[0]) eq 'HASH') ) {
Condition0%0/3
$_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and ref $_[0] eq "HASH"
$_[0] ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
39 0 $props = $_[0];
40 0 $class = (caller())[0];
41 0 logtrc 1, "pobject ('%s'), %s", $class, $props
42 }
43 # otherwise, we throw a usage exception:
44 else {
45 0 logcroak "Usage error"
46 }
47 # should we make sure that current package is not 'main'?
48 5 ▶ if ( $class eq 'main' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq "main") 0 5 5
49 0 logcroak "'main' cannot be the class name"
50 }
51  
52 # creating the class virtually. Note, that it is different
53 # then the way Class::Struct works. Class::Struct literally builds
54 # the class contents in a string, and then eval()s them.
55 # And we play with symtables. However, I'm not sure how secure this method is.
56  
57 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
58 # if the properties have already been created, it means the user
59 # is declaring the class with the same name twice. He should be shot!
60 5 ▶ if ( ${ "$class\::props" } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${"${class}::props";}) 0 5 5
61 0 logcroak "are you trying to create the same class two times?"
62 }
63  
64 # we should have some columns
65 5 ▶ unless ( @{$props->{columns}} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$props->{"columns"};}) 0 5 5
66 0 logcroak "class '%s' should have columns!", $class
67 }
68  
69 # one of the columns should be 'id'. I believe this is a limitation,
70 # which should be eliminated in next release
71 5 my $has_id = 0;
72 5 for ( @{$props->{columns}} ) {
73 5 ▶ $has_id = ($_ eq 'id') and last
Branch: 1 branch
BranchTrueFalseTotal
$has_id = $_ eq "id" and last 5 0 5
74 }
75 5 ▶ unless ( $has_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_id) 0 5 5
76 0 logcroak "one of the columns must be 'id'"
77 }
78  
79 # certain method names are reserved. Making sure they won't get overridden
80 5 my @reserved_methods = qw(
81 new load fetch save remove remove_all set get
82 pobject_init set_datasource set_driver drop_datasource DESTROY
83 );
84 5 for my $method ( @reserved_methods ) {
85 65 for my $column ( @{$props->{columns}} ) {
86 247 ▶ if ( $method eq $column ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq $column) 0 247 247
87 0 logcroak "method '%s' is reserved", $method
88 }
89 }
90 }
91  
92 5 for my $colname ( @{$props->{columns}} ) {
93 19 ▶ unless ( defined($props->{tmap}) && $props->{tmap}->{$colname} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $props->{"tmap"} and $props->{"tmap"}{$colname}) 12 7 19
Condition100%3/3
defined $props->{"tmap"} and $props->{"tmap"}{$colname}
!ll&&!rl&&r
2107
MC/DC100%2/2
defined $props->{"tmap"} and $props->{"tmap"}{$colname}
defined $props->{"tmap"} $props->{"tmap"}{$colname}
Truth table100%3/3
A and B
A= defined $props->{"tmap"}B= $props->{"tmap"}{$colname}
ABresult
0X0
100
111
94 12 ▶ if ( $colname eq 'id' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colname eq "id") 4 8 12
95 4 $props->{tmap}->{$colname} = 'INTEGER', next
96 }
97 8 $props->{tmap}->{$colname} = 'VARCHAR(255)'
98 }
99 }
100  
101 # if no driver was specified, default driver to be used is 'file'
102 5 ▶ $props->{driver} ||= 'file';
Condition100%2/2
$props->{"driver"} ||= "file"
l!l
41
MC/DC100%1/1
$props->{"driver"} ||= "file"
$props->{"driver"}
Truth table100%2/2
A
A= $props->{"driver"}
Aresult
11
00
103  
104 # if no serializer set defaulting to 'storable'
105 5 ▶ $props->{serializer} ||= 'storable';
Condition50%1/2
$props->{"serializer"} ||= "storable"
l!l
50
MC/DC0%0/1
$props->{"serializer"} ||= "storable"
$props->{"serializer"}
Truth table50%1/2
A
A= $props->{"serializer"}
Aresult
11
00
106  
107 # it's important that we cache all the properties passed to the pobject()
108 # as a static data. This lets multiple instances of the pobject to access
109 # this data whenever needed
110 5 ${ "$class\::props" } = $props;
111  
112 5 require Class::PObject::Template;
113 # To ensure operator overloading works properly on these
114 # objects, let's also set the caller's @ISA array:
115 5 push @{ "$class\::ISA" }, "Class::PObject::Template";
116  
117 # creating accessor methods
118 5 for my $colname ( @{ $props->{columns} } ) {
119 19 ▶ if ( $class->UNIVERSAL::can($colname) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->UNIVERSAL::can($colname)) 0 19 19
120 0 logcarp "method '%s' exists in the caller's package", $colname;
121 next
122 0 }
123 19 ▶ *{ "$class\::$colname" } = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
124 197 ▶ if ( @_ == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 37 160 197
125 37 my $set = \&Class::PObject::Template::set;
126 37 return $set->( $_[0], $colname, $_[1] )
127 }
128 160 my $get = \&Class::PObject::Template::get;
129 160 return $get->( $_[0], $colname )
130 }
131 19 }
132 }
133  
134 3 logtrc 1, "%s loaded successfully", __PACKAGE__;
135  
136 3 1;
137 __END__
« PObject/Driver/mysql.pm ↑ Summary PObject/Driver/sqlite.pm »