OO/_Base.pm

filter: Statementstmt 73.5% 50 / 68 Branchbran 81.8% 18 / 22 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 61.5% 8 / 13 Podpod 50.0% 1 / 2 totaltotal 67.8% 78 / 115 CC 15 SCAR 21.0
CC
15
Cov
72%
_printDebug
20.0
_argsToHash
11.0
new
2.0
CRAP 8.1
« OO/_SpecObj.pm ↑ Summary OO.pm »
1 ######################################################################
2 # Copyright (c)2010-2011, David L. Armstrong.
3 #
4 # P4::OO::_Base.pm
5 #
6 # See COPYRIGHT AND LICENSE section in pod text below for usage
7 # and distribution rights.
8 #
9 ######################################################################
10  
11 =head1 NAME
12  
13 P4::OO::_Base - Base class for all P4::OO objects
14  
15 =head1 SYNOPSIS
16  
17 use base 'P4::OO::_Base';
18  
19 =head1 DESCRIPTION
20  
21 P4::OO::_Base functions only as a base class, providing the following:
22  
23 =head2 Attribute Handling
24  
25 _getAttr(), _setAttr(), _delAttr(), _listAttrs()
26  
27 =head2 Option Handling
28  
29 _argsToHash()
30  
31 =head2 Exception handling and basic hierarchy
32  
33 Exceptions are provided by the P4::OO::_Error decorator.
34  
35 =cut
36  
37  
38 ######################################################################
39 # Package Initialization
40 #
41 package P4::OO::_Base;
42 28 our $VERSION = '0.00_02';
43  
44 # Import exception methods and hierarchy
45 28 ▶ use P4::OO::_Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 # Standard Stuff
48 28 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 28 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 # For _uniqueID()
52 28 require Scalar::Util;
53  
54  
55 ######################################################################
56  
57 =head1 CONSTRUCTOR
58  
59 =cut
60  
61 ######################################################################
62  
63 ######################################################################
64 sub new
65 37 ▶{
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
66 37 my $proto = shift;
67 37 ▶ my $class = ref( $proto ) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0370
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
68
69 37 my $self = {};
70 37 bless( $self, $class );
71
72 37 my $subName = ( caller( 0 ) )[3];
73 37 $self->{'_objAttrs'} = $self->_argsToHash( $subName, @_ );
74
75 37 return( $self );
76 }
77  
78  
79 ######################################################################
80  
81 =head1 METHODS
82  
83 =cut
84  
85 ######################################################################
86  
87 =head2 query
88  
89 PURPOSE:
90 Helper method that allows all subclass objects to query Perforce.
91  
92 PARAMETERS/RETURNS/THROWS:
93 See Documentation for P4::OO::_Connection for details
94  
95 =cut
96  
97 ######################################################################
98 sub query
99 0 ▶{
Subroutine query: not called CC=1 SCAR=6.9
Pod documented
100 0 my $self = shift();
101  
102 0 my $p4Conn = $self->_getP4Connection();
103  
104 0 return( $p4Conn->query( @_ ) );
105 }
106  
107  
108 sub _uniqueID
109 14 ▶{
Subroutine _uniqueID: called CC=1 SCAR=0.0
110 14 my $self = shift();
111  
112 14 return( Scalar::Util::refaddr( $self ) );
113 }
114  
115  
116 sub _printDebug
117 0 ▶{
Subroutine _printDebug: not called CC=4 SCAR=30.0
118 0 my $self = shift();
119  
120 0 ▶ if( ( $self->_getAttr( 'debugFlag' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_getAttr("debugFlag") or $ENV{"P4OO_DEBUG"}) 0 0 0
Condition0%0/3
$self->_getAttr("debugFlag") or $ENV{"P4OO_DEBUG"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_getAttr("debugFlag") or $ENV{"P4OO_DEBUG"}
$self->_getAttr("debugFlag") $ENV{"P4OO_DEBUG"}
Truth table0%0/3
A or B
A= $self->_getAttr("debugFlag")B= $ENV{"P4OO_DEBUG"}
ABresult
1X1
011
000
121 || ( $ENV{'P4OO_DEBUG'} ) )
122 {
123 0 foreach my $debugLine ( @_ )
124 {
125 0 chomp( $debugLine );
126 0 print "DEBUG: $debugLine\n";
127 }
128 }
129 }
130  
131  
132 sub _getAttr
133 21 ▶{
Subroutine _getAttr: called CC=1 SCAR=0.0
134 21 return( $_[0]->{'_objAttrs'}->{$_[1]} );
135 }
136  
137  
138 sub _setAttr
139 10 ▶{
Subroutine _setAttr: called CC=1 SCAR=0.0
140 10 return( $_[0]->{'_objAttrs'}->{$_[1]} = $_[2] );
141 }
142  
143 sub _setAttrs
144 0 ▶{
Subroutine _setAttrs: not called CC=1 SCAR=6.9
145 0 my $self = shift();
146 0 my $subName = ( caller( 0 ) )[3];
147 0 my $argsHash = $self->_argsToHash( $subName, @_ );
148  
149 0 return( map { $self->{'_objAttrs'}->{$_} = $argsHash->{$_} } ( keys( %{$argsHash} ) ) );
150 }
151  
152 sub _delAttr
153 0 ▶{
Subroutine _delAttr: not called CC=1 SCAR=6.9
154 0 return( delete( $_[0]->{'_objAttrs'}->{$_[1]} ) );
155 }
156  
157  
158 sub _listAttrs
159 0 ▶{
Subroutine _listAttrs: not called CC=1 SCAR=6.9
160 0 return( keys( %{$_[0]->{'_objAttrs'}} ) );
161 }
162  
163  
164 ######################################################################
165 # _argsToHash
166 #
167 # _argsToHash takes the arguments passed in and constructs a hash
168 # of name/value pairs from them, returning a reference to the hash.
169 #
170 # This is a helper method intended to make it simple for functions
171 # to accept name/value pair arguments in a consistent and easy way.
172 #
173 # If the same name is specified more than once when called in a list
174 # or ARRAYref form, the resulting value will be an ARRAYref of the
175 # values.
176 #
177 # Values are preserved as-is, so any reference values are simply
178 # copied, not dereferenced in any way.
179 #
180 # This method expects to be called on an object, not a class, and
181 # may try to recurse on itself, so an object inheriting this method
182 # must be used. No object state is changed in this method.
183 #
184 # For more helpful exceptions, the first argument required is the
185 # name of the caller, and will be used in the exception text.
186 #
187 # Each of these will return the same result:
188 # $self->_argsToHash( $callerName, 'name1', 'value1', 'name2', 'value2' );
189 # $self->_argsToHash( $callerName, [ 'name1', 'value1', 'name2', 'value2' ] );
190 # $self->_argsToHash( $callerName, { 'name1' => 'value1', 'name2' => 'value2' } );
191 #
192 ######################################################################
193 sub _argsToHash
194 45 ▶{
Subroutine _argsToHash: called CC=11 SCAR=24.0
195 45 my $self = shift();
196 45 my( $caller, @argsIn ) = @_;
197  
198 45 my $argsHashOut = {};
199  
200 45 ▶ if( scalar( @argsIn ) == 0 )
Branch: 3 branches
BranchTrueFalseTotal
if (scalar @argsIn == 0) { } 33 12 45
elsif (scalar(@argsIn) % 2 == 0) { } 10 2 12
elsif (scalar @argsIn == 1) { } 2 0 2
201 {
202 # Nothing to see here...
203 33 return( $argsHashOut );
204 }
205 elsif( scalar( @argsIn ) % 2 == 0 )
206 {
207 # Even number of args, so must be a list call - process duplicates
208  
209 # Keep track of args first seen as ARRAYrefs so we do break them down
210 10 my $arrayRefsSeen = {};
211  
212 10 ▶ while( scalar( @argsIn ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @argsIn) 18 10 28
213 {
214 18 my $name = shift( @argsIn );
215 18 my $value = shift( @argsIn );
216  
217 18 ▶ if( exists( $argsHashOut->{$name} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $argsHashOut->{$name}) { } 4 14 18
218 {
219 4 ▶ if( exists( $arrayRefsSeen->{$name} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $arrayRefsSeen->{$name}) { } 1 3 4
220 {
221 # We saw it first as an ARRAYref, so we just make a new ref.
222 1 $argsHashOut->{$name} = [ $argsHashOut->{$name}, $value ];
223 1 delete( $arrayRefsSeen->{$name} );
224 }
225 else
226 {
227 # We've seen it before, and maybe more than once!
228 3 ▶ if( UNIVERSAL::isa( $argsHashOut->{$name}, "ARRAY" ) )
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($argsHashOut->{$name}, 'ARRAY')) { } 1 2 3
229 {
230 1 push( @{$argsHashOut->{$name}}, $value );
231 }
232 else
233 {
234 2 $argsHashOut->{$name} = [ $argsHashOut->{$name}, $value ];
235 }
236 }
237 }
238 else
239 {
240 14 $argsHashOut->{$name} = $value;
241  
242 14 ▶ if( UNIVERSAL::isa( $value, "ARRAY" ) )
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($value, "ARRAY")) 1 13 14
243 {
244 1 $arrayRefsSeen->{$name} = 1;
245 }
246 }
247 }
248 }
249 elsif( scalar( @argsIn ) == 1 )
250 {
251 2 ▶ if( UNIVERSAL::isa( $argsIn[0], "ARRAY" ) )
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($argsIn[0], 'ARRAY')) { } 1 1 2
elsif (UNIVERSAL::isa($argsIn[0], 'HASH')) { } 1 0 1
252 {
253 # Called with ARRAYref, so just call self recurively with deref
254 1 return( $self->_argsToHash( $caller, @{$argsIn[0]} ) );
255 }
256 elsif( UNIVERSAL::isa( $argsIn[0], "HASH" ) )
257 {
258 # For now we'll copy it, but could be bad for performance...
259 1 %{$argsHashOut} = %{$argsIn[0]};
260 }
261 }
262 else
263 {
264 0 throw E_Fatal "$caller: Invalid arguments.\n";
265 }
266  
267 11 return( $argsHashOut );
268 }
269  
270  
271 ######################################################################
272 # Standard authorship and copyright for documentation
273 #
274  
275 =head1 AUTHOR
276  
277 David L. Armstrong <armstd@cpan.org>
278  
279 =head1 COPYRIGHT AND LICENSE
280  
281 P4::OO::_Base is Copyright (c)2010-2011, David L. Armstrong.
282  
283 This module is free software; you can redistribute it and/or
284 modify it under the same terms as Perl itself, either Perl
285 version 5.8.8 or, at your option, any later version of Perl 5
286 you may have available.
287  
288 =head1 SUPPORT AND WARRANTY
289  
290 This program is distributed in the hope that it will be
291 useful, but it is provided "as is" and without any expressed
292 or implied warranties.
293  
294 =cut
295  
296 28 1;
« OO/_SpecObj.pm ↑ Summary OO.pm »