docs and changelog
[gitmo/Class-MOP.git] / lib / metaclass.pm
CommitLineData
677eb158 1
2package metaclass;
3
4use strict;
5use warnings;
6
22286063 7use Carp 'confess';
8use Scalar::Util 'blessed';
10c5d753 9use Try::Tiny;
677eb158 10
2440993d 11our $VERSION = '1.08';
d519662a 12$VERSION = eval $VERSION;
f0480c45 13our $AUTHORITY = 'cpan:STEVAN';
677eb158 14
15use Class::MOP;
16
17sub 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
641;
65
66__END__
67
68=pod
69
70=head1 NAME
71
96ceced8 72metaclass - 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 105This is a pragma to make it easier to use a specific metaclass
106and a set of custom attribute and method metaclasses. It also
37f3c875 107installs a C<meta> method to your class as well, if the
108C<no_meta> option is not specified.
c9e77dbb 109
1a09d9cc 110=head1 AUTHORS
677eb158 111
112Stevan Little E<lt>stevan@iinteractive.comE<gt>
113
114=head1 COPYRIGHT AND LICENSE
115
3e2c8600 116Copyright 2006-2010 by Infinity Interactive, Inc.
677eb158 117
118L<http://www.iinteractive.com>
119
120This library is free software; you can redistribute it and/or modify
95514cb4 121it under the same terms as Perl itself.
677eb158 122
16e960bd 123=cut