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