Reorder changes to put most interesting ones first
[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
6d0815b5 9our $VERSION = '0.93';
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
0385d05e 28__PACKAGE__->meta->add_attribute('_methods' => (
29 reader => '_method_map',
fb1e11d5 30 default => sub { {} }
31));
32
7071e2cb 33__PACKAGE__->meta->add_attribute(
34 'application_role_summation_class',
35 reader => 'application_role_summation_class',
36 default => 'Moose::Meta::Role::Application::RoleSummation',
37);
38
fb1e11d5 39sub new {
40 my ($class, %params) = @_;
12d8c847 41
fb1e11d5 42 # the roles param is required ...
70ea9161 43 foreach ( @{$params{roles}} ) {
44 unless ( $_->isa('Moose::Meta::Role') ) {
45 require Moose;
46 Moose->throw_error("The list of roles must be instances of Moose::Meta::Role, not $_");
47 }
48 }
12d8c847 49
50 my @composition_roles = map {
4701ceff 51 $_->composition_class_roles
12d8c847 52 } @{ $params{roles} };
53
54 if (@composition_roles) {
55 my $meta = Moose::Meta::Class->create_anon_class(
56 superclasses => [ $class ],
57 roles => [ @composition_roles ],
58 cache => 1,
59 );
60 $meta->add_method(meta => sub { $meta });
61 $class = $meta->name;
62 }
63
fb1e11d5 64 # and the name is created from the
65 # roles if one has not been provided
66 $params{name} ||= (join "|" => map { $_->name } @{$params{roles}});
e606ae5f 67 $class->_new(\%params);
fb1e11d5 68}
69
87e63626 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
73# add the symbol.
74sub add_method {
fb1e11d5 75 my ($self, $method_name, $method) = @_;
70ea9161 76
77 unless ( defined $method_name && $method_name ) {
78 Moose->throw_error("You must define a method name");
79 }
87e63626 80
81 my $body;
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,
d03bd989 87 name => $method_name
87e63626 88 ) if $method->can('clone');
89 }
90 }
91 else {
92 $body = $method;
93 $method = $self->wrap_method_body( body => $body, name => $method_name );
94 }
fb1e11d5 95
0385d05e 96 $self->_method_map->{$method_name} = $method;
97}
98
99sub get_method_list {
100 my $self = shift;
101 return keys %{ $self->_method_map };
102}
103
104sub has_method {
105 my ($self, $method_name) = @_;
106
107 return exists $self->_method_map->{$method_name};
108}
109
110sub get_method {
111 my ($self, $method_name) = @_;
112
113 return $self->_method_map->{$method_name};
fb1e11d5 114}
115
7071e2cb 116sub apply_params {
117 my ($self, $role_params) = @_;
118 Class::MOP::load_class($self->application_role_summation_class);
119
120 $self->application_role_summation_class->new(
121 role_params => $role_params,
122 )->apply($self);
123
124 return $self;
125}
126
6e04cb0e 127sub reinitialize {
128 my ($class, $old_meta, @args) = @_;
129 Moose->throw_error('Moose::Meta::Role::Composite instances can only be reinitialized from an existing metaclass instance')
130 if !blessed $old_meta || !$old_meta->isa('Moose::Meta::Role::Composite');
131 return $old_meta->meta->clone_object($old_meta, @args);
132}
133
fb1e11d5 1341;
135
136__END__
137
138=pod
139
140=head1 NAME
141
142Moose::Meta::Role::Composite - An object to represent the set of roles
143
144=head1 DESCRIPTION
145
da5cc486 146A composite is a role that consists of a set of two or more roles.
147
148The API of a composite role is almost identical to that of a regular
149role.
150
151=head1 INHERITANCE
152
153C<Moose::Meta::Role::Composite> is a subclass of L<Moose::Meta::Role>.
154
fb1e11d5 155=head2 METHODS
156
157=over 4
158
da5cc486 159=item B<< Moose::Meta::Role::Composite->new(%options) >>
fb1e11d5 160
da5cc486 161This returns a new composite role object. It accepts the same
162options as its parent class, with a few changes:
fb1e11d5 163
da5cc486 164=over 8
fb1e11d5 165
da5cc486 166=item * roles
fb1e11d5 167
da5cc486 168This option is an array reference containing a list of
169L<Moose::Meta::Role> object. This is a required option.
170
171=item * name
172
173If a name is not given, one is generated from the roles provided.
174
7071e2cb 175=item * apply_params(\%role_params)
176
177Creates a new RoleSummation role application with C<%role_params> and applies
178the composite role to it. The RoleSummation role application class used is
179determined by the composite role's C<application_role_summation_class>
180attribute.
181
6e04cb0e 182=item * reinitialize($metaclass)
183
184Like C<< Class::MOP::Package->reinitialize >>, but doesn't allow passing a
185string with the package name, as there is no real package for composite roles.
186
da5cc486 187=back
fb1e11d5 188
189=back
190
191=head1 BUGS
192
193All complex software has bugs lurking in it, and this module is no
194exception. If you find a bug please either email me, or add the bug
195to cpan-RT.
196
197=head1 AUTHOR
198
199Stevan Little E<lt>stevan@iinteractive.comE<gt>
200
201=head1 COPYRIGHT AND LICENSE
202
2840a3b2 203Copyright 2006-2009 by Infinity Interactive, Inc.
fb1e11d5 204
205L<http://www.iinteractive.com>
206
207This library is free software; you can redistribute it and/or modify
208it under the same terms as Perl itself.
209
e606ae5f 210=cut