Declare/Meta.pm

filter: Statementstmt 89.2% 149 / 167 Branchbran 56.8% 25 / 44 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 94.7% 36 / 38 Podpod 57.1% 4 / 7 totaltotal 82.6% 214 / 259 CC 34 SCAR 11.3
CC
34
Cov
82%
_timeouts
12.0
as_perl
7.3
to_start
6.3
CRAP 3.1
« Declare.pm ↑ Summary Declare/Meta/Timeout.pm »
1 package POE::Declare::Meta;
2  
3 =pod
4  
5 =head1 NAME
6  
7 POE::Declare::Meta - Metadata object that describes a POE::Declare class
8  
9 =head1 DESCRIPTION
10  
11 B<POE::Declare::Meta> objects are constructed and used internally by
12 L<POE::Declare> during class construction. B<POE::Declare::Meta> objects
13 are not created directly.
14  
15 Access to the meta object for a L<POE::Declare> class is via the exported
16 C<meta> function.
17  
18 =head1 METHODS
19  
20 =cut
21  
22 9 ▶use 5.008007;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 6 ▶use Scalar::Util 1.19 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 6 ▶use Params::Util 1.00 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 6 ▶use Class::ISA 0.33 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 ▶use Class::Inspector 1.22 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 5 ▶use vars qw{$VERSION $DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
33 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
34 5 $VERSION = '0.59';
35 5 $DEBUG = !! $DEBUG;
36 }
37  
38 5 ▶use constant DEBUG => $DEBUG;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 5 ▶use POE::Declare::Meta::Slot ();
Subroutine BEGIN: called CC=1 SCAR=0.0
41 5 ▶use POE::Declare::Meta::Message ();
Subroutine BEGIN: called CC=1 SCAR=0.0
42 5 ▶use POE::Declare::Meta::Event ();
Subroutine BEGIN: called CC=1 SCAR=0.0
43 5 ▶use POE::Declare::Meta::Timeout ();
Subroutine BEGIN: called CC=1 SCAR=0.0
44 5 ▶use POE::Declare::Meta::Attribute ();
Subroutine BEGIN: called CC=1 SCAR=0.0
45 5 ▶use POE::Declare::Meta::Internal ();
Subroutine BEGIN: called CC=1 SCAR=0.0
46 5 ▶use POE::Declare::Meta::Param ();
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 5 ▶use Class::XSAccessor 1.10 {
Subroutine BEGIN: called CC=1 SCAR=0.0
49 5 getters => {
50 name => 'name',
51 alias => 'alias',
52 sequence => 'sequence',
53 compiled => 'compiled',
54 },
55 5 };
56  
57  
58  
59  
60  
61 #####################################################################
62 # Constructor
63  
64 9 ▶sub new {
Subroutine new: called CC=4 SCAR=15.8
Pod undocumented
65 9 my $class = shift;
66  
67 # The name of the class
68 9 my $name = shift;
69 9 ▶ unless ( Params::Util::_CLASS($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Params::Util::_CLASS($name)) 0 9 9
70 0 Carp::croak("Invalid class name '$name'");
71 }
72 9 ▶ unless ( Class::Inspector->loaded($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Class::Inspector"->loaded($name)) 0 9 9
73 0 Carp::croak("Class $name is not loaded");
74 }
75 9 ▶ unless ( $name->isa('POE::Declare::Object') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name->isa("POE::Declare::Object")) 0 9 9
76 0 Carp::croak("Class $name is not a POE::Declare::Object subclass");
77 }
78  
79 # Create the object
80 9 my $self = bless {
81 name => $name,
82 alias => $name,
83 sequence => 0,
84 attr => { },
85 }, $class;
86  
87 9 $self;
88 }
89  
90  
91  
92  
93  
94 #####################################################################
95 # Accessors
96  
97 =pod
98  
99 =head2 name
100  
101 The C<name> accessor returns the name of the class for this meta instance.
102  
103 =cut
104  
105 # sub name {
106 # $_[0]->{name};
107 # }
108  
109 =pod
110  
111 =head2 alias
112  
113 The C<alias> accessor returns the alias root string that will be used for
114 objects that are created of this type.
115  
116 Normally this will be identical to the class C<name> but may be changed
117 at constructor time.
118  
119 =cut
120  
121 # sub alias {
122 # $_[0]->{alias};
123 # }
124  
125 =pod
126  
127 =head2 sequence
128  
129 Because each object has its own L<POE::Session>, each session also needs
130 its own session alias, and the session alias is derived from a combination
131 of the C<alias> method an an incrementing C<sequence> value.
132  
133 The C<sequence> accessor returns the most recently requested value from the
134 sequence. As with sequence in SQL, not all values pulled from the sequence
135 will necesarily be used in an object, and objects will not necesarily have
136 incrementing sequence values.
137  
138 =cut
139  
140 # sub sequence {
141 # $_[0]->{sequence};
142 # }
143  
144  
145  
146  
147  
148 #####################################################################
149 # Methods
150  
151 =pod
152  
153 =head2 next_alias
154  
155 The C<next_alias> method generates and returns a new session alias,
156 by taking the C<alias> base string and appending an incremented
157 C<sequence> value.
158  
159 The typical alias string returned will look something like
160 C<'My::Class.123'>.
161  
162 =cut
163  
164 4 ▶sub next_alias {
Subroutine next_alias: called CC=1 SCAR=0.0
Pod documented
165 4 $_[0]->{alias} . '.' . ++$_[0]->{sequence};
166 }
167  
168 =pod
169  
170 =head2 super_path
171  
172 The C<super_path> method is provided as a convenience, and returns a list
173 of the inheritance path for the class.
174  
175 It is equivalent to C<Class::ISA::self_and_super_path('My::Class')>.
176  
177 =cut
178  
179 62 ▶sub super_path {
Subroutine super_path: called CC=1 SCAR=0.0
Pod documented
180 62 Class::ISA::self_and_super_path( $_[0]->name );
181 }
182  
183 =pod
184  
185 =head2 attr
186  
187 my $attribute = My::Class->meta->attr('foo');
188  
189 The C<attr> method is used to get a single named attribute meta object
190 within the class meta object.
191  
192 Returns a L<POE::Declare::Meta::Attribute> object or C<undef> if no such
193 named attribute exists.
194  
195 =cut
196  
197 44 ▶sub attr {
Subroutine attr: called CC=4 SCAR=13.9
Pod documented
198 44 my $self = shift;
199 44 my $name = shift;
200 44 foreach my $c ( $self->super_path ) {
201 65 ▶ my $meta = $POE::Declare::META{$c} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $meta = $POE::Declare::META{$c} or next 65 0 65
202 65 ▶ my $attr = $meta->{attr}->{$name} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $attr = $meta->{"attr"}{$name} or next 28 37 65
203 28 return $attr;
204 }
205 16 return undef;
206 }
207  
208 # Fetch all named attributes (from this or parents)
209 9 ▶sub attrs {
Subroutine attrs: called CC=4 SCAR=13.9
Pod undocumented
210 9 my $self = shift;
211 9 my %hash = ();
212 9 foreach my $c ( $self->super_path ) {
213 18 ▶ my $meta = $POE::Declare::META{$c} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $meta = $POE::Declare::META{$c} or next 18 0 18
214 18 my $attr = $meta->{attr};
215 18 foreach ( keys %$attr ) {
216 69 $hash{$_} = $attr->{$_};
217 }
218 }
219 9 return values %hash;
220 }
221  
222  
223  
224  
225  
226 #####################################################################
227 # Compilation
228  
229 9 ▶sub as_perl {
Subroutine as_perl: called CC=7 SCAR=19.8
Pod undocumented
230 9 my $self = shift;
231 9 my $name = $self->name;
232 9 my $attr = $self->{attr};
233  
234 # Go over all our methods, and add any required events
235 9 my $methods = Class::Inspector->methods($name, 'expanded');
236 9 foreach my $method ( @$methods ) {
237 484 my $mname = $method->[2];
238 484 my $mcode = $method->[3];
239 484 my $maddr = Scalar::Util::refaddr($mcode);
240 484 ▶ my $mevent = $POE::Declare::EVENT{$maddr} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $mevent = $POE::Declare::EVENT{$maddr} or next 26 458 484
241 26 my $mattr = $self->attr($mname);
242 26 ▶ if ( $mattr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mattr) 10 16 26
243 # Make sure the existing attribute is an event
244 10 ▶ next if $mattr->isa('POE::Declare::Meta::Event');
Branch: 1 branch
BranchTrueFalseTotal
if $mattr->isa("POE::Declare::Meta::Event") 10 0 10
245 0 Carp::croak("Event '$mname' in $name clashes with non-event in parent class");
246 0 next;
247 }
248  
249 # Add an attribute for the event
250 16 my $class = $mevent->[0];
251 16 my @param = @$mevent[1..$#$mevent];
252 16 $self->{attr}->{$mname} = $class->new(
253 name => $mname,
254 @param,
255 );
256 }
257  
258 # Get all the package fragments
259 my $code = join "\n", (
260 "package $name;",
261 "",
262 "BEGIN {",
263 " no strict 'refs';",
264 " delete \${\"\${name}::\"}{'meta'};",
265 " use strict;",
266 "}",
267 "",
268 "sub meta () { \$POE::Declare::META{'$name'} }",
269 map {
270 9 $attr->{$_}->as_perl
271 } sort keys %$attr
272 );
273  
274 # Load the code
275 9 if ( DEBUG ) {
276 # Compile the combined code via a temp file
277 my ($fh, $filename) = File::Temp::tempfile();
278 $fh->print("$code\n\n1;\n");
279 close $fh;
280 require $filename;
281 unlink $filename;
282  
283 # Print the debugging output
284 my @trace = map {
285 s/\s*[{;]$//;
286 s/^s/ s/;
287 s/^p/\np/;
288 "$_\n"
289 } grep {
290 /^(?:package|sub)\b/
291 } split /\n/, $code;
292 print STDERR @trace, "\n$name code saved as $filename\n\n";
293 } else {
294 0 ▶ eval("$code\n\n1;\n");
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine meta: not called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Pod documented
Branch: 3 branches
BranchTrueFalseTotal
$_[0]{"EventOne"} or return "" 1 0 1
$_[0]{"EventThree"} or return "" 1 0 1
$_[0]{"EventTwo"} or return "" 1 0 1
Condition0%0/2
$_[2] or 0
l!l
00
MC/DC0%0/1
$_[2] or 0
$_[2]
Truth table0%0/2
A
A= $_[2]
Aresult
11
00
295 9 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 9 9
296 9 ▶ Carp::croak("Failed to compile code for $name") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 9 9
297 }
298  
299 return (
300 9 $self->{compiled} = 1
301 );
302 }
303  
304 # sub compiled {
305 # $_[0]->{compiled};
306 # }
307  
308  
309  
310  
311  
312 #####################################################################
313 # Run-Time Support Methods
314  
315 # Resolve the inline states for a class
316 13 ▶sub _package_states {
Subroutine _package_states: called CC=3 SCAR=11.1
317 13 my $self = shift;
318 4 ▶ unless ( exists $self->{_package_states} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_package_states"}) 3 1 4
319 # Cache for speed reasons
320 $self->{_package_states} = [
321 sort map {
322 12 $_->name
323 } grep {
324 3 $_->isa('POE::Declare::Meta::Event')
325 } $self->attrs
326 ];
327 }
328 4 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 4 0 4
329 4 return @{$self->{_package_states}};
330 } else {
331 0 return $self->{_package_states};
332 }
333 }
334  
335 # Resolve the parameter list
336 5 ▶sub _params {
Subroutine _params: called CC=3 SCAR=11.1
337 5 my $self = shift;
338 5 ▶ unless ( exists $self->{_params} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_params"}) 3 2 5
339 # Cache for speed reasons
340 $self->{_params} = [
341 sort map {
342 8 $_->name
343 } grep {
344 3 $_->isa('POE::Declare::Meta::Param')
345 } $self->attrs
346 ];
347 }
348 5 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 5 0 5
349 5 return @{$self->{_params}};
350 } else {
351 0 return $self->{_params};
352 }
353 }
354  
355 # Resolve the message list
356 3 ▶sub _messages {
Subroutine _messages: called CC=3 SCAR=11.3
357 3 my $self = shift;
358 3 ▶ unless ( exists $self->{_messages} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_messages"}) 3 0 3
359 # Cache for speed reasons
360 $self->{_messages} = [
361 sort map {
362 3 $_->name
363 } grep {
364 3 $_->isa('POE::Declare::Meta::Message')
365 } $self->attrs
366 ];
367 }
368 3 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 3 0 3
369 3 return @{$self->{_messages}};
370 } else {
371 0 return $self->{_messages};
372 }
373 }
374  
375 # Resolve the timeout list
376 0 ▶sub _timeouts {
Subroutine _timeouts: not called CC=3 SCAR=24.8
377 0 my $self = shift;
378 0 ▶ unless ( exists $self->{_timeouts} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_timeouts"}) 0 0 0
379 # Cache for speed reasons
380 $self->{_timeouts} = [
381 sort map {
382 0 $_->name
383 } grep {
384 0 $_->isa('POE::Declare::Meta::Timeout')
385 } $self->attrs
386 ];
387 }
388 0 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
389 0 return @{$self->{_timeouts}};
390 } else {
391 0 return $self->{_timeouts};
392 }
393 }
394  
395 5 1;
396  
397 =pod
398  
399 =head1 SUPPORT
400  
401 Bugs should be always be reported via the CPAN bug tracker at
402  
403 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=POE-Declare>
404  
405 For other issues, or commercial enhancement or support, contact the author.
406  
407 =head1 AUTHORS
408  
409 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
410  
411 =head1 SEE ALSO
412  
413 L<POE>
414  
415 =head1 COPYRIGHT
416  
417 Copyright 2006 - 2012 Adam Kennedy.
418  
419 This program is free software; you can redistribute
420 it and/or modify it under the same terms as Perl itself.
421  
422 The full text of the license can be found in the
423 LICENSE file included with this module.
424  
425 =cut
« Declare.pm ↑ Summary Declare/Meta/Timeout.pm »