First stab at generate_role; metas within metas..
Shawn M Moore [Sun, 9 Nov 2008 04:21:47 +0000 (04:21 +0000)]
lib/MooseX/Role/Parameterized.pm
lib/MooseX/Role/Parameterized/Meta/Role.pm

index d2e40ad..f1fc28e 100644 (file)
@@ -6,6 +6,8 @@ extends 'Moose::Exporter';
 
 use MooseX::Role::Parameterized::Meta::Role;
 
+our $CURRENT_ROLE;
+
 __PACKAGE__->setup_import_methods(
     with_caller => ['parameter', 'role'],
 );
index 522c6ab..74f24e0 100644 (file)
@@ -32,6 +32,25 @@ sub construct_parameters {
     $self->parameter_metaclass->construct_instance(@_);
 }
 
+sub generate_role {
+    my $self = shift;
+    my %args = @_;
+
+    confess "A role generator is required to generate roles"
+        unless $self->has_role_generator;
+
+    my $role = Moose::Meta::Class->create_anon_class(
+        superclasses => ['Moose::Meta::Role'],
+    );
+
+    my $parameters = $self->construct_parameters(%args);
+
+    local $MooseX::Role::Parameterized::CURRENT_ROLE = $role;
+    $self->role_generator->($parameters, $role);
+
+    return $role->construct_instance;
+}
+
 __PACKAGE__->meta->make_immutable;
 no Moose;