X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=lib%2FMouse%2FRole.pm;h=bc326656c02deb05988b025ab2b61e26278b9583;hp=efd7f951d59a2cb76e034507f3aa1175534ff040;hb=01afd8ffba9b9783e84c6cfc8ba45e11a0f5d8f4;hpb=6719984210754e8d012ae678536f194c35000823 diff --git a/lib/Mouse/Role.pm b/lib/Mouse/Role.pm index efd7f95..bc32665 100644 --- a/lib/Mouse/Role.pm +++ b/lib/Mouse/Role.pm @@ -1,17 +1,35 @@ package Mouse::Role; use strict; use warnings; -use base 'Exporter'; -use Carp 'confess', 'croak'; +use Exporter; + +use Carp 'confess'; use Scalar::Util 'blessed'; -use Mouse::Meta::Role; +use Mouse::Util qw(load_class get_code_package not_supported); +use Mouse (); + +our @ISA = qw(Exporter); + +our @EXPORT = qw( + extends with + has + before after around + override super + augment inner + + requires excludes + + blessed confess +); -our @EXPORT = qw(before after around super override inner augment has extends with requires excludes confess blessed); +our %is_removable = map{ $_ => undef } @EXPORT; +delete $is_removable{confess}; +delete $is_removable{blessed}; sub before { - my $meta = Mouse::Meta::Role->initialize(caller); + my $meta = Mouse::Meta::Role->initialize(scalar caller); my $code = pop; for (@_) { @@ -20,7 +38,7 @@ sub before { } sub after { - my $meta = Mouse::Meta::Role->initialize(caller); + my $meta = Mouse::Meta::Role->initialize(scalar caller); my $code = pop; for (@_) { @@ -29,7 +47,7 @@ sub after { } sub around { - my $meta = Mouse::Meta::Role->initialize(caller); + my $meta = Mouse::Meta::Role->initialize(scalar caller); my $code = pop; for (@_) { @@ -52,8 +70,8 @@ sub override { my $fullname = "${classname}::${name}"; defined &$fullname - && confess "Cannot add an override of method '$fullname' " . - "because there is a local version of '$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; @@ -66,41 +84,38 @@ sub override { # We keep the same errors messages as Moose::Role emits, here. sub inner { - croak "Moose::Role cannot support 'inner'"; + Carp::croak "Roles cannot support 'inner'"; } sub augment { - croak "Moose::Role cannot support 'augment'"; + Carp::croak "Roles cannot support 'augment'"; } sub has { - my $meta = Mouse::Meta::Role->initialize(caller); - + my $meta = Mouse::Meta::Role->initialize(scalar caller); my $name = shift; - my %opts = @_; - $meta->add_attribute($name => \%opts); + $meta->add_attribute($_ => @_) for ref($name) ? @{$name} : $name; } -sub extends { confess "Roles do not support 'extends'" } +sub extends { + Carp::croak "Roles do not support 'extends'" +} sub with { - my $meta = Mouse::Meta::Role->initialize(caller); - my $role = shift; - my $args = shift || {}; - confess "Mouse::Role only supports 'with' on individual roles at a time" if @_ || !ref $args; - - Mouse::load_class($role); - $role->meta->apply($meta, %$args); + my $meta = Mouse::Meta::Role->initialize(scalar caller); + Mouse::Util::apply_all_roles($meta->name, @_); } sub requires { - my $meta = Mouse::Meta::Role->initialize(caller); - Carp::croak "Must specify at least one method" unless @_; + my $meta = Mouse::Meta::Role->initialize(scalar caller); + $meta->throw_error("Must specify at least one method") unless @_; $meta->add_required_methods(@_); } -sub excludes { confess "Mouse::Role does not currently support 'excludes'" } +sub excludes { + not_supported; +} sub import { my $class = shift; @@ -116,11 +131,9 @@ sub import { return; } - my $meta = Mouse::Meta::Role->initialize(caller); - - no strict 'refs'; - no warnings 'redefine'; - *{$caller.'::meta'} = sub { $meta }; + Mouse::Meta::Role->initialize($caller)->add_method(meta => sub { + return Mouse::Meta::Role->initialize(ref($_[0]) || $_[0]); + }); Mouse::Role->export_to_level(1, @_); } @@ -128,10 +141,21 @@ sub import { sub unimport { my $caller = caller; - no strict 'refs'; + my $stash = do{ + no strict 'refs'; + \%{$caller . '::'} + }; + for my $keyword (@EXPORT) { - delete ${ $caller . '::' }{$keyword}; + my $code; + if(exists $is_removable{$keyword} + && ($code = $caller->can($keyword)) + && get_code_package($code) eq __PACKAGE__){ + + delete $stash->{$keyword}; + } } + return; } 1; @@ -140,55 +164,60 @@ __END__ =head1 NAME -Mouse::Role - define a role in Mouse +Mouse::Role - The Mouse Role + +=head1 SYNOPSIS + + package MyRole; + use Mouse::Role; =head1 KEYWORDS -=head2 meta -> Mouse::Meta::Role +=head2 C<< meta -> Mouse::Meta::Role >> Returns this role's metaclass instance. -=head2 before (method|methods) => Code +=head2 C<< before (method|methods) -> CodeRef >> -Sets up a "before" method modifier. See L or +Sets up a B method modifier. See L or L. -=head2 after (method|methods) => Code +=head2 C<< after (method|methods) => CodeRef >> -Sets up an "after" method modifier. See L or +Sets up an B method modifier. See L or L. -=head2 around (method|methods) => Code +=head2 C<< around (method|methods) => CodeRef >> -Sets up an "around" method modifier. See L or +Sets up an B method modifier. See L or L. -=item B +=head2 C -Sets up the "super" keyword. See L. +Sets up the B keyword. See L. -=item B +=head2 C<< override method => CodeRef >> -Sets up an "override" method modifier. See L. +Sets up an B method modifier. See L. -=item B +=head2 C -This is not supported and emits an error. See L. +This is not supported in roles and emits an error. See L. -=item B +=head2 C<< augment method => CodeRef >> -This is not supported and emits an error. See L. +This is not supported in roles and emits an error. See L. -=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. -=head2 confess error -> BOOM +=head2 C<< confess(error) -> BOOM >> L for your convenience. -=head2 blessed value -> ClassName | undef +=head2 C<< blessed(value) -> ClassName | undef >> L for your convenience. @@ -200,8 +229,12 @@ Importing Mouse::Role will give you sugar. =head2 unimport -Please unimport Mouse (C) 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) it will break loudly instead breaking subtly. +=head1 SEE ALSO + +L + =cut