Support modifier by regexp
[gitmo/Mouse.git] / lib / Mouse / Role.pm
index 4f02065..511a4e5 100644 (file)
@@ -1,84 +1,96 @@
 package Mouse::Role;
-use strict;
-use warnings;
-use base 'Exporter';
+use Mouse::Exporter; # enables strict and warnings
 
-use Carp 'confess';
-use Scalar::Util 'blessed';
+our $VERSION = '0.50';
 
-use Mouse::Util qw(load_class not_supported);
+use Carp         qw(confess);
+use Scalar::Util qw(blessed);
+
+use Mouse::Util  qw(not_supported);
+use Mouse::Meta::Role;
 use Mouse ();
 
-our @EXPORT = qw(before after around super override inner augment has extends with requires excludes confess blessed);
+Mouse::Exporter->setup_import_methods(
+    as_is => [qw(
+        extends with
+        has
+        before after around
+        override super
+        augment  inner
+
+        requires excludes
+    ),
+        \&Scalar::Util::blessed,
+        \&Carp::confess,
+    ],
+);
 
-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 has {
+    my $meta = Mouse::Meta::Role->initialize(scalar caller);
+    my $name = shift;
 
-our %is_removable = map{ $_ => undef } @EXPORT;
-delete $is_removable{confess};
-delete $is_removable{blessed};
+    $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;
 }
 
 
 sub super {
-    return unless $Mouse::SUPER_BODY; 
+    return if !defined $Mouse::SUPER_BODY;
     $Mouse::SUPER_BODY->(@Mouse::SUPER_ARGS);
 }
 
 sub override {
-    my $classname = caller;
-    my $meta = Mouse::Meta::Role->initialize($classname);
-
-    my $name = shift;
-    my $code = shift;
-    my $fullname = "${classname}::${name}";
-
-    defined &$fullname
-        && $meta->throw_error("Cannot add an override of method '$fullname' "
-                            . "because there is a local version of '$fullname'");
-
-    $meta->add_override_method_modifier($name => sub {
-        local $Mouse::SUPER_PACKAGE = shift;
-        local $Mouse::SUPER_BODY = shift;
-        local @Mouse::SUPER_ARGS = @_;
-
-        $code->(@_);
-    });
+    # 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.
@@ -90,73 +102,37 @@ sub augment {
     Carp::croak "Roles cannot support 'augment'";
 }
 
-sub has {
-    my $meta = Mouse::Meta::Role->initialize(scalar caller);
-
-    my $name = shift;
-    my %opts = @_;
-
-    $meta->add_attribute($name => \%opts);
-}
-
-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 {
     not_supported;
 }
 
-sub import {
-    my $class = shift;
+sub init_meta{
+    shift;
+    my %args = @_;
 
-    strict->import;
-    warnings->import;
+    my $class = $args{for_class}
+        or Carp::confess("Cannot call init_meta without specifying a for_class");
 
-    my $caller = caller;
+    my $metaclass  = $args{metaclass}  || 'Mouse::Meta::Role';
 
-    # we should never export to main
-    if ($caller eq 'main') {
-        warn qq{$class does not export its sugar to the 'main' package.\n};
-        return;
-    }
+    my $meta = $metaclass->initialize($class);
 
-    Mouse::Meta::Role->initialize($caller)->add_method(meta => sub {
-        return Mouse::Meta::Role->initialize(ref($_[0]) || $_[0]);
+    $meta->add_method(meta => sub{
+        $metaclass->initialize(ref($_[0]) || $_[0]);
     });
 
-    Mouse::Role->export_to_level(1, @_);
-}
-
-sub unimport {
-    my $caller = caller;
-
-    my $stash = do{
-        no strict 'refs';
-        \%{$caller . '::'}
-    };
+    # make a role type for each Mouse role
+    Mouse::Util::TypeConstraints::role_type($class)
+        unless Mouse::Util::TypeConstraints::find_type_constraint($class);
 
-    for my $keyword (@EXPORT) {
-        my $code;
-        if(exists $is_removable{$keyword}
-            && ($code = $caller->can($keyword))
-            && (Mouse::Util::get_code_info($code))[0] eq __PACKAGE__){
-
-            delete $stash->{$keyword};
-        }
-    }
-    return;
+    return $meta;
 }
 
 1;
@@ -165,59 +141,61 @@ __END__
 
 =head1 NAME
 
-Mouse::Role - define a role in Mouse
+Mouse::Role - The Mouse Role
 
-=head1 KEYWORDS
+=head1 VERSION
 
-=head2 meta -> Mouse::Meta::Role
+This document describes Mouse version 0.50
 
-Returns this role's metaclass instance.
+=head1 SYNOPSIS
+
+    package MyRole;
+    use Mouse::Role;
+
+=head1 KEYWORDS
 
-=head2 before (method|methods) => Code
+=head2 C<< meta -> Mouse::Meta::Role >>
 
-Sets up a "before" method modifier. See L<Moose/before> or
-L<Class::Method::Modifiers/before>.
+Returns this role's metaclass instance.
 
-=head2 after (method|methods) => Code
+=head2 C<< before (method|methods) -> CodeRef >>
 
-Sets up an "after" method modifier. See L<Moose/after> or
-L<Class::Method::Modifiers/after>.
+Sets up a B<before> method modifier. See L<Moose/before>.
 
-=head2 around (method|methods) => Code
+=head2 C<< after (method|methods) => CodeRef >>
 
-Sets up an "around" method modifier. See L<Moose/around> or
-L<Class::Method::Modifiers/around>.
+Sets up an B<after> method modifier. See L<Moose/after>.
 
-=over 4
+=head2 C<< around (method|methods) => CodeRef >>
 
-=item B<super>
+Sets up an B<around> method modifier. See L<Moose/around>.
 
-Sets up the "super" keyword. See L<Moose/super>.
+=head2 C<super>
 
-=item B<override ($name, &sub)>
+Sets up the B<super> keyword. See L<Moose/super>.
 
-Sets up an "override" method modifier. See L<Moose/Role/override>.
+=head2  C<< override method => CodeRef >>
 
-=item B<inner>
+Sets up an B<override> method modifier. See L<Moose/Role/override>.
 
-This is not supported and emits an error. See L<Moose/Role>.
+=head2 C<inner>
 
-=item B<augment ($name, &sub)>
+This is not supported in roles and emits an error. See L<Moose/Role>.
 
-This is not supported and emits an error. See L<Moose/Role>.
+=head2 C<< augment method => CodeRef >>
 
-=back
+This is not supported in roles and emits an error. See L<Moose/Role>.
 
-=head2 has (name|names) => parameters
+=head2 C<< has (name|names) => parameters >>
 
 Sets up an attribute (or if passed an arrayref of names, multiple attributes) to
 this role. See L<Mouse/has>.
 
-=head2 confess error -> BOOM
+=head2 C<< confess(error) -> BOOM >>
 
 L<Carp/confess> for your convenience.
 
-=head2 blessed value -> ClassName | undef
+=head2 C<< blessed(value) -> ClassName | undef >>
 
 L<Scalar::Util/blessed> for your convenience.
 
@@ -229,8 +207,12 @@ Importing Mouse::Role will give you sugar.
 
 =head2 unimport
 
-Please unimport Mouse (C<no Mouse::Role>) so that if someone calls one of the
+Please unimport (C<< no Mouse::Role >>) so that if someone calls one of the
 keywords (such as L</has>) it will break loudly instead breaking subtly.
 
+=head1 SEE ALSO
+
+L<Moose::Role>
+
 =cut