c7329d47326c313de43f55e30a94cdf66566a154
[gitmo/Moose.git] / lib / Moose / Meta / Role / Composite.pm
1 package Moose::Meta::Role::Composite;
2
3 use strict;
4 use warnings;
5 use metaclass;
6
7 use Scalar::Util 'blessed';
8
9 our $VERSION   = '0.89_02';
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 use base 'Moose::Meta::Role';
14
15 # NOTE:
16 # we need to override the ->name
17 # method from Class::MOP::Package
18 # since we don't have an actual
19 # package for this.
20 # - SL
21 __PACKAGE__->meta->add_attribute('name' => (reader => 'name'));
22
23 # NOTE:
24 # Again, since we don't have a real
25 # package to store our methods in,
26 # we use a HASH ref instead.
27 # - SL
28 __PACKAGE__->meta->add_attribute('methods' => (
29     reader  => 'get_method_map',
30     default => sub { {} }
31 ));
32
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
39 sub new {
40     my ($class, %params) = @_;
41     # the roles param is required ...
42     foreach ( @{$params{roles}} ) {
43         unless ( $_->isa('Moose::Meta::Role') ) {
44             require Moose;
45             Moose->throw_error("The list of roles must be instances of Moose::Meta::Role, not $_");
46         }
47     }
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);
52 }
53
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
57 # add the symbol.
58 sub add_method {
59     my ($self, $method_name, $method) = @_;
60
61     unless ( defined $method_name && $method_name ) {
62         Moose->throw_error("You must define a method name");
63     }
64
65     my $body;
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,
71                 name         => $method_name
72             ) if $method->can('clone');
73         }
74     }
75     else {
76         $body = $method;
77         $method = $self->wrap_method_body( body => $body, name => $method_name );
78     }
79
80     $self->get_method_map->{$method_name} = $method;
81 }
82
83 sub apply_params {
84     my ($self, $role_params) = @_;
85     Class::MOP::load_class($self->application_role_summation_class);
86
87     $self->application_role_summation_class->new(
88         role_params => $role_params,
89     )->apply($self);
90
91     return $self;
92 }
93
94 1;
95
96 __END__
97
98 =pod
99
100 =head1 NAME
101
102 Moose::Meta::Role::Composite - An object to represent the set of roles
103
104 =head1 DESCRIPTION
105
106 A composite is a role that consists of a set of two or more roles.
107
108 The API of a composite role is almost identical to that of a regular
109 role.
110
111 =head1 INHERITANCE
112
113 C<Moose::Meta::Role::Composite> is a subclass of L<Moose::Meta::Role>.
114
115 =head2 METHODS
116
117 =over 4
118
119 =item B<< Moose::Meta::Role::Composite->new(%options) >>
120
121 This returns a new composite role object. It accepts the same
122 options as its parent class, with a few changes:
123
124 =over 8
125
126 =item * roles
127
128 This option is an array reference containing a list of
129 L<Moose::Meta::Role> object. This is a required option.
130
131 =item * name
132
133 If a name is not given, one is generated from the roles provided.
134
135 =item * apply_params(\%role_params)
136
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>
140 attribute.
141
142 =back
143
144 =back
145
146 =head1 BUGS
147
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
150 to cpan-RT.
151
152 =head1 AUTHOR
153
154 Stevan Little E<lt>stevan@iinteractive.comE<gt>
155
156 =head1 COPYRIGHT AND LICENSE
157
158 Copyright 2006-2009 by Infinity Interactive, Inc.
159
160 L<http://www.iinteractive.com>
161
162 This library is free software; you can redistribute it and/or modify
163 it under the same terms as Perl itself.
164
165 =cut