X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FRole.pm;h=37007e58bf66b75e71bf58a838367fb8a2ccfac0;hb=6c169c5063b77a791818f5db2c1da3bd9b47d3f9;hp=8eeadc9c481e704859fb38cd82d730c8d896464e;hpb=eb812bdec4270b062ab0f35e2d22696e92324dfe;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Role.pm b/lib/Mouse/Role.pm index 8eeadc9..37007e5 100644 --- a/lib/Mouse/Role.pm +++ b/lib/Mouse/Role.pm @@ -2,59 +2,146 @@ package Mouse::Role; use strict; use warnings; +use base 'Exporter'; -use Sub::Exporter; use Carp 'confess'; +use Scalar::Util 'blessed'; -do { - my $CALLER; - - my %exports = ( - extends => sub { - return sub { - confess "Role does not currently support 'extends'"; - } - }, - before => sub { - return sub { } - }, - after => sub { - return sub { } - }, - around => sub { - return sub { } - }, - has => sub { - return sub { } - }, - with => sub { - return sub { } - }, - requires => sub { - return sub { } - }, - excludes => sub { - return sub { } - }, - ); - - my $exporter = Sub::Exporter::build_exporter({ - exports => \%exports, - groups => { default => [':all'] }, - }); - - sub import { - $CALLER = caller; - - strict->import; - warnings->import; - - goto $exporter; +use Mouse::Meta::Role; + +our @EXPORT = qw(before after around has extends with requires excludes confess blessed); + +sub before { + my $meta = Mouse::Meta::Role->initialize(caller); + + my $code = pop; + for (@_) { + $meta->add_before_method_modifier($_ => $code); + } +} + +sub after { + my $meta = Mouse::Meta::Role->initialize(caller); + + my $code = pop; + for (@_) { + $meta->add_after_method_modifier($_ => $code); } +} - sub unimport { +sub around { + my $meta = Mouse::Meta::Role->initialize(caller); + + my $code = pop; + for (@_) { + $meta->add_around_method_modifier($_ => $code); } -}; +} + +sub has { + my $meta = Mouse::Meta::Role->initialize(caller); + + my $name = shift; + my %opts = @_; + + $meta->add_attribute($name => \%opts); +} + +sub extends { confess "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); +} + +sub requires { + my $meta = Mouse::Meta::Role->initialize(caller); + Carp::croak "Must specify at least one method" unless @_; + $meta->add_required_methods(@_); +} + +sub excludes { confess "Mouse::Role does not currently support 'excludes'" } + +sub import { + strict->import; + warnings->import; + + my $caller = caller; + my $meta = Mouse::Meta::Role->initialize(caller); + + no strict 'refs'; + no warnings 'redefine'; + *{$caller.'::meta'} = sub { $meta }; + + Mouse::Role->export_to_level(1, @_); +} + +sub unimport { + my $caller = caller; + + no strict 'refs'; + for my $keyword (@EXPORT) { + delete ${ $caller . '::' }{$keyword}; + } +} 1; +__END__ + +=head1 NAME + +Mouse::Role + +=head1 KEYWORDS + +=head2 meta -> Mouse::Meta::Role + +Returns this role's metaclass instance. + +=head2 before (method|methods) => Code + +Sets up a "before" method modifier. See L or +L. + +=head2 after (method|methods) => Code + +Sets up an "after" method modifier. See L or +L. + +=head2 around (method|methods) => Code + +Sets up an "around" method modifier. See L or +L. + +=head2 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 + +L for your convenience. + +=head2 blessed value -> ClassName | undef + +L for your convenience. + +=head1 MISC + +=head2 import + +Importing Mouse::Role will give you sugar. + +=head2 unimport + +Please unimport Mouse (C) so that if someone calls one of the +keywords (such as L) it will break loudly instead breaking subtly. + +=cut +