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