| 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
MC/DC0%0/1
$ENV{'POBJECT_DEBUG'} || 0
$ENV{'POBJECT_DEBUG'}
Truth table50%1/2
A
A= $ENV{'POBJECT_DEBUG'}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 5 | ▶ | unless ( $has_id ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $props->{"tmap"} and $props->{"tmap"}{$colname}
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}
| |||||||||||||||||||||||||||||
| 94 | 12 | ▶ | if ( $colname eq 'id' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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"
MC/DC100%1/1
$props->{"driver"} ||= "file"
$props->{"driver"}
Truth table100%2/2
A
A= $props->{"driver"}
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # if no serializer set defaulting to 'storable' | ||||||||||||||||||||||||||||
| 105 | 5 | ▶ | $props->{serializer} ||= 'storable'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$props->{"serializer"} ||= "storable"
MC/DC0%0/1
$props->{"serializer"} ||= "storable"
$props->{"serializer"}
Truth table50%1/2
A
A= $props->{"serializer"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||