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=ec9932ffb8f251538516bb08c76c36812d6e497e;hp=07c68537bb17a01bf5480046e51535e500008353;hb=013ee5f0c0ce5afa1fea9d45bd14bd8f8bfd67f4;hpb=deb9a0f32002cd07012c50884a227335b93f1449 diff --git a/lib/Mouse/Meta/Module.pm b/lib/Mouse/Meta/Module.pm index 07c6853..ec9932f 100755 --- a/lib/Mouse/Meta/Module.pm +++ b/lib/Mouse/Meta/Module.pm @@ -1,17 +1,22 @@ package Mouse::Meta::Module; -use strict; -use warnings; +use Mouse::Util qw/:meta get_code_package get_code_ref load_class not_supported/; # enables strict and warnings -use Carp (); -use Scalar::Util qw/blessed weaken/; +use Carp (); +use Scalar::Util (); -use Mouse::Util qw/:meta get_code_package not_supported load_class/; +my %METAS; +if(Mouse::Util::MOUSE_XS){ + # register meta storage for performance + Mouse::Util::__register_metaclass_storage(\%METAS, 0); -my %METAS; + # ensure thread safety + *CLONE = sub { Mouse::Util::__register_metaclass_storage(\%METAS, 1) }; +} sub _metaclass_cache { # DEPRECATED my($class, $name) = @_; + Carp::cluck('_metaclass_cache() has been deprecated. Use Mouse::Util::get_metaclass_by_name() instead'); return $METAS{$name}; } @@ -25,7 +30,19 @@ sub initialize { ||= $class->_construct_meta(package => $package_name, @args); } -sub class_of{ +sub reinitialize { + my($class, $package_name, @args) = @_; + + $package_name = $package_name->name if ref $package_name; + + ($package_name && !ref($package_name)) + || $class->throw_error("You must pass a package name and it cannot be blessed"); + + delete $METAS{$package_name}; + return $class->initialize($package_name, @args); +} + +sub _class_of{ my($class_or_instance) = @_; return undef unless defined $class_or_instance; return $METAS{ ref($class_or_instance) || $class_or_instance }; @@ -33,18 +50,18 @@ sub class_of{ # Means of accessing all the metaclasses that have # been initialized thus far -#sub get_all_metaclasses { %METAS } -sub get_all_metaclass_instances { values %METAS } -sub get_all_metaclass_names { keys %METAS } -sub get_metaclass_by_name { $METAS{$_[0]} } -#sub store_metaclass_by_name { $METAS{$_[0]} = $_[1] } -#sub weaken_metaclass { weaken($METAS{$_[0]}) } -#sub does_metaclass_exist { defined $METAS{$_[0]} } -#sub remove_metaclass_by_name { delete $METAS{$_[0]} } - +#sub _get_all_metaclasses { %METAS } +sub _get_all_metaclass_instances { values %METAS } +sub _get_all_metaclass_names { keys %METAS } +sub _get_metaclass_by_name { $METAS{$_[0]} } +#sub _store_metaclass_by_name { $METAS{$_[0]} = $_[1] } +#sub _weaken_metaclass { weaken($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 @@ -62,78 +79,65 @@ sub name { $_[0]->{package} } # add_attribute is an abstract method sub get_attribute_map { # DEPRECATED - Carp::cluck('get_attribute_map() has been deprecated'); + Carp::cluck('get_attribute_map() has been deprecated. Use get_attribute_list() and get_attribute() instead'); return $_[0]->{attributes}; } sub has_attribute { exists $_[0]->{attributes}->{$_[1]} } 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) = @_; - - if(!defined $name){ - $self->throw_error('You must pass a defined name'); - } - if(!defined $code){ - $self->throw_error('You must pass a defined code'); - } - - if(ref($code) ne 'CODE'){ - not_supported 'add_method for a method object'; - } - - $self->{methods}->{$name}++; # Moose stores meta object here. - - my $pkg = $self->name; - no strict 'refs'; - no warnings 'redefine'; - *{ $pkg . '::' . $name } = $code; -} +sub get_attribute_list{ keys %{$_[0]->{attributes}} } # XXX: for backward compatibility my %foreign = map{ $_ => undef } qw( Mouse Mouse::Role Mouse::Util Mouse::Util::TypeConstraints - Carp Scalar::Util + Carp Scalar::Util List::Util ); sub _code_is_mine{ - my($self, $code) = @_; - - my $package = get_code_package($code); +# my($self, $code) = @_; - return !exists $foreign{$package}; + return !exists $foreign{ get_code_package($_[1]) }; } +sub add_method; + 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 defined($self->{methods}{$method_name}) || do{ + my $code = get_code_ref($self->{package}, $method_name); + $code && $self->_code_is_mine($code); + }; +} + +sub get_method_body { + my($self, $method_name) = @_; - my $code = do{ no strict 'refs'; *{$self->{package} . '::' . $method_name}{CODE} }; + defined($method_name) + or $self->throw_error('You must define a method name'); - return $code && $self->_code_is_mine($code); + return $self->{methods}{$method_name} ||= do{ + my $code = get_code_ref($self->{package}, $method_name); + $code && $self->_code_is_mine($code) ? $code : undef; + }; } sub get_method{ my($self, $method_name) = @_; - if($self->has_method($method_name)){ + if(my $code = $self->get_method_body($method_name)){ 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 => $code, + name => $method_name, + package => $self->name, + associated_metaclass => $self, ); } @@ -146,42 +150,74 @@ sub get_method_list { return grep { $self->has_method($_) } keys %{ $self->namespace }; } +sub _collect_methods { # Mouse specific + my($meta, @args) = @_; + + my @methods; + foreach my $arg(@args){ + if(my $type = ref $arg){ + if($type eq 'Regexp'){ + push @methods, grep { $_ =~ $arg } $meta->get_all_method_names; + } + elsif($type eq 'ARRAY'){ + push @methods, @{$arg}; + } + else{ + my $subname = ( caller(1) )[3]; + $meta->throw_error( + sprintf( + 'Methods passed to %s must be provided as a list, ArrayRef or regular expression, not %s', + $subname, + $type, + ) + ); + } + } + else{ + push @methods, $arg; + } + } + return @methods; +} + + { my $ANON_SERIAL = 0; 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(Mouse::Util::is_a_metarole($self)){ 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 +245,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} + Scalar::Util::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}) @@ -260,6 +295,8 @@ sub get_method_list { sub DESTROY{ my($self) = @_; + return if $Mouse::Util::in_global_destruction; + my $serial_id = $self->{anon_serial_id}; return if !$serial_id; @@ -297,13 +334,16 @@ sub throw_error{ } 1; - __END__ =head1 NAME Mouse::Meta::Module - The base class for Mouse::Meta::Class and Mouse::Meta::Role +=head1 VERSION + +This document describes Mouse version 0.50 + =head1 SEE ALSO L