Use dzil Authority plugin - remove $AUTHORITY from code
[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 use base 'Moose::Meta::Role';
10
11 # NOTE:
12 # we need to override the ->name
13 # method from Class::MOP::Package
14 # since we don't have an actual
15 # package for this.
16 # - SL
17 __PACKAGE__->meta->add_attribute('name' => (reader => 'name'));
18
19 # NOTE:
20 # Again, since we don't have a real
21 # package to store our methods in,
22 # we use a HASH ref instead.
23 # - SL
24 __PACKAGE__->meta->add_attribute('_methods' => (
25     reader  => '_method_map',
26     default => sub { {} }
27 ));
28
29 __PACKAGE__->meta->add_attribute(
30     'application_role_summation_class',
31     reader  => 'application_role_summation_class',
32     default => 'Moose::Meta::Role::Application::RoleSummation',
33 );
34
35 sub new {
36     my ($class, %params) = @_;
37
38     # the roles param is required ...
39     foreach ( @{$params{roles}} ) {
40         unless ( $_->isa('Moose::Meta::Role') ) {
41             require Moose;
42             Moose->throw_error("The list of roles must be instances of Moose::Meta::Role, not $_");
43         }
44     }
45
46     my @composition_roles = map {
47         $_->composition_class_roles
48     } @{ $params{roles} };
49
50     if (@composition_roles) {
51         my $meta = Moose::Meta::Class->create_anon_class(
52             superclasses => [ $class ],
53             roles        => [ @composition_roles ],
54             cache        => 1,
55         );
56         $class = $meta->name;
57     }
58
59     # and the name is created from the
60     # roles if one has not been provided
61     $params{name} ||= (join "|" => map { $_->name } @{$params{roles}});
62     $class->_new(\%params);
63 }
64
65 # This is largely a cope of what's in Moose::Meta::Role (itself
66 # largely a copy of Class::MOP::Class). However, we can't actually
67 # call add_package_symbol, because there's no package to which which
68 # add the symbol.
69 sub add_method {
70     my ($self, $method_name, $method) = @_;
71
72     unless ( defined $method_name && $method_name ) {
73         Moose->throw_error("You must define a method name");
74     }
75
76     my $body;
77     if (blessed($method)) {
78         $body = $method->body;
79         if ($method->package_name ne $self->name) {
80             $method = $method->clone(
81                 package_name => $self->name,
82                 name         => $method_name
83             ) if $method->can('clone');
84         }
85     }
86     else {
87         $body = $method;
88         $method = $self->wrap_method_body( body => $body, name => $method_name );
89     }
90
91     $self->_method_map->{$method_name} = $method;
92 }
93
94 sub get_method_list {
95     my $self = shift;
96     return keys %{ $self->_method_map };
97 }
98
99 sub _get_local_methods {
100     my $self = shift;
101     return values %{ $self->_method_map };
102 }
103
104 sub has_method {
105     my ($self, $method_name) = @_;
106
107     return exists $self->_method_map->{$method_name};
108 }
109
110 sub get_method {
111     my ($self, $method_name) = @_;
112
113     return $self->_method_map->{$method_name};
114 }
115
116 sub 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
127 sub reinitialize {
128     my ( $class, $old_meta, @args ) = @_;
129
130     Moose->throw_error(
131         'Moose::Meta::Role::Composite instances can only be reinitialized from an existing metaclass instance'
132         )
133         if !blessed $old_meta
134             || !$old_meta->isa('Moose::Meta::Role::Composite');
135
136     my %existing_classes = map { $_ => $old_meta->$_() } qw(
137         application_role_summation_class
138     );
139
140     return $old_meta->meta->clone_object( $old_meta, %existing_classes, @args );
141 }
142
143 1;
144
145 # ABSTRACT: An object to represent the set of roles
146
147 __END__
148
149 =pod
150
151 =head1 DESCRIPTION
152
153 A composite is a role that consists of a set of two or more roles.
154
155 The API of a composite role is almost identical to that of a regular
156 role.
157
158 =head1 INHERITANCE
159
160 C<Moose::Meta::Role::Composite> is a subclass of L<Moose::Meta::Role>.
161
162 =head2 METHODS
163
164 =over 4
165
166 =item B<< Moose::Meta::Role::Composite->new(%options) >>
167
168 This returns a new composite role object. It accepts the same
169 options as its parent class, with a few changes:
170
171 =over 8
172
173 =item * roles
174
175 This option is an array reference containing a list of
176 L<Moose::Meta::Role> object. This is a required option.
177
178 =item * name
179
180 If a name is not given, one is generated from the roles provided.
181
182 =item * apply_params(\%role_params)
183
184 Creates a new RoleSummation role application with C<%role_params> and applies
185 the composite role to it. The RoleSummation role application class used is
186 determined by the composite role's C<application_role_summation_class>
187 attribute.
188
189 =item * reinitialize($metaclass)
190
191 Like C<< Class::MOP::Package->reinitialize >>, but doesn't allow passing a
192 string with the package name, as there is no real package for composite roles.
193
194 =back
195
196 =back
197
198 =head1 BUGS
199
200 See L<Moose/BUGS> for details on reporting bugs.
201
202 =cut