| File | /usr/local/lib/perl5/site_perl/5.10.1/darwin-2level/Moose/Meta/Role/Composite.pm |
| Statements Executed | 22 |
| Statement Execution Time | 700µs |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 16µs | 22µs | Moose::Meta::Role::Composite::BEGIN@3 |
| 1 | 1 | 1 | 13µs | 107µs | Moose::Meta::Role::Composite::BEGIN@13 |
| 1 | 1 | 1 | 11µs | 165µs | Moose::Meta::Role::Composite::BEGIN@5 |
| 1 | 1 | 1 | 10µs | 54µs | Moose::Meta::Role::Composite::BEGIN@7 |
| 1 | 1 | 1 | 8µs | 27µs | Moose::Meta::Role::Composite::BEGIN@4 |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::Role::Composite::__ANON__[:30] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::Role::Composite::__ANON__[:60] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::Role::Composite::add_method |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::Role::Composite::apply_params |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::Role::Composite::get_method |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::Role::Composite::get_method_list |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::Role::Composite::has_method |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::Role::Composite::new |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::Role::Composite::reinitialize |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package Moose::Meta::Role::Composite; | ||||
| 2 | |||||
| 3 | 3 | 22µs | 2 | 28µs | # spent 22µs (16+6) within Moose::Meta::Role::Composite::BEGIN@3 which was called
# once (16µs+6µs) by Moose::BEGIN@27 at line 3 # spent 22µs making 1 call to Moose::Meta::Role::Composite::BEGIN@3
# spent 6µs making 1 call to strict::import |
| 4 | 3 | 21µs | 2 | 45µs | # spent 27µs (8+18) within Moose::Meta::Role::Composite::BEGIN@4 which was called
# once (8µs+18µs) by Moose::BEGIN@27 at line 4 # spent 27µs making 1 call to Moose::Meta::Role::Composite::BEGIN@4
# spent 18µs making 1 call to warnings::import |
| 5 | 3 | 33µs | 2 | 319µs | # spent 165µs (11+154) within Moose::Meta::Role::Composite::BEGIN@5 which was called
# once (11µs+154µs) by Moose::BEGIN@27 at line 5 # spent 165µs making 1 call to Moose::Meta::Role::Composite::BEGIN@5
# spent 154µs making 1 call to metaclass::import |
| 6 | |||||
| 7 | 3 | 80µs | 2 | 99µs | # spent 54µs (10+45) within Moose::Meta::Role::Composite::BEGIN@7 which was called
# once (10µs+45µs) by Moose::BEGIN@27 at line 7 # spent 54µs making 1 call to Moose::Meta::Role::Composite::BEGIN@7
# spent 45µs making 1 call to Exporter::import |
| 8 | |||||
| 9 | 1 | 700ns | our $VERSION = '0.98'; | ||
| 10 | 1 | 22µs | $VERSION = eval $VERSION; | ||
| 11 | 1 | 400ns | our $AUTHORITY = 'cpan:STEVAN'; | ||
| 12 | |||||
| 13 | 3 | 491µs | 2 | 200µs | # spent 107µs (13+94) within Moose::Meta::Role::Composite::BEGIN@13 which was called
# once (13µs+94µs) by Moose::BEGIN@27 at line 13 # spent 107µs making 1 call to Moose::Meta::Role::Composite::BEGIN@13
# spent 94µs making 1 call to base::import |
| 14 | |||||
| 15 | # NOTE: | ||||
| 16 | # we need to override the ->name | ||||
| 17 | # method from Class::MOP::Package | ||||
| 18 | # since we don't have an actual | ||||
| 19 | # package for this. | ||||
| 20 | # - SL | ||||
| 21 | 1 | 4µs | 2 | 312µs | __PACKAGE__->meta->add_attribute('name' => (reader => 'name')); # spent 289µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 22µs making 1 call to Moose::Meta::Role::Composite::meta |
| 22 | |||||
| 23 | # NOTE: | ||||
| 24 | # Again, since we don't have a real | ||||
| 25 | # package to store our methods in, | ||||
| 26 | # we use a HASH ref instead. | ||||
| 27 | # - SL | ||||
| 28 | __PACKAGE__->meta->add_attribute('_methods' => ( | ||||
| 29 | reader => '_method_map', | ||||
| 30 | default => sub { {} } | ||||
| 31 | 1 | 3µs | 2 | 277µs | )); # spent 266µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 11µs making 1 call to Moose::Meta::Role::Composite::meta |
| 32 | |||||
| 33 | 1 | 2µs | 2 | 331µs | __PACKAGE__->meta->add_attribute( # spent 320µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 11µs making 1 call to Moose::Meta::Role::Composite::meta |
| 34 | 'application_role_summation_class', | ||||
| 35 | reader => 'application_role_summation_class', | ||||
| 36 | default => 'Moose::Meta::Role::Application::RoleSummation', | ||||
| 37 | ); | ||||
| 38 | |||||
| 39 | sub new { | ||||
| 40 | my ($class, %params) = @_; | ||||
| 41 | |||||
| 42 | # the roles param is required ... | ||||
| 43 | foreach ( @{$params{roles}} ) { | ||||
| 44 | unless ( $_->isa('Moose::Meta::Role') ) { | ||||
| 45 | require Moose; | ||||
| 46 | Moose->throw_error("The list of roles must be instances of Moose::Meta::Role, not $_"); | ||||
| 47 | } | ||||
| 48 | } | ||||
| 49 | |||||
| 50 | my @composition_roles = map { | ||||
| 51 | $_->composition_class_roles | ||||
| 52 | } @{ $params{roles} }; | ||||
| 53 | |||||
| 54 | if (@composition_roles) { | ||||
| 55 | my $meta = Moose::Meta::Class->create_anon_class( | ||||
| 56 | superclasses => [ $class ], | ||||
| 57 | roles => [ @composition_roles ], | ||||
| 58 | cache => 1, | ||||
| 59 | ); | ||||
| 60 | $meta->add_method(meta => sub { $meta }); | ||||
| 61 | $class = $meta->name; | ||||
| 62 | } | ||||
| 63 | |||||
| 64 | # and the name is created from the | ||||
| 65 | # roles if one has not been provided | ||||
| 66 | $params{name} ||= (join "|" => map { $_->name } @{$params{roles}}); | ||||
| 67 | $class->_new(\%params); | ||||
| 68 | } | ||||
| 69 | |||||
| 70 | # This is largely a cope of what's in Moose::Meta::Role (itself | ||||
| 71 | # largely a copy of Class::MOP::Class). However, we can't actually | ||||
| 72 | # call add_package_symbol, because there's no package to which which | ||||
| 73 | # add the symbol. | ||||
| 74 | sub add_method { | ||||
| 75 | my ($self, $method_name, $method) = @_; | ||||
| 76 | |||||
| 77 | unless ( defined $method_name && $method_name ) { | ||||
| 78 | Moose->throw_error("You must define a method name"); | ||||
| 79 | } | ||||
| 80 | |||||
| 81 | my $body; | ||||
| 82 | if (blessed($method)) { | ||||
| 83 | $body = $method->body; | ||||
| 84 | if ($method->package_name ne $self->name) { | ||||
| 85 | $method = $method->clone( | ||||
| 86 | package_name => $self->name, | ||||
| 87 | name => $method_name | ||||
| 88 | ) if $method->can('clone'); | ||||
| 89 | } | ||||
| 90 | } | ||||
| 91 | else { | ||||
| 92 | $body = $method; | ||||
| 93 | $method = $self->wrap_method_body( body => $body, name => $method_name ); | ||||
| 94 | } | ||||
| 95 | |||||
| 96 | $self->_method_map->{$method_name} = $method; | ||||
| 97 | } | ||||
| 98 | |||||
| 99 | sub get_method_list { | ||||
| 100 | my $self = shift; | ||||
| 101 | return keys %{ $self->_method_map }; | ||||
| 102 | } | ||||
| 103 | |||||
| 104 | sub has_method { | ||||
| 105 | my ($self, $method_name) = @_; | ||||
| 106 | |||||
| 107 | return exists $self->_method_map->{$method_name}; | ||||
| 108 | } | ||||
| 109 | |||||
| 110 | sub get_method { | ||||
| 111 | my ($self, $method_name) = @_; | ||||
| 112 | |||||
| 113 | return $self->_method_map->{$method_name}; | ||||
| 114 | } | ||||
| 115 | |||||
| 116 | sub apply_params { | ||||
| 117 | my ($self, $role_params) = @_; | ||||
| 118 | Class::MOP::load_class($self->application_role_summation_class); | ||||
| 119 | |||||
| 120 | $self->application_role_summation_class->new( | ||||
| 121 | role_params => $role_params, | ||||
| 122 | )->apply($self); | ||||
| 123 | |||||
| 124 | return $self; | ||||
| 125 | } | ||||
| 126 | |||||
| 127 | sub reinitialize { | ||||
| 128 | my ( $class, $old_meta, @args ) = @_; | ||||
| 129 | |||||
| 130 | Moose->throw_error( | ||||
| 131 | 'Moose::Meta::Role::Composite instances can only be reinitialized from an existing metaclass instance' | ||||
| 132 | ) | ||||
| 133 | if !blessed $old_meta | ||||
| 134 | || !$old_meta->isa('Moose::Meta::Role::Composite'); | ||||
| 135 | |||||
| 136 | my %existing_classes = map { $_ => $old_meta->$_() } qw( | ||||
| 137 | application_role_summation_class | ||||
| 138 | ); | ||||
| 139 | |||||
| 140 | return $old_meta->meta->clone_object( $old_meta, %existing_classes, @args ); | ||||
| 141 | } | ||||
| 142 | |||||
| 143 | 1 | 21µs | 1; | ||
| 144 | |||||
| 145 | __END__ | ||||
| 146 | |||||
| 147 | =pod | ||||
| 148 | |||||
| 149 | =head1 NAME | ||||
| 150 | |||||
| 151 | Moose::Meta::Role::Composite - An object to represent the set of roles | ||||
| 152 | |||||
| 153 | =head1 DESCRIPTION | ||||
| 154 | |||||
| 155 | A composite is a role that consists of a set of two or more roles. | ||||
| 156 | |||||
| 157 | The API of a composite role is almost identical to that of a regular | ||||
| 158 | role. | ||||
| 159 | |||||
| 160 | =head1 INHERITANCE | ||||
| 161 | |||||
| 162 | C<Moose::Meta::Role::Composite> is a subclass of L<Moose::Meta::Role>. | ||||
| 163 | |||||
| 164 | =head2 METHODS | ||||
| 165 | |||||
| 166 | =over 4 | ||||
| 167 | |||||
| 168 | =item B<< Moose::Meta::Role::Composite->new(%options) >> | ||||
| 169 | |||||
| 170 | This returns a new composite role object. It accepts the same | ||||
| 171 | options as its parent class, with a few changes: | ||||
| 172 | |||||
| 173 | =over 8 | ||||
| 174 | |||||
| 175 | =item * roles | ||||
| 176 | |||||
| 177 | This option is an array reference containing a list of | ||||
| 178 | L<Moose::Meta::Role> object. This is a required option. | ||||
| 179 | |||||
| 180 | =item * name | ||||
| 181 | |||||
| 182 | If a name is not given, one is generated from the roles provided. | ||||
| 183 | |||||
| 184 | =item * apply_params(\%role_params) | ||||
| 185 | |||||
| 186 | Creates a new RoleSummation role application with C<%role_params> and applies | ||||
| 187 | the composite role to it. The RoleSummation role application class used is | ||||
| 188 | determined by the composite role's C<application_role_summation_class> | ||||
| 189 | attribute. | ||||
| 190 | |||||
| 191 | =item * reinitialize($metaclass) | ||||
| 192 | |||||
| 193 | Like C<< Class::MOP::Package->reinitialize >>, but doesn't allow passing a | ||||
| 194 | string with the package name, as there is no real package for composite roles. | ||||
| 195 | |||||
| 196 | =back | ||||
| 197 | |||||
| 198 | =back | ||||
| 199 | |||||
| 200 | =head1 BUGS | ||||
| 201 | |||||
| 202 | See L<Moose/BUGS> for details on reporting bugs. | ||||
| 203 | |||||
| 204 | =head1 AUTHOR | ||||
| 205 | |||||
| 206 | Stevan Little E<lt>stevan@iinteractive.comE<gt> | ||||
| 207 | |||||
| 208 | =head1 COPYRIGHT AND LICENSE | ||||
| 209 | |||||
| 210 | Copyright 2006-2010 by Infinity Interactive, Inc. | ||||
| 211 | |||||
| 212 | L<http://www.iinteractive.com> | ||||
| 213 | |||||
| 214 | This library is free software; you can redistribute it and/or modify | ||||
| 215 | it under the same terms as Perl itself. | ||||
| 216 | |||||
| 217 | =cut |