Add composition_class_roles to Meta::Class and build the Role::Composite using that.
[gitmo/Moose.git] / lib / Moose / Meta / Role / Composite.pm
index 217ca8a..4c6b13e 100644 (file)
@@ -6,32 +6,39 @@ use metaclass;
 
 use Scalar::Util 'blessed';
 
-our $VERSION   = '0.73_01';
+our $VERSION   = '0.89_02';
 $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',
     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 +46,23 @@ sub new {
             Moose->throw_error("The list of roles must be instances of Moose::Meta::Role, not $_");
         }
     }
+
+    my @composition_roles = map {
+        $_->has_composition_class_roles
+            ? @{ $_->composition_class_roles }
+            : ()
+    } @{ $params{roles} };
+
+    if (@composition_roles) {
+        my $meta = Moose::Meta::Class->create_anon_class(
+            superclasses => [ $class ],
+            roles        => [ @composition_roles ],
+            cache        => 1,
+        );
+        $meta->add_method(meta => sub { $meta });
+        $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 +86,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');
         }
     }
@@ -74,6 +98,17 @@ sub add_method {
     $self->get_method_map->{$method_name} = $method;
 }
 
+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;
+}
+
 1;
 
 __END__
@@ -115,6 +150,13 @@ L<Moose::Meta::Role> object. This is a required option.
 
 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.
+
 =back
 
 =back