clean up metaclass::import
[gitmo/Class-MOP.git] / lib / metaclass.pm
1
2 package metaclass;
3
4 use strict;
5 use warnings;
6
7 use Carp         'confess';
8 use Scalar::Util 'blessed';
9
10 our $VERSION   = '0.65';
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 use Class::MOP;
14
15 sub import {
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 {
26         Class::MOP::load_class($metaclass);
27     }
28
29     ($metaclass->isa('Class::MOP::Class'))
30         || confess "The metaclass ($metaclass) must be derived from Class::MOP::Class";
31
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
39     my $package = caller();
40
41     # create a meta object so we can install &meta
42     my $meta = $metaclass->initialize($package => %options);
43     $meta->add_method('meta' => sub {
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
48         # big deal anyway.
49         $metaclass->initialize((blessed($_[0]) || $_[0]) => %options)
50     });
51 }
52
53 1;
54
55 __END__
56
57 =pod
58
59 =head1 NAME
60
61 metaclass - a pragma for installing and using Class::MOP metaclasses
62
63 =head1 SYNOPSIS
64
65   package MyClass;
66
67   # use Class::MOP::Class
68   use metaclass;
69
70   # ... or use a custom metaclass
71   use metaclass 'MyMetaClass';
72
73   # ... or use a custom metaclass
74   # and custom attribute and method
75   # metaclasses
76   use metaclass 'MyMetaClass' => (
77       'attribute_metaclass' => 'MyAttributeMetaClass',
78       'method_metaclass'    => 'MyMethodMetaClass',
79   );
80
81   # ... or just specify custom attribute
82   # and method classes, and Class::MOP::Class
83   # is the assumed metaclass
84   use metaclass (
85       'attribute_metaclass' => 'MyAttributeMetaClass',
86       'method_metaclass'    => 'MyMethodMetaClass',
87   );
88
89 =head1 DESCRIPTION
90
91 This is a pragma to make it easier to use a specific metaclass
92 and a set of custom attribute and method metaclasses. It also
93 installs a C<meta> method to your class as well.
94
95 =head1 AUTHORS
96
97 Stevan Little E<lt>stevan@iinteractive.comE<gt>
98
99 =head1 COPYRIGHT AND LICENSE
100
101 Copyright 2006-2008 by Infinity Interactive, Inc.
102
103 L<http://www.iinteractive.com>
104
105 This library is free software; you can redistribute it and/or modify
106 it under the same terms as Perl itself.
107
108 =cut