X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FModule.pm;h=227313e4bb060b3a85a2a1fb065f153bacd7acae;hp=07c68537bb17a01bf5480046e51535e500008353;hb=034587d897c53d4df84085a9229334bd2d51f1c6;hpb=deb9a0f32002cd07012c50884a227335b93f1449 diff --git a/lib/Mouse/Meta/Module.pm b/lib/Mouse/Meta/Module.pm index 07c6853..227313e 100755 --- a/lib/Mouse/Meta/Module.pm +++ b/lib/Mouse/Meta/Module.pm @@ -1,13 +1,9 @@ package Mouse::Meta::Module; -use strict; -use warnings; +use Mouse::Util qw/:meta get_code_package load_class not_supported/; # enables strict and warnings use Carp (); use Scalar::Util qw/blessed weaken/; -use Mouse::Util qw/:meta get_code_package not_supported load_class/; - - my %METAS; sub _metaclass_cache { # DEPRECATED @@ -88,14 +84,14 @@ sub add_method { } if(ref($code) ne 'CODE'){ - not_supported 'add_method for a method object'; + $code = \&{$code}; # coerce } - $self->{methods}->{$name}++; # Moose stores meta object here. + $self->{methods}->{$name} = $code; # Moose stores meta object here. my $pkg = $self->name; no strict 'refs'; - no warnings 'redefine'; + no warnings 'redefine', 'once'; *{ $pkg . '::' . $name } = $code; } @@ -115,13 +111,37 @@ sub _code_is_mine{ sub has_method { my($self, $method_name) = @_; - return 1 if $self->{methods}->{$method_name}; + defined($method_name) + or $self->throw_error('You must define a method name'); - my $code = do{ no strict 'refs'; *{$self->{package} . '::' . $method_name}{CODE} }; + return 1 if $self->{methods}{$method_name}; + + my $code = do{ + no strict 'refs'; + no warnings 'once'; + *{ $self->{package} . '::' . $method_name }{CODE}; + }; return $code && $self->_code_is_mine($code); } +sub get_method_body{ + my($self, $method_name) = @_; + + defined($method_name) + or $self->throw_error('You must define a method name'); + + return $self->{methods}{$method_name} ||= do{ + my $code = do{ + no strict 'refs'; + no warnings 'once'; + *{$self->{package} . '::' . $method_name}{CODE}; + }; + + ($code && $self->_code_is_mine($code)) ? $code : undef; + }; +} + sub get_method{ my($self, $method_name) = @_; @@ -152,36 +172,37 @@ sub get_method_list { my %IMMORTALS; sub create { - my($class, $package_name, %options) = @_; + my($self, $package_name, %options) = @_; - $class->throw_error('You must pass a package name') if @_ < 2; + my $class = ref($self) || $self; + $self->throw_error('You must pass a package name') if @_ < 2; my $superclasses; if(exists $options{superclasses}){ - if($class->isa('Mouse::Meta::Role')){ + if($self->isa('Mouse::Meta::Role')){ delete $options{superclasses}; } else{ $superclasses = delete $options{superclasses}; (ref $superclasses eq 'ARRAY') - || $class->throw_error("You must pass an ARRAY ref of superclasses"); + || $self->throw_error("You must pass an ARRAY ref of superclasses"); } } my $attributes = delete $options{attributes}; if(defined $attributes){ (ref $attributes eq 'ARRAY' || ref $attributes eq 'HASH') - || $class->throw_error("You must pass an ARRAY ref of attributes"); + || $self->throw_error("You must pass an ARRAY ref of attributes"); } my $methods = delete $options{methods}; if(defined $methods){ (ref $methods eq 'HASH') - || $class->throw_error("You must pass a HASH ref of methods"); + || $self->throw_error("You must pass a HASH ref of methods"); } my $roles = delete $options{roles}; if(defined $roles){ (ref $roles eq 'ARRAY') - || $class->throw_error("You must pass an ARRAY ref of roles"); + || $self->throw_error("You must pass an ARRAY ref of roles"); } my $mortal; my $cache_key; @@ -209,14 +230,13 @@ sub get_method_list { ${ $package_name . '::AUTHORITY' } = delete $options{authority} if exists $options{authority}; } - my $meta = $class->initialize( $package_name, %options); + my $meta = $self->initialize( $package_name, %options); weaken $METAS{$package_name} if $mortal; - # FIXME totally lame - $meta->add_method('meta' => sub { - $class->initialize(ref($_[0]) || $_[0]); + $meta->add_method(meta => sub{ + $self->initialize(ref($_[0]) || $_[0]); }); $meta->superclasses(@{$superclasses}) @@ -304,6 +324,10 @@ __END__ Mouse::Meta::Module - The base class for Mouse::Meta::Class and Mouse::Meta::Role +=head1 VERSION + +This document describes Mouse version 0.40 + =head1 SEE ALSO L