X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=lib%2FMouse%2FRole.pm;h=511a4e56cdb87617cdf9f0db6a5f03b364edf3b6;hp=37007e58bf66b75e71bf58a838367fb8a2ccfac0;hb=013ee5f0c0ce5afa1fea9d45bd14bd8f8bfd67f4;hpb=6c169c5063b77a791818f5db2c1da3bd9b47d3f9 diff --git a/lib/Mouse/Role.pm b/lib/Mouse/Role.pm index 37007e5..511a4e5 100644 --- a/lib/Mouse/Role.pm +++ b/lib/Mouse/Role.pm @@ -1,93 +1,138 @@ -#!/usr/bin/env perl 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.50'; +use Carp qw(confess); +use Scalar::Util qw(blessed); + +use Mouse::Util qw(not_supported); use Mouse::Meta::Role; +use Mouse (); + +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'"; +} -our @EXPORT = qw(before after around has extends with requires excludes confess blessed); +sub with { + my $meta = Mouse::Meta::Role->initialize(scalar caller); + Mouse::Util::apply_all_roles($meta->name, @_); + return; +} -sub before { - my $meta = Mouse::Meta::Role->initialize(caller); +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 + + if(ref $name){ # has [qw(foo bar)] => (...) + for (@{$name}){ + $meta->add_attribute($_ => @_); + } + } + else{ # has foo => (...) + $meta->add_attribute($name => @_); + } + 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 has { - my $meta = Mouse::Meta::Role->initialize(caller); - - my $name = shift; - my %opts = @_; - $meta->add_attribute($name => \%opts); +sub super { + return if !defined $Mouse::SUPER_BODY; + $Mouse::SUPER_BODY->(@Mouse::SUPER_ARGS); } -sub extends { confess "Roles do not support 'extends'" } +sub override { + # my($name, $code) = @_; + Mouse::Meta::Role->initialize(scalar caller)->add_override_method_modifier(@_); + return; +} -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; +# We keep the same errors messages as Moose::Role emits, here. +sub inner { + Carp::croak "Roles cannot support 'inner'"; +} - Mouse::load_class($role); - $role->meta->apply($meta, %$args); +sub augment { + Carp::croak "Roles cannot support 'augment'"; } 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(@_); + return; } -sub excludes { confess "Mouse::Role does not currently support 'excludes'" } +sub excludes { + not_supported; +} -sub import { - strict->import; - warnings->import; +sub init_meta{ + shift; + my %args = @_; - my $caller = caller; - my $meta = Mouse::Meta::Role->initialize(caller); + my $class = $args{for_class} + or Carp::confess("Cannot call init_meta without specifying a for_class"); - no strict 'refs'; - no warnings 'redefine'; - *{$caller.'::meta'} = sub { $meta }; + my $metaclass = $args{metaclass} || 'Mouse::Meta::Role'; - Mouse::Role->export_to_level(1, @_); -} + my $meta = $metaclass->initialize($class); -sub unimport { - my $caller = caller; + $meta->add_method(meta => sub{ + $metaclass->initialize(ref($_[0]) || $_[0]); + }); - no strict 'refs'; - for my $keyword (@EXPORT) { - delete ${ $caller . '::' }{$keyword}; - } + # make a role type for each Mouse role + Mouse::Util::TypeConstraints::role_type($class) + unless Mouse::Util::TypeConstraints::find_type_constraint($class); + + return $meta; } 1; @@ -96,39 +141,61 @@ __END__ =head1 NAME -Mouse::Role +Mouse::Role - The Mouse Role + +=head1 VERSION + +This document describes Mouse version 0.50 + +=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 B method modifier. See L. -Sets up a "before" method modifier. See L or -L. +=head2 C<< after (method|methods) => CodeRef >> -=head2 after (method|methods) => Code +Sets up an B method modifier. See L. -Sets up an "after" method modifier. See L or -L. +=head2 C<< around (method|methods) => CodeRef >> -=head2 around (method|methods) => Code +Sets up an B method modifier. See L. -Sets up an "around" method modifier. See L or -L. +=head2 C -=head2 has (name|names) => parameters +Sets up the B keyword. See L. + +=head2 C<< override method => CodeRef >> + +Sets up an B method modifier. See L. + +=head2 C + +This is not supported in roles and emits an error. See L. + +=head2 C<< augment method => CodeRef >> + +This is not supported in roles and emits an error. See L. + +=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. @@ -140,8 +207,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