1 package Moose::Meta::Role::Composite;
7 use Scalar::Util 'blessed';
9 use base 'Moose::Meta::Role';
12 # we need to override the ->name
13 # method from Class::MOP::Package
14 # since we don't have an actual
17 __PACKAGE__->meta->add_attribute('name' => (
19 Class::MOP::_definition_context(),
23 # Again, since we don't have a real
24 # package to store our methods in,
25 # we use a HASH ref instead.
27 __PACKAGE__->meta->add_attribute('_methods' => (
28 reader => '_method_map',
29 default => sub { {} },
30 Class::MOP::_definition_context(),
33 __PACKAGE__->meta->add_attribute(
34 'application_role_summation_class',
35 reader => 'application_role_summation_class',
36 default => 'Moose::Meta::Role::Application::RoleSummation',
37 Class::MOP::_definition_context(),
41 my ($class, %params) = @_;
43 # the roles param is required ...
44 foreach ( @{$params{roles}} ) {
45 unless ( $_->isa('Moose::Meta::Role') ) {
47 Moose->throw_error("The list of roles must be instances of Moose::Meta::Role, not $_");
51 my @composition_roles = map {
52 $_->composition_class_roles
53 } @{ $params{roles} };
55 if (@composition_roles) {
56 my $meta = Moose::Meta::Class->create_anon_class(
57 superclasses => [ $class ],
58 roles => [ @composition_roles ],
64 # and the name is created from the
65 # roles if one has not been provided
66 $params{name} ||= (join "|" => map { $_->name } @{$params{roles}});
67 $class->_new(\%params);
70 # This is largely a cope of what's in Moose::Meta::Role (itself
71 # largely a copy of Class::MOP::Class). However, we can't actually
72 # call add_package_symbol, because there's no package to which which
75 my ($self, $method_name, $method) = @_;
77 unless ( defined $method_name && $method_name ) {
78 Moose->throw_error("You must define a method name");
82 if (blessed($method)) {
83 $body = $method->body;
84 if ($method->package_name ne $self->name) {
85 $method = $method->clone(
86 package_name => $self->name,
88 ) if $method->can('clone');
93 $method = $self->wrap_method_body( body => $body, name => $method_name );
96 $self->_method_map->{$method_name} = $method;
101 return keys %{ $self->_method_map };
104 sub _get_local_methods {
106 return values %{ $self->_method_map };
110 my ($self, $method_name) = @_;
112 return exists $self->_method_map->{$method_name};
116 my ($self, $method_name) = @_;
118 return $self->_method_map->{$method_name};
122 my ($self, $role_params) = @_;
123 Class::MOP::load_class($self->application_role_summation_class);
125 $self->application_role_summation_class->new(
126 role_params => $role_params,
133 my ( $class, $old_meta, @args ) = @_;
136 'Moose::Meta::Role::Composite instances can only be reinitialized from an existing metaclass instance'
138 if !blessed $old_meta
139 || !$old_meta->isa('Moose::Meta::Role::Composite');
141 my %existing_classes = map { $_ => $old_meta->$_() } qw(
142 application_role_summation_class
145 return $old_meta->meta->clone_object( $old_meta, %existing_classes, @args );
150 # ABSTRACT: An object to represent the set of roles
158 A composite is a role that consists of a set of two or more roles.
160 The API of a composite role is almost identical to that of a regular
165 C<Moose::Meta::Role::Composite> is a subclass of L<Moose::Meta::Role>.
171 =item B<< Moose::Meta::Role::Composite->new(%options) >>
173 This returns a new composite role object. It accepts the same
174 options as its parent class, with a few changes:
180 This option is an array reference containing a list of
181 L<Moose::Meta::Role> object. This is a required option.
185 If a name is not given, one is generated from the roles provided.
187 =item * apply_params(\%role_params)
189 Creates a new RoleSummation role application with C<%role_params> and applies
190 the composite role to it. The RoleSummation role application class used is
191 determined by the composite role's C<application_role_summation_class>
194 =item * reinitialize($metaclass)
196 Like C<< Class::MOP::Package->reinitialize >>, but doesn't allow passing a
197 string with the package name, as there is no real package for composite roles.
205 See L<Moose/BUGS> for details on reporting bugs.