From: gfx Date: Sat, 3 Oct 2009 05:32:32 +0000 (+0900) Subject: Refactor create() and DESTORY() in Mouse::Meta::Module X-Git-Tag: 0.37_02~23 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8f40866cc6380d49a06070391bba6abde2eebd2d;p=gitmo%2FMouse.git Refactor create() and DESTORY() in Mouse::Meta::Module --- diff --git a/lib/Mouse/Meta/Module.pm b/lib/Mouse/Meta/Module.pm index 30f7b8a..6079af6 100755 --- a/lib/Mouse/Meta/Module.pm +++ b/lib/Mouse/Meta/Module.pm @@ -150,43 +150,41 @@ sub get_method_list { { my $ANON_SERIAL = 0; - my $ANON_PREFIX = 'Mouse::Meta::Module::__ANON__::'; my %IMMORTALS; sub create { - my ($class, $package_name, %options) = @_; - - $class->throw_error('You must pass a package name') if @_ == 1; + my($class, $package_name, %options) = @_; + $class->throw_error('You must pass a package name') if @_ < 2; + my $superclasses; if(exists $options{superclasses}){ - if($class->isa('Mouse::Meta::Class')){ - (ref $options{superclasses} eq 'ARRAY') - || $class->throw_error("You must pass an ARRAY ref of superclasses"); - } - else{ # role + if($class->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"); + } } - my $attributes; - if(exists $options{attributes}){ - $attributes = delete $options{attributes}; - (ref $attributes eq 'ARRAY' || ref $attributes eq 'HASH') - || $class->throw_error("You must pass an ARRAY ref of attributes") - } - - (ref $options{methods} eq 'HASH') - || $class->throw_error("You must pass a HASH ref of methods") - if exists $options{methods}; - - (ref $options{roles} eq 'ARRAY') - || $class->throw_error("You must pass an ARRAY ref of roles") - if exists $options{roles}; - - - my @extra_options; + 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"); + } + my $methods = delete $options{methods}; + if(defined $methods){ + (ref $methods eq 'HASH') + || $class->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"); + } my $mortal; my $cache_key; @@ -197,14 +195,13 @@ sub get_method_list { if(!$mortal){ # something like Super::Class|Super::Class::2=Role|Role::1 $cache_key = join '=' => ( - join('|', @{$options{superclasses} || []}), - join('|', sort @{$options{roles} || []}), + join('|', @{$superclasses || []}), + join('|', sort @{$roles || []}), ); return $IMMORTALS{$cache_key} if exists $IMMORTALS{$cache_key}; } - $package_name = $ANON_PREFIX . ++$ANON_SERIAL; - - push @extra_options, (anon_serial_id => $ANON_SERIAL); + $options{anon_serial_id} = ++$ANON_SERIAL; + $package_name = $class . '::__ANON__::' . $ANON_SERIAL; } # instantiate a module @@ -214,15 +211,7 @@ sub get_method_list { ${ $package_name . '::AUTHORITY' } = delete $options{authority} if exists $options{authority}; } - my %initialize_options = %options; - delete @initialize_options{qw( - package - superclasses - attributes - methods - roles - )}; - my $meta = $class->initialize( $package_name, %initialize_options, @extra_options); + my $meta = $class->initialize( $package_name, %options); weaken $METAS{$package_name} if $mortal; @@ -232,8 +221,8 @@ sub get_method_list { $class->initialize(ref($_[0]) || $_[0]); }); - $meta->superclasses(@{$options{superclasses}}) - if exists $options{superclasses}; + $meta->superclasses(@{$superclasses}) + if defined $superclasses; # NOTE: # process attributes first, so that they can @@ -242,26 +231,28 @@ sub get_method_list { # I think this should be the order of things. if (defined $attributes) { if(ref($attributes) eq 'ARRAY'){ + # array of Mouse::Meta::Attribute foreach my $attr (@{$attributes}) { - $meta->add_attribute($attr->{name} => $attr); + $meta->add_attribute($attr); } } else{ + # hash map of name and attribute spec pairs while(my($name, $attr) = each %{$attributes}){ $meta->add_attribute($name => $attr); } } } - if (exists $options{methods}) { - foreach my $method_name (keys %{$options{methods}}) { - $meta->add_method($method_name, $options{methods}->{$method_name}); + if (defined $methods) { + while(my($method_name, $method_body) = each %{$methods}){ + $meta->add_method($method_name, $method_body); } } - if (exists $options{roles}){ - Mouse::Util::apply_all_roles($package_name, @{$options{roles}}); + if (defined $roles){ + Mouse::Util::apply_all_roles($package_name, @{$roles}); } - if(!$mortal && exists $meta->{anon_serial_id}){ + if($cache_key){ $IMMORTALS{$cache_key} = $meta; } @@ -275,14 +266,17 @@ sub get_method_list { return if !$serial_id; - my $stash = $self->namespace; + @{$self->{superclasses}} = (); # clear @ISA + %{$self->namespace} = (); # clear the stash - @{$self->{superclasses}} = () if exists $self->{superclasses}; - %{$stash} = (); - delete $METAS{$self->name}; + my $name = $self->name; + delete $METAS{$name}; + + $name =~ s/::\d+$//; no strict 'refs'; - delete ${$ANON_PREFIX}{ $serial_id . '::' }; + + delete ${$name}{ $serial_id . '::' }; return; }