X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FRole.pm;h=e905004c34f136d2cf4aff713c1e46b5de3846dc;hb=7daedfff24bc2061ea1028f415b2e914bd8f1639;hp=7530423fa2af0a890e11fd3065fb005995c78484;hpb=f9e68395ec48e239c2a2c77d15399aac9497b937;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Role.pm b/lib/Mouse/Role.pm index 7530423..e905004 100644 --- a/lib/Mouse/Role.pm +++ b/lib/Mouse/Role.pm @@ -1,33 +1,155 @@ -#!/usr/bin/env perl package Mouse::Role; use strict; use warnings; +use base 'Exporter'; -use Sub::Exporter; +use Carp 'confess'; +use Scalar::Util 'blessed'; -do { - my $CALLER; +use Mouse::Meta::Role; - my %exports = ( - ); +our @EXPORT = qw(before after around has extends with requires excludes confess blessed); - my $exporter = Sub::Exporter::build_exporter({ - exports => \%exports, - groups => { default => [':all'] }, - }); +sub before { + my $meta = Mouse::Meta::Role->initialize(caller); - sub import { - $CALLER = caller; + my $code = pop; + for (@_) { + $meta->add_before_method_modifier($_ => $code); + } +} - strict->import; - warnings->import; +sub after { + my $meta = Mouse::Meta::Role->initialize(caller); - goto $exporter; + my $code = pop; + for (@_) { + $meta->add_after_method_modifier($_ => $code); } +} + +sub around { + my $meta = Mouse::Meta::Role->initialize(caller); - sub unimport { + 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 { + my $class = shift; + + strict->import; + warnings->import; + + my $caller = caller; + + # 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 = 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 +