remove trailing whitespace
[gitmo/Moose.git] / lib / Moose / Meta / Role / Composite.pm
CommitLineData
fb1e11d5 1package Moose::Meta::Role::Composite;
2
3use strict;
4use warnings;
5use metaclass;
6
21f1e231 7use Scalar::Util 'blessed';
fb1e11d5 8
74397c13 9our $VERSION = '0.75_01';
e606ae5f 10$VERSION = eval $VERSION;
fb1e11d5 11our $AUTHORITY = 'cpan:STEVAN';
12
13use base 'Moose::Meta::Role';
14
15# NOTE:
d03bd989 16# we need to override the ->name
fb1e11d5 17# method from Class::MOP::Package
d03bd989 18# since we don't have an actual
fb1e11d5 19# package for this.
20# - SL
21__PACKAGE__->meta->add_attribute('name' => (reader => 'name'));
22
23# NOTE:
d03bd989 24# Again, since we don't have a real
25# package to store our methods in,
26# we use a HASH ref instead.
fb1e11d5 27# - SL
28__PACKAGE__->meta->add_attribute('methods' => (
29 reader => 'get_method_map',
30 default => sub { {} }
31));
32
33sub new {
34 my ($class, %params) = @_;
35 # the roles param is required ...
70ea9161 36 foreach ( @{$params{roles}} ) {
37 unless ( $_->isa('Moose::Meta::Role') ) {
38 require Moose;
39 Moose->throw_error("The list of roles must be instances of Moose::Meta::Role, not $_");
40 }
41 }
fb1e11d5 42 # and the name is created from the
43 # roles if one has not been provided
44 $params{name} ||= (join "|" => map { $_->name } @{$params{roles}});
e606ae5f 45 $class->_new(\%params);
fb1e11d5 46}
47
87e63626 48# This is largely a cope of what's in Moose::Meta::Role (itself
49# largely a copy of Class::MOP::Class). However, we can't actually
50# call add_package_symbol, because there's no package to which which
51# add the symbol.
52sub add_method {
fb1e11d5 53 my ($self, $method_name, $method) = @_;
70ea9161 54
55 unless ( defined $method_name && $method_name ) {
56 Moose->throw_error("You must define a method name");
57 }
87e63626 58
59 my $body;
60 if (blessed($method)) {
61 $body = $method->body;
62 if ($method->package_name ne $self->name) {
63 $method = $method->clone(
64 package_name => $self->name,
d03bd989 65 name => $method_name
87e63626 66 ) if $method->can('clone');
67 }
68 }
69 else {
70 $body = $method;
71 $method = $self->wrap_method_body( body => $body, name => $method_name );
72 }
fb1e11d5 73
c4538447 74 $self->get_method_map->{$method_name} = $method;
fb1e11d5 75}
76
771;
78
79__END__
80
81=pod
82
83=head1 NAME
84
85Moose::Meta::Role::Composite - An object to represent the set of roles
86
87=head1 DESCRIPTION
88
da5cc486 89A composite is a role that consists of a set of two or more roles.
90
91The API of a composite role is almost identical to that of a regular
92role.
93
94=head1 INHERITANCE
95
96C<Moose::Meta::Role::Composite> is a subclass of L<Moose::Meta::Role>.
97
fb1e11d5 98=head2 METHODS
99
100=over 4
101
da5cc486 102=item B<< Moose::Meta::Role::Composite->new(%options) >>
fb1e11d5 103
da5cc486 104This returns a new composite role object. It accepts the same
105options as its parent class, with a few changes:
fb1e11d5 106
da5cc486 107=over 8
fb1e11d5 108
da5cc486 109=item * roles
fb1e11d5 110
da5cc486 111This option is an array reference containing a list of
112L<Moose::Meta::Role> object. This is a required option.
113
114=item * name
115
116If a name is not given, one is generated from the roles provided.
117
118=back
fb1e11d5 119
120=back
121
122=head1 BUGS
123
124All complex software has bugs lurking in it, and this module is no
125exception. If you find a bug please either email me, or add the bug
126to cpan-RT.
127
128=head1 AUTHOR
129
130Stevan Little E<lt>stevan@iinteractive.comE<gt>
131
132=head1 COPYRIGHT AND LICENSE
133
2840a3b2 134Copyright 2006-2009 by Infinity Interactive, Inc.
fb1e11d5 135
136L<http://www.iinteractive.com>
137
138This library is free software; you can redistribute it and/or modify
139it under the same terms as Perl itself.
140
e606ae5f 141=cut