Add VERSION section to pods
[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     return;
60 }
61
62 sub _check_required_methods{
63     my($role, $applicant, $args) = @_;
64
65     if($args->{_to} eq 'role'){
66         $applicant->add_required_methods($role->get_required_method_list);
67     }
68     else{ # to class or instance
69         my $applicant_class_name = $applicant->name;
70
71         my @missing;
72         foreach my $method_name(@{$role->{required_methods}}){
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;
78         }
79         if(@missing){
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);
85         }
86     }
87
88     return;
89 }
90
91 sub _apply_methods{
92     my($role, $applicant, $args) = @_;
93
94     my $alias    = $args->{-alias};
95     my $excludes = $args->{-excludes};
96
97     foreach my $method_name($role->get_method_list){
98         next if $method_name eq 'meta';
99
100         my $code = $role->get_method_body($method_name);
101
102         if(!exists $excludes->{$method_name}){
103             if(!$applicant->has_method($method_name)){
104                 # The third argument $role is used in Role::Composite
105                 $applicant->add_method($method_name => $code, $role);
106             }
107         }
108
109         if(exists $alias->{$method_name}){
110             my $dstname = $alias->{$method_name};
111
112             my $dstcode = $applicant->get_method_body($dstname);
113
114             if(defined($dstcode) && $dstcode != $code){
115                 $role->throw_error("Cannot create a method alias if a local method of the same name exists");
116             }
117             else{
118                 $applicant->add_method($dstname => $code, $role);
119             }
120         }
121     }
122
123     return;
124 }
125
126 sub _apply_attributes{
127     my($role, $applicant, $args) = @_;
128
129     for my $attr_name ($role->get_attribute_list) {
130         next if $applicant->has_attribute($attr_name);
131
132         $applicant->add_attribute($attr_name => $role->get_attribute($attr_name));
133     }
134     return;
135 }
136
137 sub _apply_modifiers{
138     my($role, $applicant, $args) = @_;
139
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/) {
147         my $modifiers = $role->{"${modifier_type}_method_modifiers"}
148             or next;
149
150         my $add_modifier = "add_${modifier_type}_method_modifier";
151
152         foreach my $method_name (keys %{$modifiers}){
153             foreach my $code(@{ $modifiers->{$method_name} }){
154                 next if $applicant->{"_applied_$modifier_type"}{$method_name, $code}++; # skip applied modifiers
155                 $applicant->$add_modifier($method_name => $code);
156             }
157         }
158     }
159     return;
160 }
161
162 sub _append_roles{
163     my($role, $applicant, $args) = @_;
164
165     my $roles = ($args->{_to} eq 'role') ? $applicant->get_roles : $applicant->roles;
166
167     foreach my $r($role, @{$role->get_roles}){
168         if(!$applicant->does_role($r->name)){
169             push @{$roles}, $r;
170         }
171     }
172     return;
173 }
174
175 # Moose uses Application::ToInstance, Application::ToClass, Application::ToRole
176 sub apply {
177     my $self      = shift;
178     my $applicant = shift;
179
180     my %args = (@_ == 1) ? %{ $_[0] } : @_;
181
182     my $instance;
183
184     if($applicant->isa('Mouse::Meta::Class')){  # Application::ToClass
185         $args{_to} = 'class';
186     }
187     elsif($applicant->isa('Mouse::Meta::Role')){ # Application::ToRole
188         $args{_to} = 'role';
189     }
190     else{                                       # Appplication::ToInstance
191         $args{_to} = 'instance';
192         $instance = $applicant;
193
194         $applicant = (Mouse::Util::class_of($instance) || 'Mouse::Meta::Class')->create_anon_class(
195             superclasses => [ref $instance],
196             cache        => 1,
197         );
198     }
199
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     }
206
207     $args{aliased_methods} = {};
208     if(my $alias = $args{-alias}){
209         @{$args{aliased_methods}}{ values %{$alias} } = ();
210     }
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});
216         }
217         else{
218             $args{-excludes}{$excludes} = undef;
219         }
220     }
221
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
228
229     if(defined $instance){ # Application::ToInstance
230         # rebless instance
231         bless $instance, $applicant->name;
232         $applicant->_initialize_object($instance, $instance);
233     }
234
235     return;
236 }
237
238
239 sub combine {
240     my($role_class, @role_specs) = @_;
241
242     require 'Mouse/Meta/Role/Composite.pm'; # we don't want to create its namespace
243
244     my $composite = Mouse::Meta::Role::Composite->create_anon_role();
245
246     foreach my $role_spec (@role_specs) {
247         my($role_name, $args) = @{$role_spec};
248         $role_name->meta->apply($composite, %{$args});
249     }
250     return $composite;
251 }
252
253 for my $modifier_type (qw/before after around/) {
254
255     my $modifier = "${modifier_type}_method_modifiers";
256
257     my $add_method_modifier =  sub {
258         my ($self, $method_name, $method) = @_;
259
260         push @{ $self->{$modifier}->{$method_name} ||= [] }, $method;
261         return;
262     };
263
264     my $get_method_modifiers = sub {
265         my ($self, $method_name) = @_;
266         return @{ $self->{$modifier}->{$method_name} ||= [] }
267     };
268
269     no strict 'refs';
270     *{ 'add_' . $modifier_type . '_method_modifier'  } = $add_method_modifier;
271     *{ 'get_' . $modifier_type . '_method_modifiers' } = $get_method_modifiers;
272
273     # has_${modifier_type}_method_modifiers is moved into t::lib::Test::Mouse
274 }
275
276 sub add_override_method_modifier{
277     my($self, $method_name, $method) = @_;
278
279     if($self->has_method($method_name)){
280         # This error happens in the override keyword or during role composition,
281         # so I added a message, "A local method of ...", only for compatibility (gfx)
282         $self->throw_error("Cannot add an override of method '$method_name' "
283                    . "because there is a local version of '$method_name'"
284                    . "(A local method of the same name as been found)");
285     }
286
287     $self->{override_method_modifiers}->{$method_name} = $method;
288 }
289
290 sub get_override_method_modifier {
291     my ($self, $method_name) = @_;
292     return $self->{override_method_modifiers}->{$method_name};
293 }
294
295 sub does_role {
296     my ($self, $role_name) = @_;
297
298     (defined $role_name)
299         || $self->throw_error("You must supply a role name to look for");
300
301     # if we are it,.. then return true
302     return 1 if $role_name eq $self->name;
303     # otherwise.. check our children
304     for my $role (@{ $self->get_roles }) {
305         return 1 if $role->does_role($role_name);
306     }
307     return 0;
308 }
309
310 1;
311 __END__
312
313 =head1 NAME
314
315 Mouse::Meta::Role - The Mouse Role metaclass
316
317 =head1 VERSION
318
319 This document describes Mouse version 0.37_06
320
321 =head1 SEE ALSO
322
323 L<Moose::Meta::Role>
324
325 =cut