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