Support modifier by regexp
[gitmo/Mouse.git] / lib / Mouse / Role.pm
index da5b302..511a4e5 100644 (file)
@@ -1,11 +1,14 @@
 package Mouse::Role;
-use Mouse::Util qw(not_supported); # enables strict and warnings
+use Mouse::Exporter; # enables strict and warnings
 
-use Carp qw(confess);
+our $VERSION = '0.50';
+
+use Carp         qw(confess);
 use Scalar::Util qw(blessed);
 
+use Mouse::Util  qw(not_supported);
+use Mouse::Meta::Role;
 use Mouse ();
-use Mouse::Exporter;
 
 Mouse::Exporter->setup_import_methods(
     as_is => [qw(
@@ -22,44 +25,60 @@ Mouse::Exporter->setup_import_methods(
     ],
 );
 
-# XXX: for backward compatibility
-our @EXPORT = qw(
-    extends with
-    has
-    before after around
-    override super
-    augment  inner
 
-    requires excludes
+sub extends  {
+    Carp::croak "Roles do not support 'extends'";
+}
 
-    blessed confess
-);
+sub with     {
+    my $meta = Mouse::Meta::Role->initialize(scalar caller);
+    Mouse::Util::apply_all_roles($meta->name, @_);
+    return;
+}
 
-sub before {
+sub has {
     my $meta = Mouse::Meta::Role->initialize(scalar caller);
+    my $name = shift;
+
+    $meta->throw_error(q{Usage: has 'name' => ( key => value, ... )})
+        if @_ % 2; # odd number of arguments
 
+    if(ref $name){ # has [qw(foo bar)] => (...)
+        for (@{$name}){
+            $meta->add_attribute($_ => @_);
+        }
+    }
+    else{ # has foo => (...)
+        $meta->add_attribute($name => @_);
+    }
+    return;
+}
+
+sub before {
+    my $meta = Mouse::Meta::Role->initialize(scalar caller);
     my $code = pop;
-    for (@_) {
-        $meta->add_before_method_modifier($_ => $code);
+    for my $name($meta->_collect_methods(@_)) {
+        $meta->add_before_method_modifier($name => $code);
     }
+    return;
 }
 
 sub after {
     my $meta = Mouse::Meta::Role->initialize(scalar caller);
-
     my $code = pop;
-    for (@_) {
-        $meta->add_after_method_modifier($_ => $code);
+    for my $name($meta->_collect_methods(@_)) {
+        $meta->add_after_method_modifier($name => $code);
     }
+    return;
 }
 
 sub around {
     my $meta = Mouse::Meta::Role->initialize(scalar caller);
-
     my $code = pop;
-    for (@_) {
-        $meta->add_around_method_modifier($_ => $code);
+    for my $name($meta->_collect_methods(@_)) {
+        $meta->add_around_method_modifier($name => $code);
     }
+    return;
 }
 
 
@@ -71,6 +90,7 @@ sub super {
 sub override {
     # my($name, $code) = @_;
     Mouse::Meta::Role->initialize(scalar caller)->add_override_method_modifier(@_);
+    return;
 }
 
 # We keep the same errors messages as Moose::Role emits, here.
@@ -82,26 +102,11 @@ sub augment {
     Carp::croak "Roles cannot support 'augment'";
 }
 
-sub has {
-    my $meta = Mouse::Meta::Role->initialize(scalar caller);
-    my $name = shift;
-
-    $meta->add_attribute($_ => @_) for ref($name) ? @{$name} : $name;
-}
-
-sub extends  {
-    Carp::croak "Roles do not support 'extends'"
-}
-
-sub with     {
-    my $meta = Mouse::Meta::Role->initialize(scalar caller);
-    Mouse::Util::apply_all_roles($meta->name, @_);
-}
-
 sub requires {
     my $meta = Mouse::Meta::Role->initialize(scalar caller);
     $meta->throw_error("Must specify at least one method") unless @_;
     $meta->add_required_methods(@_);
+    return;
 }
 
 sub excludes {
@@ -109,19 +114,24 @@ sub excludes {
 }
 
 sub init_meta{
-    my($class, %args) = @_;
+    shift;
+    my %args = @_;
 
-    my $for_class = $args{for_class}
+    my $class = $args{for_class}
         or Carp::confess("Cannot call init_meta without specifying a for_class");
 
     my $metaclass  = $args{metaclass}  || 'Mouse::Meta::Role';
 
-    my $meta = $metaclass->initialize($for_class);
+    my $meta = $metaclass->initialize($class);
 
     $meta->add_method(meta => sub{
         $metaclass->initialize(ref($_[0]) || $_[0]);
     });
 
+    # make a role type for each Mouse role
+    Mouse::Util::TypeConstraints::role_type($class)
+        unless Mouse::Util::TypeConstraints::find_type_constraint($class);
+
     return $meta;
 }
 
@@ -133,6 +143,10 @@ __END__
 
 Mouse::Role - The Mouse Role
 
+=head1 VERSION
+
+This document describes Mouse version 0.50
+
 =head1 SYNOPSIS
 
     package MyRole;
@@ -146,18 +160,15 @@ Returns this role's metaclass instance.
 
 =head2 C<< before (method|methods) -> CodeRef >>
 
-Sets up a B<before> method modifier. See L<Moose/before> or
-L<Class::Method::Modifiers/before>.
+Sets up a B<before> method modifier. See L<Moose/before>.
 
 =head2 C<< after (method|methods) => CodeRef >>
 
-Sets up an B<after> method modifier. See L<Moose/after> or
-L<Class::Method::Modifiers/after>.
+Sets up an B<after> method modifier. See L<Moose/after>.
 
 =head2 C<< around (method|methods) => CodeRef >>
 
-Sets up an B<around> method modifier. See L<Moose/around> or
-L<Class::Method::Modifiers/around>.
+Sets up an B<around> method modifier. See L<Moose/around>.
 
 =head2 C<super>