1 package Mouse::Meta::Role;
2 use Mouse::Util qw(:meta not_supported english_list); # enables strict and warnings
4 use Mouse::Meta::Module;
5 our @ISA = qw(Mouse::Meta::Module);
7 sub method_metaclass(){ 'Mouse::Meta::Role::Method' } # required for get_method()
14 $args{methods} ||= {};
15 $args{attributes} ||= {};
16 $args{required_methods} ||= [];
19 my $self = bless \%args, ref($class) || $class;
20 if($class ne __PACKAGE__){
21 $self->meta->_initialize_object($self, \%args);
29 return $self->create(undef, @_);
33 return exists $_[0]->{anon_serial_id};
36 sub get_roles { $_[0]->{roles} }
38 sub get_required_method_list{
39 return @{ $_[0]->{required_methods} };
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;
50 my($self, $name) = @_;
51 return scalar( grep{ $_ eq $name } @{ $self->{required_methods} } ) != 0;
58 $self->{attributes}->{$name} = (@_ == 1) ? $_[0] : { @_ };
61 sub _check_required_methods{
62 my($role, $applicant, $args) = @_;
64 if($args->{_to} eq 'role'){
65 $applicant->add_required_methods($role->get_required_method_list);
67 else{ # to class or instance
68 my $class_name = $applicant->name;
69 my $role_name = $role->name;
71 foreach my $method_name(@{$role->{required_methods}}){
72 if(!($class_name->can($method_name) || exists $args->{to_be_provided}{$method_name})){
73 push @missing, $method_name;
77 $role->throw_error("'$role_name' requires the "
78 . (@missing == 1 ? 'method' : 'methods')
80 . english_list(map{ sprintf q{'%s'}, $_ } @missing)
81 . " to be implemented by '$class_name'");
89 my($role, $applicant, $args) = @_;
91 my $role_name = $role->name;
92 my $class_name = $applicant->name;
94 my $alias = $args->{-alias};
95 my $excludes = $args->{-excludes};
97 foreach my $method_name($role->get_method_list){
98 next if $method_name eq 'meta';
100 my $code = $role->get_method_body($method_name);
102 if($excludes && !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);
109 if($alias && $alias->{$method_name}){
110 my $dstname = $alias->{$method_name};
112 my $dstcode = $applicant->get_method_body($dstname);
114 if(defined($dstcode) && $dstcode != $code){
115 $role->throw_error("Cannot create a method alias if a local method of the same name exists");
118 $applicant->add_method($dstname => $code, $role);
126 sub _apply_attributes{
127 my($role, $applicant, $args) = @_;
129 for my $attr_name ($role->get_attribute_list) {
130 next if $applicant->has_attribute($attr_name);
132 $applicant->add_attribute($attr_name => $role->get_attribute($attr_name));
137 sub _apply_modifiers{
138 my($role, $applicant, $args) = @_;
140 for my $modifier_type (qw/override before around after/) {
141 my $modifiers = $role->{"${modifier_type}_method_modifiers"}
144 my $add_modifier = "add_${modifier_type}_method_modifier";
146 foreach my $method_name (keys %{$modifiers}){
147 my $modifier_codes = $modifiers->{$method_name};
148 foreach my $code(ref($modifier_codes) eq 'ARRAY' ? @{$modifier_codes} : $modifier_codes){
149 next if $applicant->{"_$modifier_type"}{$code}++; # skip applied modifiers
150 $applicant->$add_modifier($method_name => $code);
158 my($role, $applicant, $args) = @_;
160 my $roles = ($args->{_to} eq 'role') ? $applicant->get_roles : $applicant->roles;
162 foreach my $r($role, @{$role->get_roles}){
163 if(!$applicant->does_role($r->name)){
170 # Moose uses Application::ToInstance, Application::ToClass, Application::ToRole
173 my $applicant = shift;
175 my %args = (@_ == 1) ? %{ $_[0] } : @_;
177 if($applicant->isa('Mouse::Meta::Class')){ # Application::ToClass
178 $args{_to} = 'class';
180 elsif($applicant->isa('Mouse::Meta::Role')){ # Application::ToRole
183 else{ # Appplication::ToInstance
184 $args{_to} = 'instance';
186 my $metaclass = $applicant->meta->create_anon_class(
187 superclasses => [ref $applicant],
190 bless $applicant, $metaclass->name; # rebless
192 $applicant = $metaclass;
195 if($args{alias} && !exists $args{-alias}){
196 $args{-alias} = $args{alias};
198 if($args{excludes} && !exists $args{-excludes}){
199 $args{-excludes} = $args{excludes};
202 # In Moose it is called 'aliased methods'
203 $args{to_be_provided} = {};
204 if(my $alias = $args{-alias}){
205 @{$args{to_be_provided}}{ values %{$alias} } = ();
207 @{ $args{to_be_provided} }{ $self->get_method_list } = ();
209 if(my $excludes = $args{-excludes}){
210 $args{-excludes} = {}; # replace with a hash ref
212 %{$args{-excludes}} = (map{ $_ => undef } @{$excludes});
215 $args{-excludes}{$excludes} = undef;
219 $self->_check_required_methods($applicant, \%args);
220 $self->_apply_attributes($applicant, \%args);
221 $self->_apply_methods($applicant, \%args);
222 $self->_apply_modifiers($applicant, \%args);
223 $self->_append_roles($applicant, \%args);
229 my($role_class, @role_specs) = @_;
231 require 'Mouse/Meta/Role/Composite.pm'; # we don't want to create its namespace
233 my $composite = Mouse::Meta::Role::Composite->create_anon_role();
235 foreach my $role_spec (@role_specs) {
236 my($role_name, $args) = @{$role_spec};
237 $role_name->meta->apply($composite, %{$args});
242 for my $modifier_type (qw/before after around/) {
244 my $modifier = "${modifier_type}_method_modifiers";
245 my $add_method_modifier = sub {
246 my ($self, $method_name, $method) = @_;
248 push @{ $self->{$modifier}->{$method_name} ||= [] }, $method;
251 my $has_method_modifiers = sub{
252 my($self, $method_name) = @_;
253 my $m = $self->{$modifier}->{$method_name};
254 return $m && @{$m} != 0;
256 my $get_method_modifiers = sub {
257 my ($self, $method_name) = @_;
258 return @{ $self->{$modifier}->{$method_name} ||= [] }
262 *{ 'add_' . $modifier_type . '_method_modifier' } = $add_method_modifier;
263 *{ 'has_' . $modifier_type . '_method_modifiers' } = $has_method_modifiers;
264 *{ 'get_' . $modifier_type . '_method_modifiers' } = $get_method_modifiers;
267 sub add_override_method_modifier{
268 my($self, $method_name, $method) = @_;
270 if($self->has_method($method_name)){
271 # This error happens in the override keyword or during role composition,
272 # so I added a message, "A local method of ...", only for compatibility (gfx)
273 $self->throw_error("Cannot add an override of method '$method_name' "
274 . "because there is a local version of '$method_name'"
275 . "(A local method of the same name as been found)");
278 $self->{override_method_modifiers}->{$method_name} = $method;
281 sub has_override_method_modifier {
282 my ($self, $method_name) = @_;
283 return exists $self->{override_method_modifiers}->{$method_name};
286 sub get_override_method_modifier {
287 my ($self, $method_name) = @_;
288 return $self->{override_method_modifiers}->{$method_name};
291 sub get_method_modifier_list {
292 my($self, $modifier_type) = @_;
294 return keys %{ $self->{$modifier_type . '_method_modifiers'} };
297 # This is currently not passing all the Moose tests.
299 my ($self, $role_name) = @_;
302 || $self->throw_error("You must supply a role name to look for");
304 # if we are it,.. then return true
305 return 1 if $role_name eq $self->name;
306 # otherwise.. check our children
307 for my $role (@{ $self->get_roles }) {
308 return 1 if $role->does_role($role_name);
320 Mouse::Meta::Role - The Mouse Role metaclass