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