clean up metaclass::import
[gitmo/Class-MOP.git] / lib / metaclass.pm
CommitLineData
677eb158 1
2package metaclass;
3
4use strict;
5use warnings;
6
22286063 7use Carp 'confess';
8use Scalar::Util 'blessed';
677eb158 9
2e5c1a3f 10our $VERSION = '0.65';
f0480c45 11our $AUTHORITY = 'cpan:STEVAN';
677eb158 12
13use Class::MOP;
14
15sub import {
ec21cb13 16 my ( $class, @args ) = @_;
17
18 unshift @args, "metaclass" if @args % 2 == 1;
19 my %options = @args;
20
21 my $metaclass = delete $options{metaclass};
22
23 unless ( defined $metaclass ) {
24 $metaclass = "Class::MOP::Class";
25 } else {
95514cb4 26 Class::MOP::load_class($metaclass);
1becdfcc 27 }
ec21cb13 28
29 ($metaclass->isa('Class::MOP::Class'))
30 || confess "The metaclass ($metaclass) must be derived from Class::MOP::Class";
31
8861fab2 32 # make sure the custom metaclasses get loaded
33 foreach my $class (grep {
34 /^(attribute|method|instance)_metaclass/
35 } keys %options) {
36 Class::MOP::load_class($options{$class})
37 }
38
d82060fe 39 my $package = caller();
95514cb4 40
677eb158 41 # create a meta object so we can install &meta
42 my $meta = $metaclass->initialize($package => %options);
43 $meta->add_method('meta' => sub {
95514cb4 44 # we must re-initialize so that it
45 # works as expected in subclasses,
46 # since metaclass instances are
47 # singletons, this is not really a
677eb158 48 # big deal anyway.
22286063 49 $metaclass->initialize((blessed($_[0]) || $_[0]) => %options)
677eb158 50 });
51}
52
531;
54
55__END__
56
57=pod
58
59=head1 NAME
60
96ceced8 61metaclass - a pragma for installing and using Class::MOP metaclasses
677eb158 62
63=head1 SYNOPSIS
64
550d56db 65 package MyClass;
66
67 # use Class::MOP::Class
95514cb4 68 use metaclass;
550d56db 69
70 # ... or use a custom metaclass
677eb158 71 use metaclass 'MyMetaClass';
95514cb4 72
73 # ... or use a custom metaclass
550d56db 74 # and custom attribute and method
75 # metaclasses
677eb158 76 use metaclass 'MyMetaClass' => (
c23184fc 77 'attribute_metaclass' => 'MyAttributeMetaClass',
95514cb4 78 'method_metaclass' => 'MyMethodMetaClass',
677eb158 79 );
80
1becdfcc 81 # ... or just specify custom attribute
82 # and method classes, and Class::MOP::Class
83 # is the assumed metaclass
84 use metaclass (
c23184fc 85 'attribute_metaclass' => 'MyAttributeMetaClass',
95514cb4 86 'method_metaclass' => 'MyMethodMetaClass',
1becdfcc 87 );
88
677eb158 89=head1 DESCRIPTION
90
95514cb4 91This is a pragma to make it easier to use a specific metaclass
92and a set of custom attribute and method metaclasses. It also
93installs a C<meta> method to your class as well.
c9e77dbb 94
1a09d9cc 95=head1 AUTHORS
677eb158 96
97Stevan Little E<lt>stevan@iinteractive.comE<gt>
98
99=head1 COPYRIGHT AND LICENSE
100
69e3ab0a 101Copyright 2006-2008 by Infinity Interactive, Inc.
677eb158 102
103L<http://www.iinteractive.com>
104
105This library is free software; you can redistribute it and/or modify
95514cb4 106it under the same terms as Perl itself.
677eb158 107
16e960bd 108=cut