X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FRole.pm;h=dfb1520e0ad9b199d73c7c83946420639122927c;hb=903b25aa09b1dea003bc50a6f1e5307ae40629d2;hp=efd7f951d59a2cb76e034507f3aa1175534ff040;hpb=6719984210754e8d012ae678536f194c35000823;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Role.pm b/lib/Mouse/Role.pm index efd7f95..dfb1520 100644 --- a/lib/Mouse/Role.pm +++ b/lib/Mouse/Role.pm @@ -1,207 +1,246 @@ package Mouse::Role; -use strict; -use warnings; -use base 'Exporter'; +use Mouse::Exporter; # enables strict and warnings -use Carp 'confess', 'croak'; -use Scalar::Util 'blessed'; +our $VERSION = '0.91'; -use Mouse::Meta::Role; +use Carp (); +use Scalar::Util (); -our @EXPORT = qw(before after around super override inner augment has extends with requires excludes confess blessed); +use Mouse (); -sub before { - my $meta = Mouse::Meta::Role->initialize(caller); +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, + ], +); + + +sub extends { + Carp::croak "Roles do not support 'extends'"; +} + +sub with { + Mouse::Util::apply_all_roles(scalar(caller), @_); + return; +} + +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 + + for my $n(ref($name) ? @{$name} : $name){ + $meta->add_attribute($n => @_); + } + 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(caller); - + 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(caller); - + 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 - && confess "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. 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 $name = shift; - my %opts = @_; +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; +} - $meta->add_attribute($name => \%opts); +sub excludes { + Mouse::Util::not_supported(); } -sub extends { confess "Roles do not support 'extends'" } +sub init_meta{ + shift; + my %args = @_; -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; + my $class = $args{for_class} + or Carp::confess("Cannot call init_meta without specifying a for_class"); - Mouse::load_class($role); - $role->meta->apply($meta, %$args); -} + my $metaclass = $args{metaclass} || 'Mouse::Meta::Role'; -sub requires { - my $meta = Mouse::Meta::Role->initialize(caller); - Carp::croak "Must specify at least one method" unless @_; - $meta->add_required_methods(@_); + 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; } -sub excludes { confess "Mouse::Role does not currently support 'excludes'" } +1; -sub import { - my $class = shift; +__END__ - strict->import; - warnings->import; +=head1 NAME - my $caller = caller; +Mouse::Role - The Mouse 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; - } +=head1 VERSION - my $meta = Mouse::Meta::Role->initialize(caller); +This document describes Mouse version 0.91 - no strict 'refs'; - no warnings 'redefine'; - *{$caller.'::meta'} = sub { $meta }; +=head1 SYNOPSIS - Mouse::Role->export_to_level(1, @_); -} + package Comparable; + use Mouse::Role; # the package is now a Mouse role -sub unimport { - my $caller = caller; + # Declare methods that are required by this role + requires qw(compare); - no strict 'refs'; - for my $keyword (@EXPORT) { - delete ${ $caller . '::' }{$keyword}; + # Define methods this role provides + sub equals { + my($self, $other) = @_; + return $self->compare($other) == 0; } -} -1; + # and later + package MyObject; + use Mouse; + with qw(Comparable); # Now MyObject can equals() -__END__ + sub compare { + # ... + } -=head1 NAME + my $foo = MyObject->new(); + my $bar = MyObject->new(); + $obj->equals($bar); # yes, it is comparable -Mouse::Role - define a role in Mouse +=head1 DESCRIPTION -=head1 KEYWORDS +This module declares the caller class to be a Mouse role. -=head2 meta -> Mouse::Meta::Role +The concept of roles is documented in L. +This document serves as API documentation. -Returns this role's metaclass instance. +=head1 EXPORTED FUNCTIONS -=head2 before (method|methods) => Code +Mouse::Role supports all of the functions that Mouse exports, but +differs slightly in how some items are handled (see L below +for details). -Sets up a "before" method modifier. See L or -L. +Mouse::Role also offers two role-specific keywords: -=head2 after (method|methods) => Code +=head2 C<< requires(@method_names) >> -Sets up an "after" method modifier. See L or -L. +Roles can require that certain methods are implemented by any class which +C the role. -=head2 around (method|methods) => Code +Note that attribute accessors also count as methods for the purposes of +satisfying the requirements of a role. -Sets up an "around" method modifier. See L or -L. +=head2 C<< excludes(@role_names) >> -=item B +This is exported but not implemented in Mouse. -Sets up the "super" keyword. See L. +=head1 IMPORT AND UNIMPORT -=item B +=head2 import -Sets up an "override" method modifier. See L. +Importing Mouse::Role will give you sugar. C<-traits> are also supported. -=item B +=head2 unimport -This is not supported and emits an error. See L. +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. -=item B +=head1 CAVEATS -This is not supported and emits an error. See L. +Role support has only a few caveats: -=head2 has (name|names) => parameters +=over -Sets up an attribute (or if passed an arrayref of names, multiple attributes) to -this role. See L. +=item * -=head2 confess error -> BOOM +Roles cannot use the C keyword; it will throw an exception for now. +The same is true of the C and C keywords (not sure those +really make sense for roles). All other Mouse keywords will be I +so that they can be applied to the consuming class. -L for your convenience. +=item * -=head2 blessed value -> ClassName | undef +Role composition does its best to B be order-sensitive when it comes to +conflict resolution and requirements detection. However, it is order-sensitive +when it comes to method modifiers. All before/around/after modifiers are +included whenever a role is composed into a class, and then applied in the order +in which the roles are used. This also means that there is no conflict for +before/around/after modifiers. -L for your convenience. +In most cases, this will be a non-issue; however, it is something to keep in +mind when using method modifiers in a role. You should never assume any +ordering. -=head1 MISC +=back -=head2 import +=head1 SEE ALSO -Importing Mouse::Role will give you sugar. +L -=head2 unimport +L -Please unimport Mouse (C) so that if someone calls one of the -keywords (such as L) it will break loudly instead breaking subtly. +L + +L =cut