1 package Mouse::Meta::Role::Composite;
2 use Mouse::Util; # enables strict and warnings
4 use Mouse::Meta::Role::Application;
5 our @ISA = qw(Mouse::Meta::Role);
9 my $args = $class->Mouse::Object::BUILDARGS(@_);
10 my $roles = delete $args->{roles};
11 my $self = $class->create_anon_role(%{$args});
12 foreach my $role_spec(@{$roles}) {
13 my($role, $args) = ref($role_spec) eq 'ARRAY'
16 $role->apply($self, %{$args});
22 #my($self, $role_params) = @_;
29 return keys %{ $self->{methods} };
33 my($self, $method_name, $code, $role) = @_;
35 if( ($self->{methods}{$method_name} || 0) == $code){
36 # This role already has the same method.
40 if($method_name eq 'meta'){
41 $self->SUPER::add_method($method_name => $code);
44 # no need to add a subroutine to the stash
45 my $roles = $self->{composed_roles_by_method}{$method_name} ||= [];
46 push @{$roles}, $role;
48 $self->{conflicting_methods}{$method_name}++;
50 $self->{methods}{$method_name} = $code;
56 my($self, $method_name) = @_;
57 return $self->{methods}{$method_name};
61 # my($self, $method_name) = @_;
62 return 0; # to fool apply_methods() in combine()
66 # my($self, $method_name) = @_;
67 return 0; # to fool appply_attributes() in combine()
70 sub has_override_method_modifier {
71 # my($self, $method_name) = @_;
72 return 0; # to fool apply_modifiers() in combine()
77 my $attr_name = shift;
78 my $spec = (@_ == 1 ? $_[0] : {@_});
80 my $existing = $self->{attributes}{$attr_name};
81 if($existing && $existing != $spec){
82 $self->throw_error("We have encountered an attribute conflict with '$attr_name' "
83 . "during composition. This is fatal error and cannot be disambiguated.");
85 $self->SUPER::add_attribute($attr_name, $spec);
89 sub add_override_method_modifier {
90 my($self, $method_name, $code) = @_;
92 my $existing = $self->{override_method_modifiers}{$method_name};
93 if($existing && $existing != $code){
94 $self->throw_error( "We have encountered an 'override' method conflict with '$method_name' during "
95 . "composition (Two 'override' methods of the same name encountered). "
96 . "This is fatal error.")
98 $self->SUPER::add_override_method_modifier($method_name, $code);
104 my $consumer = shift;
106 Mouse::Meta::Role::Application::RoleSummation->new(@_)->apply($self, $consumer);
110 package Mouse::Meta::Role::Application::RoleSummation;
111 our @ISA = qw(Mouse::Meta::Role::Application);
114 my($self, $role, $consumer, @extra) = @_;
116 if(exists $role->{conflicting_methods}){
117 my $consumer_class_name = $consumer->name;
119 my @conflicting = grep{ !$consumer_class_name->can($_) }
120 keys %{ $role->{conflicting_methods} };
123 my $method_name_conflict = (@conflicting == 1
124 ? 'a method name conflict'
125 : 'method name conflicts');
128 my $roles = Mouse::Util::quoted_english_list(
129 grep{ !$seen{$_}++ } # uniq
132 @{ $role->{composed_roles_by_method} }{@conflicting}
135 $self->throw_error(sprintf
136 q{Due to %s in roles %s,}
137 . q{ the method%s %s must be implemented or excluded by '%s'},
138 $method_name_conflict,
140 (@conflicting > 1 ? 's' : ''),
141 Mouse::Util::quoted_english_list(@conflicting),
142 $consumer_class_name);
146 $self->SUPER::apply_methods($role, $consumer, @extra);
150 package Mouse::Meta::Role::Composite;
156 Mouse::Meta::Role::Composite - An object to represent the set of roles
160 This document describes Mouse version 0.71
164 L<Moose::Meta::Role::Composite>