obscure enough feature that there are not too
many work arounds out in the wild.
- added tests for this by groditi
- - updated docs to explain this
+ - updated docs to explain this
+
+ * metaclass
+ - load custom metaclass automatically.
+ - modified tests to test this behavior
0.37 Sat. March 10, 2007
~~ Many, many documentation updates ~~
}
else {
$metaclass = shift;
+ Class::MOP::load_class($metaclass);
($metaclass->isa('Class::MOP::Class'))
|| confess "The metaclass ($metaclass) must be derived from Class::MOP::Class";
}
my %options = @_;
my $package = caller();
-
+
# create a meta object so we can install &meta
my $meta = $metaclass->initialize($package => %options);
$meta->add_method('meta' => sub {
- # we must re-initialize so that it
- # works as expected in subclasses,
- # since metaclass instances are
- # singletons, this is not really a
+ # we must re-initialize so that it
+ # works as expected in subclasses,
+ # since metaclass instances are
+ # singletons, this is not really a
# big deal anyway.
$metaclass->initialize((blessed($_[0]) || $_[0]) => %options)
});
package MyClass;
# use Class::MOP::Class
- use metaclass;
+ use metaclass;
# ... or use a custom metaclass
use metaclass 'MyMetaClass';
-
- # ... or use a custom metaclass
+
+ # ... or use a custom metaclass
# and custom attribute and method
# metaclasses
use metaclass 'MyMetaClass' => (
'attribute_metaclass' => 'MyAttributeMetaClass',
- 'method_metaclass' => 'MyMethodMetaClass',
+ 'method_metaclass' => 'MyMethodMetaClass',
);
# ... or just specify custom attribute
# is the assumed metaclass
use metaclass (
'attribute_metaclass' => 'MyAttributeMetaClass',
- 'method_metaclass' => 'MyMethodMetaClass',
+ 'method_metaclass' => 'MyMethodMetaClass',
);
=head1 DESCRIPTION
-This is a pragma to make it easier to use a specific metaclass
-and a set of custom attribute and method metaclasses. It also
-installs a C<meta> method to your class as well.
+This is a pragma to make it easier to use a specific metaclass
+and a set of custom attribute and method metaclasses. It also
+installs a C<meta> method to your class as well.
=head1 AUTHORS
L<http://www.iinteractive.com>
This library is free software; you can redistribute it and/or modify
-it under the same terms as Perl itself.
+it under the same terms as Perl itself.
=cut
use strict;
use warnings;
+use FindBin;
+use File::Spec::Functions;
+
use Test::More tests => 36;
use Test::Exception;
use_ok('Class::MOP');
}
+use lib catdir($FindBin::Bin, 'lib');
+
# make sure the Class::MOP::Class->meta does the right thing
my $meta = Class::MOP::Class->meta();
my $cloned_meta = $meta->clone_object($meta);
isa_ok($cloned_meta, 'Class::MOP::Class');
-is($cloned_meta, $meta, '... it creates the singleton even if you try to clone it');
+is($cloned_meta, $meta, '... it creates the singleton even if you try to clone it');
# make sure other metaclasses do the right thing
is($meta->new_object('package' => 'Foo'), $foo_meta, '... got the right Foo->meta singleton');
is($meta->clone_object($foo_meta), $foo_meta, '... cloning got the right Foo->meta singleton');
-
-# make sure subclassed of Class::MOP::Class do the right thing
-{
- package MyMetaClass;
- use base 'Class::MOP::Class';
-}
+# make sure subclassed of Class::MOP::Class do the right thing
my $my_meta = MyMetaClass->meta;
isa_ok($my_meta, 'Class::MOP::Class');
$bar_meta->superclasses('Foo');
-# check with MyMetaClass
+# check with MyMetaClass
{
package Baz;
dies_ok {
$foo_meta->clone_object($meta);
-} '... this dies as expected';
+} '... this dies as expected';
# test stuff
{
package FooBar;
use metaclass;
-
+
FooBar->meta->add_attribute('test');
}
isa_ok($attr_clone, 'Class::MOP::Attribute');
isnt($attr, $attr_clone, '... we successfully cloned our attributes');
-is($attr->associated_class,
- $attr_clone->associated_class,
+is($attr->associated_class,
+ $attr_clone->associated_class,
'... we successfully did not clone our associated metaclass');
--- /dev/null
+
+package MyMetaClass;
+
+use strict;
+use warnings;
+
+use base 'Class::MOP::Class';
+
+1;