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 calculate_all_roles {
41 return grep { !$seen{ $_->name }++ }
42 ($self, map { $_->calculate_all_roles } @{ $self->get_roles });
45 sub get_required_method_list{
46 return @{ $_[0]->{required_methods} };
49 sub add_required_methods {
50 my($self, @methods) = @_;
51 my %required = map{ $_ => 1 } @{$self->{required_methods}};
52 push @{$self->{required_methods}}, grep{ !$required{$_}++ && !$self->has_method($_) } @methods;
57 my($self, $name) = @_;
58 return scalar( grep{ $_ eq $name } @{ $self->{required_methods} } ) != 0;
65 $self->{attributes}->{$name} = (@_ == 1) ? $_[0] : { @_ };
69 sub _check_required_methods{
70 my($role, $applicant, $args) = @_;
72 if($args->{_to} eq 'role'){
73 $applicant->add_required_methods($role->get_required_method_list);
75 else{ # to class or instance
76 my $applicant_class_name = $applicant->name;
79 foreach my $method_name(@{$role->{required_methods}}){
80 next if exists $args->{aliased_methods}{$method_name};
81 next if exists $role->{methods}{$method_name};
82 next if $applicant_class_name->can($method_name);
84 push @missing, $method_name;
87 $role->throw_error(sprintf "'%s' requires the method%s %s to be implemented by '%s'",
89 (@missing == 1 ? '' : 's'), # method or methods
90 english_list(map{ sprintf q{'%s'}, $_ } @missing),
91 $applicant_class_name);
99 my($role, $applicant, $args) = @_;
101 my $alias = $args->{-alias};
102 my $excludes = $args->{-excludes};
104 foreach my $method_name($role->get_method_list){
105 next if $method_name eq 'meta';
107 my $code = $role->get_method_body($method_name);
109 if(!exists $excludes->{$method_name}){
110 if(!$applicant->has_method($method_name)){
111 # The third argument $role is used in Role::Composite
112 $applicant->add_method($method_name => $code, $role);
116 if(exists $alias->{$method_name}){
117 my $dstname = $alias->{$method_name};
119 my $dstcode = $applicant->get_method_body($dstname);
121 if(defined($dstcode) && $dstcode != $code){
122 $role->throw_error("Cannot create a method alias if a local method of the same name exists");
125 $applicant->add_method($dstname => $code, $role);
133 sub _apply_attributes{
134 my($role, $applicant, $args) = @_;
136 for my $attr_name ($role->get_attribute_list) {
137 next if $applicant->has_attribute($attr_name);
139 $applicant->add_attribute($attr_name => $role->get_attribute($attr_name));
144 sub _apply_modifiers{
145 my($role, $applicant, $args) = @_;
147 if(my $modifiers = $role->{override_method_modifiers}){
148 foreach my $method_name (keys %{$modifiers}){
149 $applicant->add_override_method_modifier($method_name => $modifiers->{$method_name});
153 for my $modifier_type (qw/before around after/) {
154 my $modifiers = $role->{"${modifier_type}_method_modifiers"}
157 my $add_modifier = "add_${modifier_type}_method_modifier";
159 foreach my $method_name (keys %{$modifiers}){
160 foreach my $code(@{ $modifiers->{$method_name} }){
161 next if $applicant->{"_applied_$modifier_type"}{$method_name, $code}++; # skip applied modifiers
162 $applicant->$add_modifier($method_name => $code);
170 my($role, $applicant, $args) = @_;
172 my $roles = ($args->{_to} eq 'role') ? $applicant->get_roles : $applicant->roles;
174 foreach my $r($role, @{$role->get_roles}){
175 if(!$applicant->does_role($r->name)){
182 # Moose uses Application::ToInstance, Application::ToClass, Application::ToRole
185 my $applicant = shift;
187 my %args = (@_ == 1) ? %{ $_[0] } : @_;
191 if($applicant->isa('Mouse::Meta::Class')){ # Application::ToClass
192 $args{_to} = 'class';
194 elsif($applicant->isa('Mouse::Meta::Role')){ # Application::ToRole
197 else{ # Appplication::ToInstance
198 $args{_to} = 'instance';
199 $instance = $applicant;
201 $applicant = (Mouse::Util::class_of($instance) || 'Mouse::Meta::Class')->create_anon_class(
202 superclasses => [ref $instance],
207 if($args{alias} && !exists $args{-alias}){
208 $args{-alias} = $args{alias};
210 if($args{excludes} && !exists $args{-excludes}){
211 $args{-excludes} = $args{excludes};
214 $args{aliased_methods} = {};
215 if(my $alias = $args{-alias}){
216 @{$args{aliased_methods}}{ values %{$alias} } = ();
219 if(my $excludes = $args{-excludes}){
220 $args{-excludes} = {}; # replace with a hash ref
222 %{$args{-excludes}} = (map{ $_ => undef } @{$excludes});
225 $args{-excludes}{$excludes} = undef;
229 $self->_check_required_methods($applicant, \%args);
230 $self->_apply_attributes($applicant, \%args);
231 $self->_apply_methods($applicant, \%args);
232 $self->_apply_modifiers($applicant, \%args);
233 $self->_append_roles($applicant, \%args);
236 if(defined $instance){ # Application::ToInstance
238 bless $instance, $applicant->name;
239 $applicant->_initialize_object($instance, $instance);
247 my($role_class, @role_specs) = @_;
249 require 'Mouse/Meta/Role/Composite.pm'; # we don't want to create its namespace
251 my $composite = Mouse::Meta::Role::Composite->create_anon_role();
253 foreach my $role_spec (@role_specs) {
254 my($role_name, $args) = @{$role_spec};
255 $role_name->meta->apply($composite, %{$args});
260 for my $modifier_type (qw/before after around/) {
262 my $modifier = "${modifier_type}_method_modifiers";
264 my $add_method_modifier = sub {
265 my ($self, $method_name, $method) = @_;
267 push @{ $self->{$modifier}->{$method_name} ||= [] }, $method;
271 my $get_method_modifiers = sub {
272 my ($self, $method_name) = @_;
273 return @{ $self->{$modifier}->{$method_name} ||= [] }
277 *{ 'add_' . $modifier_type . '_method_modifier' } = $add_method_modifier;
278 *{ 'get_' . $modifier_type . '_method_modifiers' } = $get_method_modifiers;
280 # has_${modifier_type}_method_modifiers is moved into t::lib::Test::Mouse
283 sub add_override_method_modifier{
284 my($self, $method_name, $method) = @_;
286 if($self->has_method($method_name)){
287 # This error happens in the override keyword or during role composition,
288 # so I added a message, "A local method of ...", only for compatibility (gfx)
289 $self->throw_error("Cannot add an override of method '$method_name' "
290 . "because there is a local version of '$method_name'"
291 . "(A local method of the same name as been found)");
294 $self->{override_method_modifiers}->{$method_name} = $method;
297 sub get_override_method_modifier {
298 my ($self, $method_name) = @_;
299 return $self->{override_method_modifiers}->{$method_name};
303 my ($self, $role_name) = @_;
306 || $self->throw_error("You must supply a role name to look for");
308 # if we are it,.. then return true
309 return 1 if $role_name eq $self->name;
310 # otherwise.. check our children
311 for my $role (@{ $self->get_roles }) {
312 return 1 if $role->does_role($role_name);
322 Mouse::Meta::Role - The Mouse Role metaclass
326 This document describes Mouse version 0.39