1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
package Moose::Meta::Role::Composite;
our $VERSION = '2.4000';
use strict;
use warnings;
use metaclass;
use Scalar::Util 'blessed';
use Moose::Util 'throw_exception';
use parent 'Moose::Meta::Role';
# NOTE:
# we need to override the ->name
# method from Class::MOP::Package
# since we don't have an actual
# package for this.
# - SL
__PACKAGE__->meta->add_attribute('name' => (
reader => 'name',
Class::MOP::_definition_context(),
));
# NOTE:
# Again, since we don't have a real
# package to store our methods in,
# we use a HASH ref instead.
# - SL
__PACKAGE__->meta->add_attribute('_methods' => (
reader => '_method_map',
default => sub { {} },
Class::MOP::_definition_context(),
));
__PACKAGE__->meta->add_attribute('_overloads' => (
reader => '_overload_map',
default => sub { {} },
Class::MOP::_definition_context(),
));
__PACKAGE__->meta->add_attribute('_overload_fallback' => (
accessor => '_overload_fallback',
Class::MOP::_definition_context(),
));
__PACKAGE__->meta->add_attribute(
'application_role_summation_class',
reader => 'application_role_summation_class',
default => 'Moose::Meta::Role::Application::RoleSummation',
Class::MOP::_definition_context(),
);
sub new {
my ($class, %params) = @_;
# the roles param is required ...
foreach ( @{$params{roles}} ) {
unless ( $_->isa('Moose::Meta::Role') ) {
throw_exception( RolesListMustBeInstancesOfMooseMetaRole => params => \%params,
role => $_,
class => $class
);
}
}
my @composition_roles = map {
$_->composition_class_roles
} @{ $params{roles} };
if (@composition_roles) {
my $meta = Moose::Meta::Class->create_anon_class(
superclasses => [ $class ],
roles => [ @composition_roles ],
cache => 1,
);
$class = $meta->name;
}
# and the name is created from the
# roles if one has not been provided
$params{name} ||= (join "|" => map { $_->name } @{$params{roles}});
$class->_new(\%params);
}
# There's no such thing as an anonymous composite role since composites are an
# artifact of Moose's internals. However, a composite role that contains an
# anon role may _look_ like an anon role since $self->name =~ /$anon_key/ can
# return true if the first role in the composite is anonymous itself.
sub is_anon { 0 }
# This is largely a copy of what's in Moose::Meta::Role (itself
# largely a copy of Class::MOP::Class). However, we can't actually
# call add_package_symbol, because there's no package into which to
# add the symbol.
sub add_method {
my ($self, $method_name, $method) = @_;
unless ( defined $method_name && $method_name ) {
throw_exception( MustDefineAMethodName => instance => $self );
}
my $body;
if (blessed($method)) {
$body = $method->body;
if ($method->package_name ne $self->name) {
$method = $method->clone(
package_name => $self->name,
name => $method_name
) if $method->can('clone');
}
}
else {
$body = $method;
$method = $self->wrap_method_body( body => $body, name => $method_name );
}
$self->_method_map->{$method_name} = $method;
}
sub get_method_list {
my $self = shift;
return keys %{ $self->_method_map };
}
sub _get_local_methods {
my $self = shift;
return values %{ $self->_method_map };
}
sub has_method {
my ($self, $method_name) = @_;
return exists $self->_method_map->{$method_name};
}
sub get_method {
my ($self, $method_name) = @_;
return $self->_method_map->{$method_name};
}
sub is_overloaded {
my ($self) = @_;
return keys %{ $self->_overload_map };
}
sub add_overloaded_operator {
my ( $self, $op_name, $overload ) = @_;
unless ( defined $op_name && $op_name ) {
throw_exception(
'MustDefineAnOverloadOperator',
instance => $self,
);
}
$self->_overload_map->{$op_name} = $overload;
}
sub get_overload_fallback_value {
my ($self) = @_;
return $self->_overload_fallback;
}
sub set_overload_fallback_value {
my $self = shift;
$self->_overload_fallback(shift);
}
sub get_all_overloaded_operators {
my ( $self, $method_name ) = @_;
return values %{ $self->_overload_map };
}
sub apply_params {
my ($self, $role_params) = @_;
Moose::Util::_load_user_class($self->application_role_summation_class);
$self->application_role_summation_class->new(
role_params => $role_params,
)->apply($self);
return $self;
}
sub reinitialize {
my ( $class, $old_meta, @args ) = @_;
throw_exception( CannotInitializeMooseMetaRoleComposite => old_meta => $old_meta,
args => \@args,
role_composite => $class
)
if !blessed $old_meta
|| !$old_meta->isa('Moose::Meta::Role::Composite');
my %existing_classes = map { $_ => $old_meta->$_() } qw(
application_role_summation_class
);
return $old_meta->meta->clone_object( $old_meta, %existing_classes, @args );
}
1;
# ABSTRACT: An object to represent the set of roles
__END__
=pod
=encoding UTF-8
=head1 NAME
Moose::Meta::Role::Composite - An object to represent the set of roles
=head1 VERSION
version 2.4000
=head1 DESCRIPTION
A composite is a role that consists of a set of two or more roles.
The API of a composite role is almost identical to that of a regular
role.
=head1 INHERITANCE
C<Moose::Meta::Role::Composite> is a subclass of L<Moose::Meta::Role>.
=head1 METHODS
=head2 Moose::Meta::Role::Composite->new(%options)
This returns a new composite role object. It accepts the same
options as its parent class, with a few changes:
=over 4
=item * roles
This option is an array reference containing a list of
L<Moose::Meta::Role> object. This is a required option.
=item * name
If a name is not given, one is generated from the roles provided.
=item * apply_params(\%role_params)
Creates a new RoleSummation role application with C<%role_params> and applies
the composite role to it. The RoleSummation role application class used is
determined by the composite role's C<application_role_summation_class>
attribute.
=item * reinitialize($metaclass)
Like C<< Class::MOP::Package->reinitialize >>, but doesn't allow passing a
string with the package name, as there is no real package for composite roles.
=back
=head1 BUGS
See L<Moose/BUGS> for details on reporting bugs.
=head1 AUTHORS
=over 4
=item *
Stevan Little <stevan@cpan.org>
=item *
Dave Rolsky <autarch@urth.org>
=item *
Jesse Luehrs <doy@cpan.org>
=item *
Shawn M Moore <sartak@cpan.org>
=item *
יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
=item *
Karen Etheridge <ether@cpan.org>
=item *
Florian Ragwitz <rafl@debian.org>
=item *
Hans Dieter Pearcey <hdp@cpan.org>
=item *
Chris Prather <chris@prather.org>
=item *
Matt S Trout <mstrout@cpan.org>
=back
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2006 by Infinity Interactive, Inc.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|