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