1 package Moose::Meta::Role::Composite;
7 use Scalar::Util 'blessed';
9 our $VERSION = '0.89_02';
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Moose::Meta::Role';
16 # we need to override the ->name
17 # method from Class::MOP::Package
18 # since we don't have an actual
21 __PACKAGE__->meta->add_attribute('name' => (reader => 'name'));
24 # Again, since we don't have a real
25 # package to store our methods in,
26 # we use a HASH ref instead.
28 __PACKAGE__->meta->add_attribute('methods' => (
29 reader => 'get_method_map',
33 __PACKAGE__->meta->add_attribute(
34 'application_role_summation_class',
35 reader => 'application_role_summation_class',
36 default => 'Moose::Meta::Role::Application::RoleSummation',
40 my ($class, %params) = @_;
41 # the roles param is required ...
42 foreach ( @{$params{roles}} ) {
43 unless ( $_->isa('Moose::Meta::Role') ) {
45 Moose->throw_error("The list of roles must be instances of Moose::Meta::Role, not $_");
48 # and the name is created from the
49 # roles if one has not been provided
50 $params{name} ||= (join "|" => map { $_->name } @{$params{roles}});
51 $class->_new(\%params);
54 # This is largely a cope of what's in Moose::Meta::Role (itself
55 # largely a copy of Class::MOP::Class). However, we can't actually
56 # call add_package_symbol, because there's no package to which which
59 my ($self, $method_name, $method) = @_;
61 unless ( defined $method_name && $method_name ) {
62 Moose->throw_error("You must define a method name");
66 if (blessed($method)) {
67 $body = $method->body;
68 if ($method->package_name ne $self->name) {
69 $method = $method->clone(
70 package_name => $self->name,
72 ) if $method->can('clone');
77 $method = $self->wrap_method_body( body => $body, name => $method_name );
80 $self->get_method_map->{$method_name} = $method;
84 my ($self, $role_params) = @_;
85 Class::MOP::load_class($self->application_role_summation_class);
87 $self->application_role_summation_class->new(
88 role_params => $role_params,
102 Moose::Meta::Role::Composite - An object to represent the set of roles
106 A composite is a role that consists of a set of two or more roles.
108 The API of a composite role is almost identical to that of a regular
113 C<Moose::Meta::Role::Composite> is a subclass of L<Moose::Meta::Role>.
119 =item B<< Moose::Meta::Role::Composite->new(%options) >>
121 This returns a new composite role object. It accepts the same
122 options as its parent class, with a few changes:
128 This option is an array reference containing a list of
129 L<Moose::Meta::Role> object. This is a required option.
133 If a name is not given, one is generated from the roles provided.
135 =item * apply_params(\%role_params)
137 Creates a new RoleSummation role application with C<%role_params> and applies
138 the composite role to it. The RoleSummation role application class used is
139 determined by the composite role's C<application_role_summation_class>
148 All complex software has bugs lurking in it, and this module is no
149 exception. If you find a bug please either email me, or add the bug
154 Stevan Little E<lt>stevan@iinteractive.comE<gt>
156 =head1 COPYRIGHT AND LICENSE
158 Copyright 2006-2009 by Infinity Interactive, Inc.
160 L<http://www.iinteractive.com>
162 This library is free software; you can redistribute it and/or modify
163 it under the same terms as Perl itself.