X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FMeta%2FRole%2FComposite.pm;h=e3a6d570b09c7c410c1a5702ba170d53ff2fba7c;hb=462bdc732ebe32abf5b9f3cd40033540a5f1388f;hp=b7262fe17c7cdc46eb417a9a99df093e52a968c9;hpb=da5cc4866ad96cdc412bc495118fd38fc85a3f35;p=gitmo%2FMoose.git diff --git a/lib/Moose/Meta/Role/Composite.pm b/lib/Moose/Meta/Role/Composite.pm index b7262fe..e3a6d57 100644 --- a/lib/Moose/Meta/Role/Composite.pm +++ b/lib/Moose/Meta/Role/Composite.pm @@ -6,32 +6,35 @@ use metaclass; use Scalar::Util 'blessed'; -our $VERSION = '0.72'; -$VERSION = eval $VERSION; -our $AUTHORITY = 'cpan:STEVAN'; - use base 'Moose::Meta::Role'; # NOTE: -# we need to override the ->name +# we need to override the ->name # method from Class::MOP::Package -# since we don't have an actual +# since we don't have an actual # package for this. # - SL __PACKAGE__->meta->add_attribute('name' => (reader => 'name')); # NOTE: -# Again, since we don't have a real -# package to store our methods in, -# we use a HASH ref instead. +# 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 => 'get_method_map', +__PACKAGE__->meta->add_attribute('_methods' => ( + reader => '_method_map', default => sub { {} } )); +__PACKAGE__->meta->add_attribute( + 'application_role_summation_class', + reader => 'application_role_summation_class', + default => 'Moose::Meta::Role::Application::RoleSummation', +); + sub new { my ($class, %params) = @_; + # the roles param is required ... foreach ( @{$params{roles}} ) { unless ( $_->isa('Moose::Meta::Role') ) { @@ -39,6 +42,20 @@ sub new { Moose->throw_error("The list of roles must be instances of Moose::Meta::Role, not $_"); } } + + 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}}); @@ -62,7 +79,7 @@ sub add_method { if ($method->package_name ne $self->name) { $method = $method->clone( package_name => $self->name, - name => $method_name + name => $method_name ) if $method->can('clone'); } } @@ -71,19 +88,66 @@ sub add_method { $method = $self->wrap_method_body( body => $body, name => $method_name ); } - $self->get_method_map->{$method_name} = $method; + $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 apply_params { + my ($self, $role_params) = @_; + Class::MOP::load_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 ) = @_; + + Moose->throw_error( + 'Moose::Meta::Role::Composite instances can only be reinitialized from an existing metaclass instance' + ) + 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 -=head1 NAME - -Moose::Meta::Role::Composite - An object to represent the set of roles - =head1 DESCRIPTION A composite is a role that consists of a set of two or more roles. @@ -115,27 +179,24 @@ L object. This is a required option. If a name is not given, one is generated from the roles provided. -=back +=item * apply_params(\%role_params) -=back - -=head1 BUGS +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 +attribute. -All complex software has bugs lurking in it, and this module is no -exception. If you find a bug please either email me, or add the bug -to cpan-RT. +=item * reinitialize($metaclass) -=head1 AUTHOR +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. -Stevan Little Estevan@iinteractive.comE - -=head1 COPYRIGHT AND LICENSE +=back -Copyright 2006-2009 by Infinity Interactive, Inc. +=back -L +=head1 BUGS -This library is free software; you can redistribute it and/or modify -it under the same terms as Perl itself. +See L for details on reporting bugs. =cut