Commit | Line | Data |
a2227e71 |
1 | package Mouse::Meta::Role; |
bc69ee88 |
2 | use Mouse::Util qw(:meta not_supported english_list); # enables strict and warnings |
74be9f76 |
3 | |
6d28c5cf |
4 | use Mouse::Meta::Module; |
f3bb863f |
5 | our @ISA = qw(Mouse::Meta::Module); |
a2227e71 |
6 | |
6cfa1e5e |
7 | sub method_metaclass(){ 'Mouse::Meta::Role::Method' } # required for get_method() |
8 | |
8e64d0fa |
9 | sub _construct_meta { |
acf0f643 |
10 | my $class = shift; |
7a50b450 |
11 | |
acf0f643 |
12 | my %args = @_; |
13 | |
3a63a2e7 |
14 | $args{methods} ||= {}; |
59089ec3 |
15 | $args{attributes} ||= {}; |
16 | $args{required_methods} ||= []; |
47f36c05 |
17 | $args{roles} ||= []; |
274b6cce |
18 | |
9009aca1 |
19 | my $self = bless \%args, ref($class) || $class; |
20 | if($class ne __PACKAGE__){ |
21 | $self->meta->_initialize_object($self, \%args); |
22 | } |
7a50b450 |
23 | |
9009aca1 |
24 | return $self; |
7a50b450 |
25 | } |
26 | |
27 | sub create_anon_role{ |
28 | my $self = shift; |
29 | return $self->create(undef, @_); |
30 | } |
31 | |
32 | sub is_anon_role{ |
33 | return exists $_[0]->{anon_serial_id}; |
acf0f643 |
34 | } |
a2227e71 |
35 | |
afc73948 |
36 | sub get_roles { $_[0]->{roles} } |
37 | |
6cfa1e5e |
38 | sub get_required_method_list{ |
39 | return @{ $_[0]->{required_methods} }; |
40 | } |
afc73948 |
41 | |
59089ec3 |
42 | sub add_required_methods { |
ea249879 |
43 | my($self, @methods) = @_; |
71e7b544 |
44 | my %required = map{ $_ => 1 } @{$self->{required_methods}}; |
45 | push @{$self->{required_methods}}, grep{ !$required{$_}++ && !$self->has_method($_) } @methods; |
46 | return; |
59089ec3 |
47 | } |
48 | |
6cfa1e5e |
49 | sub requires_method { |
50 | my($self, $name) = @_; |
51 | return scalar( grep{ $_ eq $name } @{ $self->{required_methods} } ) != 0; |
52 | } |
53 | |
274b6cce |
54 | sub add_attribute { |
55 | my $self = shift; |
56 | my $name = shift; |
6cfa1e5e |
57 | |
58 | $self->{attributes}->{$name} = (@_ == 1) ? $_[0] : { @_ }; |
c9313657 |
59 | return; |
da0c885d |
60 | } |
61 | |
3a63a2e7 |
62 | sub _check_required_methods{ |
71e7b544 |
63 | my($role, $applicant, $args) = @_; |
3a63a2e7 |
64 | |
71e7b544 |
65 | if($args->{_to} eq 'role'){ |
66 | $applicant->add_required_methods($role->get_required_method_list); |
67 | } |
68 | else{ # to class or instance |
2d2e77f9 |
69 | my $applicant_class_name = $applicant->name; |
70 | |
7a50b450 |
71 | my @missing; |
3a63a2e7 |
72 | foreach my $method_name(@{$role->{required_methods}}){ |
2d2e77f9 |
73 | next if exists $args->{aliased_methods}{$method_name}; |
74 | next if exists $role->{methods}{$method_name}; |
75 | next if $applicant_class_name->can($method_name); |
76 | |
77 | push @missing, $method_name; |
3a63a2e7 |
78 | } |
7a50b450 |
79 | if(@missing){ |
2d2e77f9 |
80 | $role->throw_error(sprintf "'%s' requires the method%s %s to be implemented by '%s'", |
81 | $role->name, |
82 | (@missing == 1 ? '' : 's'), # method or methods |
83 | english_list(map{ sprintf q{'%s'}, $_ } @missing), |
84 | $applicant_class_name); |
7a50b450 |
85 | } |
86 | } |
2e92bb89 |
87 | |
3a63a2e7 |
88 | return; |
2e92bb89 |
89 | } |
90 | |
3a63a2e7 |
91 | sub _apply_methods{ |
71e7b544 |
92 | my($role, $applicant, $args) = @_; |
da0c885d |
93 | |
7a50b450 |
94 | my $alias = $args->{-alias}; |
95 | my $excludes = $args->{-excludes}; |
e0b163e1 |
96 | |
3a63a2e7 |
97 | foreach my $method_name($role->get_method_list){ |
98 | next if $method_name eq 'meta'; |
99 | |
71e7b544 |
100 | my $code = $role->get_method_body($method_name); |
6cfa1e5e |
101 | |
2d2e77f9 |
102 | if(!exists $excludes->{$method_name}){ |
71e7b544 |
103 | if(!$applicant->has_method($method_name)){ |
2d2e77f9 |
104 | # The third argument $role is used in Role::Composite |
71e7b544 |
105 | $applicant->add_method($method_name => $code, $role); |
6cfa1e5e |
106 | } |
2e92bb89 |
107 | } |
2e92bb89 |
108 | |
2d2e77f9 |
109 | if(exists $alias->{$method_name}){ |
3a63a2e7 |
110 | my $dstname = $alias->{$method_name}; |
6cfa1e5e |
111 | |
71e7b544 |
112 | my $dstcode = $applicant->get_method_body($dstname); |
7a50b450 |
113 | |
114 | if(defined($dstcode) && $dstcode != $code){ |
71e7b544 |
115 | $role->throw_error("Cannot create a method alias if a local method of the same name exists"); |
21498b08 |
116 | } |
3a63a2e7 |
117 | else{ |
71e7b544 |
118 | $applicant->add_method($dstname => $code, $role); |
2e92bb89 |
119 | } |
59089ec3 |
120 | } |
121 | } |
122 | |
3a63a2e7 |
123 | return; |
124 | } |
125 | |
126 | sub _apply_attributes{ |
71e7b544 |
127 | my($role, $applicant, $args) = @_; |
3a63a2e7 |
128 | |
71e7b544 |
129 | for my $attr_name ($role->get_attribute_list) { |
130 | next if $applicant->has_attribute($attr_name); |
3a63a2e7 |
131 | |
71e7b544 |
132 | $applicant->add_attribute($attr_name => $role->get_attribute($attr_name)); |
da0c885d |
133 | } |
3a63a2e7 |
134 | return; |
135 | } |
136 | |
137 | sub _apply_modifiers{ |
71e7b544 |
138 | my($role, $applicant, $args) = @_; |
3a63a2e7 |
139 | |
2d2e77f9 |
140 | if(my $modifiers = $role->{override_method_modifiers}){ |
141 | foreach my $method_name (keys %{$modifiers}){ |
142 | $applicant->add_override_method_modifier($method_name => $modifiers->{$method_name}); |
143 | } |
144 | } |
145 | |
146 | for my $modifier_type (qw/before around after/) { |
71e7b544 |
147 | my $modifiers = $role->{"${modifier_type}_method_modifiers"} |
148 | or next; |
149 | |
3a63a2e7 |
150 | my $add_modifier = "add_${modifier_type}_method_modifier"; |
d99db7b6 |
151 | |
71e7b544 |
152 | foreach my $method_name (keys %{$modifiers}){ |
2d2e77f9 |
153 | foreach my $code(@{ $modifiers->{$method_name} }){ |
154 | next if $applicant->{"_applied_$modifier_type"}{$method_name, $code}++; # skip applied modifiers |
71e7b544 |
155 | $applicant->$add_modifier($method_name => $code); |
d99db7b6 |
156 | } |
157 | } |
158 | } |
3a63a2e7 |
159 | return; |
da0c885d |
160 | } |
0fc8adbc |
161 | |
3a63a2e7 |
162 | sub _append_roles{ |
71e7b544 |
163 | my($role, $applicant, $args) = @_; |
21498b08 |
164 | |
71e7b544 |
165 | my $roles = ($args->{_to} eq 'role') ? $applicant->get_roles : $applicant->roles; |
3a63a2e7 |
166 | |
167 | foreach my $r($role, @{$role->get_roles}){ |
71e7b544 |
168 | if(!$applicant->does_role($r->name)){ |
3a63a2e7 |
169 | push @{$roles}, $r; |
21498b08 |
170 | } |
171 | } |
3a63a2e7 |
172 | return; |
173 | } |
21498b08 |
174 | |
3a63a2e7 |
175 | # Moose uses Application::ToInstance, Application::ToClass, Application::ToRole |
176 | sub apply { |
7a50b450 |
177 | my $self = shift; |
178 | my $applicant = shift; |
7a50b450 |
179 | |
71e7b544 |
180 | my %args = (@_ == 1) ? %{ $_[0] } : @_; |
60b5c3be |
181 | |
71e7b544 |
182 | if($applicant->isa('Mouse::Meta::Class')){ # Application::ToClass |
183 | $args{_to} = 'class'; |
184 | } |
185 | elsif($applicant->isa('Mouse::Meta::Role')){ # Application::ToRole |
186 | $args{_to} = 'role'; |
187 | } |
188 | else{ # Appplication::ToInstance |
189 | $args{_to} = 'instance'; |
60b5c3be |
190 | |
71e7b544 |
191 | my $metaclass = $applicant->meta->create_anon_class( |
192 | superclasses => [ref $applicant], |
193 | cache => 1, |
194 | ); |
195 | bless $applicant, $metaclass->name; # rebless |
60b5c3be |
196 | |
71e7b544 |
197 | $applicant = $metaclass; |
198 | } |
60b5c3be |
199 | |
71e7b544 |
200 | if($args{alias} && !exists $args{-alias}){ |
201 | $args{-alias} = $args{alias}; |
202 | } |
203 | if($args{excludes} && !exists $args{-excludes}){ |
204 | $args{-excludes} = $args{excludes}; |
205 | } |
60b5c3be |
206 | |
2d2e77f9 |
207 | $args{aliased_methods} = {}; |
71e7b544 |
208 | if(my $alias = $args{-alias}){ |
2d2e77f9 |
209 | @{$args{aliased_methods}}{ values %{$alias} } = (); |
60b5c3be |
210 | } |
71e7b544 |
211 | |
212 | if(my $excludes = $args{-excludes}){ |
213 | $args{-excludes} = {}; # replace with a hash ref |
214 | if(ref $excludes){ |
215 | %{$args{-excludes}} = (map{ $_ => undef } @{$excludes}); |
60b5c3be |
216 | } |
217 | else{ |
71e7b544 |
218 | $args{-excludes}{$excludes} = undef; |
60b5c3be |
219 | } |
220 | } |
221 | |
71e7b544 |
222 | $self->_check_required_methods($applicant, \%args); |
223 | $self->_apply_attributes($applicant, \%args); |
224 | $self->_apply_methods($applicant, \%args); |
225 | $self->_apply_modifiers($applicant, \%args); |
226 | $self->_append_roles($applicant, \%args); |
227 | return; |
228 | } |
229 | |
21498b08 |
230 | |
71e7b544 |
231 | sub combine { |
232 | my($role_class, @role_specs) = @_; |
21498b08 |
233 | |
71e7b544 |
234 | require 'Mouse/Meta/Role/Composite.pm'; # we don't want to create its namespace |
7a50b450 |
235 | |
71e7b544 |
236 | my $composite = Mouse::Meta::Role::Composite->create_anon_role(); |
237 | |
238 | foreach my $role_spec (@role_specs) { |
239 | my($role_name, $args) = @{$role_spec}; |
240 | $role_name->meta->apply($composite, %{$args}); |
21498b08 |
241 | } |
71e7b544 |
242 | return $composite; |
21498b08 |
243 | } |
244 | |
6cfa1e5e |
245 | for my $modifier_type (qw/before after around/) { |
3a63a2e7 |
246 | |
247 | my $modifier = "${modifier_type}_method_modifiers"; |
c9313657 |
248 | |
3a63a2e7 |
249 | my $add_method_modifier = sub { |
fc0e0bbd |
250 | my ($self, $method_name, $method) = @_; |
251 | |
3a63a2e7 |
252 | push @{ $self->{$modifier}->{$method_name} ||= [] }, $method; |
253 | return; |
fc0e0bbd |
254 | }; |
c9313657 |
255 | |
3a63a2e7 |
256 | my $get_method_modifiers = sub { |
257 | my ($self, $method_name) = @_; |
258 | return @{ $self->{$modifier}->{$method_name} ||= [] } |
c2f128e7 |
259 | }; |
c2f128e7 |
260 | |
3a63a2e7 |
261 | no strict 'refs'; |
262 | *{ 'add_' . $modifier_type . '_method_modifier' } = $add_method_modifier; |
263 | *{ 'get_' . $modifier_type . '_method_modifiers' } = $get_method_modifiers; |
c9313657 |
264 | |
265 | # has_${modifier_type}_method_modifiers is moved into t::lib::Test::Mouse |
3a63a2e7 |
266 | } |
47f36c05 |
267 | |
6cfa1e5e |
268 | sub add_override_method_modifier{ |
269 | my($self, $method_name, $method) = @_; |
270 | |
60b5c3be |
271 | if($self->has_method($method_name)){ |
272 | # This error happens in the override keyword or during role composition, |
273 | # so I added a message, "A local method of ...", only for compatibility (gfx) |
8e64d0fa |
274 | $self->throw_error("Cannot add an override of method '$method_name' " |
60b5c3be |
275 | . "because there is a local version of '$method_name'" |
276 | . "(A local method of the same name as been found)"); |
277 | } |
6cfa1e5e |
278 | |
279 | $self->{override_method_modifiers}->{$method_name} = $method; |
280 | } |
281 | |
8e64d0fa |
282 | sub get_override_method_modifier { |
283 | my ($self, $method_name) = @_; |
284 | return $self->{override_method_modifiers}->{$method_name}; |
6cfa1e5e |
285 | } |
286 | |
67199842 |
287 | sub does_role { |
288 | my ($self, $role_name) = @_; |
289 | |
290 | (defined $role_name) |
fce211ae |
291 | || $self->throw_error("You must supply a role name to look for"); |
67199842 |
292 | |
293 | # if we are it,.. then return true |
294 | return 1 if $role_name eq $self->name; |
3a63a2e7 |
295 | # otherwise.. check our children |
296 | for my $role (@{ $self->get_roles }) { |
67199842 |
297 | return 1 if $role->does_role($role_name); |
298 | } |
299 | return 0; |
300 | } |
301 | |
a2227e71 |
302 | 1; |
1820fffe |
303 | __END__ |
304 | |
305 | =head1 NAME |
306 | |
307 | Mouse::Meta::Role - The Mouse Role metaclass |
308 | |
309 | =head1 SEE ALSO |
310 | |
311 | L<Moose::Meta::Role> |
312 | |
313 | =cut |