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