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