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