From: gfx Date: Thu, 17 Sep 2009 01:05:26 +0000 (+0900) Subject: Fix meta() and initialize() for more compatibility X-Git-Tag: 0.32~52 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=88ed718958233d513d2a64db12c69ec2e2653e0c;p=gitmo%2FMouse.git Fix meta() and initialize() for more compatibility --- diff --git a/lib/Mouse.pm b/lib/Mouse.pm index 03aa91f..a78d85b 100644 --- a/lib/Mouse.pm +++ b/lib/Mouse.pm @@ -122,7 +122,9 @@ sub init_meta { { no strict 'refs'; no warnings 'redefine'; - *{$class.'::meta'} = sub { $meta }; + *{$class.'::meta'} = sub { + return Mouse::Meta::Class->initialize(ref($_[0]) || $_[0]); + }; } return $meta; diff --git a/lib/Mouse/Meta/Class.pm b/lib/Mouse/Meta/Class.pm index 4761e5d..7ba5692 100644 --- a/lib/Mouse/Meta/Class.pm +++ b/lib/Mouse/Meta/Class.pm @@ -20,12 +20,13 @@ do { } sub initialize { - my $class = blessed($_[0]) || $_[0]; - my $name = $_[1]; + my($class, $package_name, @args) = @_; - $METACLASS_CACHE{$name} = $class->new(name => $name) - if !exists($METACLASS_CACHE{$name}); - return $METACLASS_CACHE{$name}; + ($package_name && !ref($package_name)) + || confess("You must pass a package name and it cannot be blessed"); + + return $METACLASS_CACHE{$package_name} + ||= $class->_construct_class_instance(package => $package_name, @args); } # Means of accessing all the metaclasses that have @@ -40,21 +41,20 @@ do { sub remove_metaclass_by_name { $METACLASS_CACHE{$_[0]} = undef } }; -sub new { - my $class = shift; - my %args = @_; +sub _construct_class_instance { + my($class, %args) = @_; - $args{attributes} = {}; + $args{attributes} = {}; $args{superclasses} = do { no strict 'refs'; - \@{ $args{name} . '::ISA' }; + \@{ $args{package} . '::ISA' }; }; $args{roles} ||= []; bless \%args, $class; } -sub name { $_[0]->{name} } +sub name { $_[0]->{package} } sub superclasses { my $self = shift; @@ -319,7 +319,7 @@ sub does_role { } sub create { - my ($self, $package_name, %options) = @_; + my ($class, $package_name, %options) = @_; (ref $options{superclasses} eq 'ARRAY') || confess "You must pass an ARRAY ref of superclasses" @@ -356,11 +356,11 @@ sub create { version authority )}; - my $meta = $self->initialize( $package_name => %initialize_options ); + my $meta = $class->initialize( $package_name => %initialize_options ); # FIXME totally lame $meta->add_method('meta' => sub { - $self->initialize(ref($_[0]) || $_[0]); + Mouse::Meta::Class->initialize(ref($_[0]) || $_[0]); }); $meta->superclasses(@{$options{superclasses}}) diff --git a/lib/Mouse/Object.pm b/lib/Mouse/Object.pm index ddcf41f..745d1f2 100644 --- a/lib/Mouse/Object.pm +++ b/lib/Mouse/Object.pm @@ -136,7 +136,7 @@ sub does { || confess "You must supply a role name to does()"; my $meta = $self->meta; foreach my $class ($meta->linearized_isa) { - my $m = $meta->initialize($class); + my $m = ref($meta)->initialize($class); return 1 if $m->can('does_role') && $m->does_role($role_name); }