From: Tokuhiro Matsuno Date: Wed, 10 Jun 2009 04:57:29 +0000 (+0900) Subject: added Mouse->init_meta X-Git-Tag: 0.24~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0d6e12bec0f93cd1e6e0f58bd00fbad9447dd325;p=gitmo%2FMouse.git added Mouse->init_meta --- diff --git a/lib/Mouse.pm b/lib/Mouse.pm index 7cdd243..9cca1dc 100644 --- a/lib/Mouse.pm +++ b/lib/Mouse.pm @@ -88,6 +88,46 @@ sub override { }); } +sub init_meta { + # This used to be called as a function. This hack preserves + # backwards compatibility. + if ( $_[0] ne __PACKAGE__ ) { + return __PACKAGE__->init_meta( + for_class => $_[0], + base_class => $_[1], + metaclass => $_[2], + ); + } + + shift; + my %args = @_; + + my $class = $args{for_class} + or Carp::croak( + "Cannot call init_meta without specifying a for_class"); + my $base_class = $args{base_class} || 'Mouse::Object'; + my $metaclass = $args{metaclass} || 'Mouse::Meta::Class'; + + Carp::croak("The Metaclass $metaclass must be a subclass of Mouse::Meta::Class.") + unless $metaclass->isa('Mouse::Meta::Class'); + + # make a subtype for each Mouse class + class_type($class) + unless find_type_constraint($class); + + my $meta = $metaclass->initialize($class); + $meta->superclasses($base_class) + unless $meta->superclasses; + + { + no strict 'refs'; + no warnings 'redefine'; + *{$class.'::meta'} = sub { $meta }; + } + + return $meta; +} + sub import { my $class = shift; @@ -111,16 +151,9 @@ sub import { return; } - my $meta = Mouse::Meta::Class->initialize($caller); - $meta->superclasses('Mouse::Object') - unless $meta->superclasses; - - # make a subtype for each Mouse class - class_type($caller) unless find_type_constraint($caller); - - no strict 'refs'; - no warnings 'redefine'; - *{$caller.'::meta'} = sub { $meta }; + Mouse->init_meta( + for_class => $caller, + ); if (@_) { __PACKAGE__->export_to_level( $level+1, $class, @_); diff --git a/t/050_metaclasses/011_init_meta.t b/t/050_metaclasses/011_init_meta.t new file mode 100644 index 0000000..9c47c78 --- /dev/null +++ b/t/050_metaclasses/011_init_meta.t @@ -0,0 +1,19 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 4; + +use Mouse (); + + +{ package Foo; } + +my $meta = Mouse::init_meta('Foo'); + +ok( Foo->isa('Mouse::Object'), '... Foo isa Mouse::Object'); +isa_ok( $meta, 'Mouse::Meta::Class' ); +isa_ok( Foo->meta, 'Mouse::Meta::Class' ); + +is($meta, Foo->meta, '... our metas are the same');