X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FRole.pm;h=17b9a203245b0ebe9622ad949cce6ab7e15d5ee0;hb=d468e99669162209fc407bc22706a91b8675dbbc;hp=df249a8f330eb3f94627b2cb1de62d7c8348354c;hpb=0740bdfadd961417299539d25ee24b0c9e1938e5;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Role.pm b/lib/Mouse/Role.pm index df249a8..17b9a20 100644 --- a/lib/Mouse/Role.pm +++ b/lib/Mouse/Role.pm @@ -1,82 +1,88 @@ 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.92'; + +use Carp (); +use Scalar::Util (); -use Mouse::Util qw(load_class not_supported); use Mouse (); -our @EXPORT = qw( - extends with - has - before after around - override super - augment inner +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, + ], +); - requires excludes - blessed confess -); +sub extends { + Carp::croak "Roles do not support 'extends'"; +} -our %is_removable = map{ $_ => undef } @EXPORT; -delete $is_removable{confess}; -delete $is_removable{blessed}; +sub with { + Mouse::Util::apply_all_roles(scalar(caller), @_); + return; +} -sub before { +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(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. @@ -88,73 +94,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; + Mouse::Util::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 . '::'} - }; - - for my $keyword (@EXPORT) { - my $code; - if(exists $is_removable{$keyword} - && ($code = $caller->can($keyword)) - && (Mouse::Util::get_code_info($code))[0] eq __PACKAGE__){ + # make a role type for each Mouse role + Mouse::Util::TypeConstraints::role_type($class) + unless Mouse::Util::TypeConstraints::find_type_constraint($class); - delete $stash->{$keyword}; - } - } - return; + return $meta; } 1; @@ -163,72 +133,114 @@ __END__ =head1 NAME -Mouse::Role - define a role in Mouse +Mouse::Role - The Mouse Role + +=head1 VERSION + +This document describes Mouse version 0.92 -=head1 KEYWORDS +=head1 SYNOPSIS -=head2 meta -> Mouse::Meta::Role + package Comparable; + use Mouse::Role; # the package is now a Mouse role -Returns this role's metaclass instance. + # Declare methods that are required by this role + requires qw(compare); -=head2 before (method|methods) => Code + # Define methods this role provides + sub equals { + my($self, $other) = @_; + return $self->compare($other) == 0; + } -Sets up a "before" method modifier. See L or -L. + # and later + package MyObject; + use Mouse; + with qw(Comparable); # Now MyObject can equals() -=head2 after (method|methods) => Code + sub compare { + # ... + } -Sets up an "after" method modifier. See L or -L. + my $foo = MyObject->new(); + my $bar = MyObject->new(); + $obj->equals($bar); # yes, it is comparable -=head2 around (method|methods) => Code +=head1 DESCRIPTION -Sets up an "around" method modifier. See L or -L. +This module declares the caller class to be a Mouse role. -=over 4 +The concept of roles is documented in L. +This document serves as API documentation. -=item B +=head1 EXPORTED FUNCTIONS -Sets up the "super" keyword. See L. +Mouse::Role supports all of the functions that Mouse exports, but +differs slightly in how some items are handled (see L below +for details). -=item B +Mouse::Role also offers two role-specific keywords: -Sets up an "override" method modifier. See L. +=head2 C<< requires(@method_names) >> -=item B +Roles can require that certain methods are implemented by any class which +C the role. -This is not supported and emits an error. See L. +Note that attribute accessors also count as methods for the purposes of +satisfying the requirements of a role. -=item B +=head2 C<< excludes(@role_names) >> -This is not supported and emits an error. See L. +This is exported but not implemented in Mouse. -=back +=head1 IMPORT AND UNIMPORT -=head2 has (name|names) => parameters +=head2 import -Sets up an attribute (or if passed an arrayref of names, multiple attributes) to -this role. See L. +Importing Mouse::Role will give you sugar. C<-traits> are also supported. -=head2 confess error -> BOOM +=head2 unimport -L for your convenience. +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. -=head2 blessed value -> ClassName | undef +=head1 CAVEATS -L for your convenience. +Role support has only a few caveats: -=head1 MISC +=over -=head2 import +=item * -Importing Mouse::Role will give you sugar. +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. -=head2 unimport +=item * -Please unimport Mouse (C) so that if someone calls one of the -keywords (such as L) it will break loudly instead breaking subtly. +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. + +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. + +=back + +=head1 SEE ALSO + +L + +L + +L + +L =cut