bump version to 1.09
[gitmo/Class-MOP.git] / lib / metaclass.pm
1
2 package metaclass;
3
4 use strict;
5 use warnings;
6
7 use Carp         'confess';
8 use Scalar::Util 'blessed';
9 use Try::Tiny;
10
11 our $VERSION   = '1.09';
12 $VERSION = eval $VERSION;
13 our $AUTHORITY = 'cpan:STEVAN';
14
15 use Class::MOP;
16
17 sub import {
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 {
28         Class::MOP::load_class($metaclass);
29     }
30
31     ($metaclass->isa('Class::MOP::Class'))
32         || confess "The metaclass ($metaclass) must be derived from Class::MOP::Class";
33
34     # make sure the custom metaclasses get loaded
35     foreach my $key (grep { /_(?:meta)?class$/ } keys %options) {
36         unless ( ref( my $class = $options{$key} ) ) {
37             Class::MOP::load_class($class)
38         }
39     }
40
41     my $package = caller();
42
43     # create a meta object so we can install &meta
44     my $meta = $metaclass->initialize($package => %options);
45     my $should_install = !delete $options{no_meta};
46     $meta->add_method('meta' => sub {
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
51         # big deal anyway.
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         }
60         $metaclass->initialize((blessed($_[0]) || $_[0]) => %options)
61     }) if $should_install;
62 }
63
64 1;
65
66 __END__
67
68 =pod
69
70 =head1 NAME
71
72 metaclass - a pragma for installing and using Class::MOP metaclasses
73
74 =head1 SYNOPSIS
75
76   package MyClass;
77
78   # use Class::MOP::Class
79   use metaclass;
80
81   # ... or use a custom metaclass
82   use metaclass 'MyMetaClass';
83
84   # ... or use a custom metaclass
85   # and custom attribute and method
86   # metaclasses
87   use metaclass 'MyMetaClass' => (
88       'attribute_metaclass' => 'MyAttributeMetaClass',
89       'method_metaclass'    => 'MyMethodMetaClass',
90   );
91
92   # ... or just specify custom attribute
93   # and method classes, and Class::MOP::Class
94   # is the assumed metaclass
95   use metaclass (
96       'attribute_metaclass' => 'MyAttributeMetaClass',
97       'method_metaclass'    => 'MyMethodMetaClass',
98   );
99
100   # if we'd rather not install a 'meta' method, we can do this
101   use metaclass no_meta => 1;
102
103 =head1 DESCRIPTION
104
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
107 installs a C<meta> method to your class as well, if the
108 C<no_meta> option is not specified.
109
110 =head1 AUTHORS
111
112 Stevan Little E<lt>stevan@iinteractive.comE<gt>
113
114 =head1 COPYRIGHT AND LICENSE
115
116 Copyright 2006-2010 by Infinity Interactive, Inc.
117
118 L<http://www.iinteractive.com>
119
120 This library is free software; you can redistribute it and/or modify
121 it under the same terms as Perl itself.
122
123 =cut