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