EO/Class.pm

filter: Statementstmt 88.4% 160 / 181 Branchbran 59.0% 26 / 44 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 93.5% 29 / 31 Podpod 100% 13 / 13 totaltotal 82.4% 230 / 279 CC 27 SCAR 11.0
CC
27
Cov
81%
methods
6.0
init
6.0
path
4.8
CRAP 3.0
« EO/Singleton.pm ↑ Summary EO/Method.pm »
1 package EO::Class;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use EO;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use EO::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use EO::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use EO::Method;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 our @ISA = qw( EO );
14 5 our $VERSION = 0.96;
15  
16 5 exception EO::Error::InvalidState;
17 5 exception EO::Error::ClassNotFound;
18  
19 ##
20 ## construct with an object as a parameter
21 ##
22 1 ▶sub new_with_object {
Subroutine new_with_object: called CC=3 SCAR=12.7
Pod documented
23 1 my $class = shift;
24 1 my $obj = shift;
25 1 ▶ if (!$obj || !blessed( $obj )) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj and builtin::blessed($obj)) 0 1 1
Condition33%1/3
$obj and builtin::blessed($obj)
!ll&&!rl&&r
001
MC/DC0%0/2
$obj and builtin::blessed($obj)
$obj builtin::blessed($obj)
Truth table33%1/3
A and B
A= $objB= builtin::blessed($obj)
ABresult
0X0
100
111
26 0 throw EO::Error::InvalidParameters text => 'no object provided';
27 }
28 1 $class->new_with_classname( ref($obj) );
29 }
30  
31 ##
32 ## construct with a classname as a parameter
33 ##
34 13 ▶sub new_with_classname {
Subroutine new_with_classname: called CC=3 SCAR=12.0
Pod documented
35 13 my $class = shift;
36 13 my $obj = shift;
37 13 ▶ if (!$obj || ref($obj)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $obj or ref $obj) 0 13 13
Condition33%1/3
not $obj or ref $obj
l!l&&r!l&&!r
0013
MC/DC0%0/2
not $obj or ref $obj
not $obj ref $obj
Truth table33%1/3
not(A) or B
A= not $objB= ref $obj
ABresult
1X1
011
000
38 0 throw EO::Error::InvalidParameters text => 'no classname provided';
39 }
40 13 my $self = $class->new();
41 13 $self->name( $obj );
42 13 return $self;
43 }
44  
45  
46 ##
47 ## gets/sets the name of the class
48 ##
49 235 ▶sub name {
Subroutine name: called CC=2 SCAR=6.9
Pod documented
50 235 my $self = shift;
51 235 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 14 221 235
52 14 $self->{ classname } = shift;
53 14 return $self;
54 }
55 221 return $self->{ classname };
56 }
57  
58 ##
59 ## adds a method to a class
60 ##
61 16 ▶sub add_method {
Subroutine add_method: called CC=4 SCAR=14.8
Pod documented
62 16 my $self = shift;
63 16 my $method = shift;
64 16 ▶ if (!$method) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($method) 0 16 16
65 0 throw EO::Error::InvalidParameters text => "must provide a EO::Method object as an argument";
66 }
67 16 ▶ if (!$method->name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($method->name) 0 16 16
68 0 throw EO::Error::InvalidState text => 'Method object has no name value set';
69 }
70 16 ▶ if (!$method->reference) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($method->reference) 0 16 16
71 0 throw EO::Error::InvalidState text => 'Method object has no reference value set';
72 }
73 16 my $class = $self->name;
74 {
75 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
76 16 *{$class . '::' . $method->name} = $method->reference;
77 }
78 }
79  
80 ##
81 ## gets a method from a class
82 ##
83 58 ▶sub get_method {
Subroutine get_method: called CC=4 SCAR=14.0
Pod documented
84 58 my $self = shift;
85 58 my $name = shift;
86 58 ▶ if (!$name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 58 58
87 0 throw EO::Error::InvalidParameters text => "no methodname provided";
88 }
89 58 ▶ if (!$self->name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->name) 0 58 58
90 0 throw EO::Error::InvalidState text => "class has no name()";
91 }
92 58 my $symbol;
93 {
94 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
95 58 $symbol = *{$self->name .'::'.$name};
96 }
97 58 my $method = EO::Method->new();
98 58 my $ref = $self->name->can($symbol);
99 58 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) 53 5 58
100 53 return EO::Method->new()
101 ->name( $name )
102 ->reference( $ref );
103 }
104 5 throw EO::Error::Method::NotFound text => "cannot find method $symbol";
105 }
106  
107 ##
108 ## returns a list of methods
109 ##
110 3 ▶sub methods {
Subroutine methods: called CC=6 SCAR=17.9
Pod documented
111 3 my $self = shift;
112 3 ▶ if (!$self->name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->name) 1 2 3
113 1 throw EO::Error::InvalidState text => "class has no name()";
114 }
115 2 my $glob;
116 {
117 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
118 2 $glob = *{$self->name .'::'};
119 }
120 2 my @syms = keys %{$glob};
121 2 my $methods = EO::Array->new();
122 2 foreach my $symbol (@syms) {
123 56 ▶ next unless $symbol;
Branch: 1 branch
BranchTrueFalseTotal
unless $symbol 0 56 56
124 56 my $method;
125 56 eval {
126 56 $method = $self->get_method( $symbol );
127 };
128 56 ▶ if (!$@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 52 4 56
129 52 $methods->push( $method );
130 }
131 }
132 2 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 1 1 2
133 1 return $methods->iterator;
134 } else {
135 1 return $methods;
136 }
137 }
138  
139 ##
140 ## returns the path to the class as an EO::File object
141 ##
142 2 ▶sub path {
Subroutine path: called CC=4 SCAR=15.7
Pod documented
143 2 my $self = shift;
144 2 ▶ if (!$self->name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->name) 0 2 2
145 0 throw EO::Error::InvalidState text => "class has no name()";
146 }
147 2 my $path = $INC{$self->_classToFile};
148 2 ▶ if ($path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) { } 2 0 2
149 2 my $pathclass = ref($self)->new_with_classname( 'EO::File' );
150 2 eval {
151 2 $pathclass->load();
152 };
153 2 ▶ if (!$@) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@) { } 2 0 2
154 2 EO::File->new( path => $INC{$self->_classToFile} );
155 } else {
156 0 EO::File::Stub->new( $path );
157 }
158 } else {
159 0 my $name = $self->name;
160 0 throw EO::Error::InvalidState text => "class $name has not yet been loaded";
161 }
162 }
163  
164 ##
165 ## returns true if the the class can delegate
166 ##
167 1 ▶sub can_delegate {
Subroutine can_delegate: called CC=2 SCAR=7.6
Pod documented
168 1 my $self = shift;
169 1 ▶ if (!$self->name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->name) 0 1 1
170 0 throw EO::Error::InvalidState text => "class has no name()";
171 }
172 1 $self->name->can('delegate');
173 }
174  
175 ##
176 ## returns the parent classes of the class
177 ##
178 3 ▶sub parents {
Subroutine parents: called CC=3 SCAR=11.3
Pod documented
179 3 my $self = shift;
180 3 ▶ if (!$self->name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->name) 0 3 3
181 0 throw EO::Error::InvalidState text => "class has no name()";
182 }
183 3 my $list = EO::Array->new();
184 {
185 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
186 3 $list->push( map { EO::Class->new_with_classname( $_ ) } @{$self->name . '::ISA'} );
187 }
188 3 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 3 3
189 0 return $list->iterator;
190 } else {
191 3 return $list;
192 }
193 }
194  
195 ##
196 ## adds a parent class
197 ##
198 2 ▶sub add_parent {
Subroutine add_parent: called CC=2 SCAR=6.9
Pod documented
199 2 my $self = shift;
200 {
201 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
202 2 push @{ $self->name . '::ISA' },
203 2 ▶ map { (ref( $_ )) ? $_->name : $_ }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 2 2
204 2 grep { defined }
205 @_;
206 }
207 2 return $self;
208 }
209  
210 ##
211 ## removes a parent class
212 ##
213 2 ▶sub del_parent {
Subroutine del_parent: called CC=2 SCAR=6.9
Pod documented
214 2 my $self = shift;
215 2 my %PARENTHASH = ();
216 {
217 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
218 2 %PARENTHASH = map { ($_ => 1) } @{ $self->name . '::ISA' };
219 }
220 2 foreach my $parent (@_) {
221 2 delete $PARENTHASH{ $parent };
222 }
223 {
224 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
225 2 @{ $self->name . '::ISA' } = keys %PARENTHASH;
226 }
227 2 return $self;
228 }
229  
230 ##
231 ## returns true if the class has been loaded
232 ##
233 2 ▶sub loaded {
Subroutine loaded: called CC=1 SCAR=0.0
Pod documented
234 2 my $self = shift;
235 2 my $path;
236 2 !!$INC{ $self->_classToFile };
237 }
238  
239 ##
240 ## loads a class
241 ##
242 3 ▶sub load {
Subroutine load: called CC=2 SCAR=7.2
Pod documented
243 3 my $self = shift;
244 3 eval {
245 3 require $self->_classToFile;
246 };
247 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
248 0 throw EO::Error::ClassNotFound text => $@;
249 }
250 3 return 1;
251 }
252  
253 ##
254 ## _classToFile is an internal method used to turn
255 ## class names into files that may be represented on
256 ## the disk. The number of times I have written this
257 ## in the past I cannot remember...
258 ##
259 9 ▶sub _classToFile {
Subroutine _classToFile: called CC=1 SCAR=0.0
260 9 my $self = shift;
261 9 my $name = $self->name;
262 9 $name =~ s/::/\//g;
263 9 $name .= '.pm';
264 }
265  
266 ##
267 ##
268 ## stub package to handle situations when EO::File is not installed
269 ##
270 ##
271 package EO::File::Stub;
272  
273 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
274 5 our @ISA = qw( EO );
275  
276 0 ▶sub init {
Subroutine init: not called CC=2 SCAR=17.9
277 0 my $self = shift;
278 0 ▶ if ($self->SUPER::init( @_ )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->SUPER::init(@_)) 0 0 0
279 0 $self->{ pathname } = shift;
280 0 return 1;
281 }
282 }
283  
284 0 ▶sub as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
285 0 my $self = shift;
286 0 return $self->{ pathname };
287 }
288  
289 5 1;
290 __END__
« EO/Singleton.pm ↑ Summary EO/Method.pm »