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=383f51d93f6e4e2e4aa57ec075aa3be06145239e;hp=1c18447fcb5d8d61b41a1c6273651cf427ddd850;hb=2591e962421f07deae90d93875aa129c57d841af;hpb=7eb3a8d52207d71fe91dad750a74c9f04bea568d diff --git a/lib/Mouse/Meta/Module.pm b/lib/Mouse/Meta/Module.pm index 1c18447..383f51d 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 @@ -42,9 +38,9 @@ sub get_metaclass_by_name { $METAS{$_[0]} } #sub does_metaclass_exist { defined $METAS{$_[0]} } #sub remove_metaclass_by_name { delete $METAS{$_[0]} } +sub name; - -sub name { $_[0]->{package} } +sub namespace; # The followings are Class::MOP specific methods @@ -71,12 +67,6 @@ sub get_attribute { $_[0]->{attributes}->{$_[1]} } sub get_attribute_list{ keys %{$_[0]->{attributes}} } sub remove_attribute { delete $_[0]->{attributes}->{$_[1]} } -sub namespace{ - my $name = $_[0]->{package}; - no strict 'refs'; - return \%{ $name . '::' }; -} - sub add_method { my($self, $name, $code) = @_; @@ -88,14 +78,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 +105,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'); + + return 1 if $self->{methods}{$method_name}; - my $code = do{ no strict 'refs'; *{$self->{package} . '::' . $method_name}{CODE} }; + 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) = @_; @@ -129,11 +143,11 @@ sub get_method{ my $method_metaclass = $self->method_metaclass; load_class($method_metaclass); - my $package = $self->name; - return $method_metaclass->new( - body => $package->can($method_name), - name => $method_name, - package => $package, + return $method_metaclass->wrap( + body => $self->get_method_body($method_name), + name => $method_name, + package => $self->name, + associated_metaclass => $self, ); } @@ -304,6 +318,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