anon-roles are now more efficient about package usage
[gitmo/Moose.git] / lib / Moose / Meta / Role / Application / ToInstance.pm
CommitLineData
fb1e11d5 1package Moose::Meta::Role::Application::ToInstance;
2
3use strict;
4use warnings;
5use metaclass;
6
7use Carp 'confess';
8use Scalar::Util 'blessed';
9
10use Data::Dumper;
11
12our $VERSION = '0.01';
13our $AUTHORITY = 'cpan:STEVAN';
14
1c9db35c 15use base 'Moose::Meta::Role::Application::ToClass';
16
d71ba374 17my %ANON_CLASSES;
1c9db35c 18
19sub apply {
20 my ($self, $role, $object) = @_;
21
d71ba374 22 my $anon_role_key = (blessed($object) . $role->name);
23
24 my $class;
25 if (exists $ANON_CLASSES{$anon_role_key} && defined $ANON_CLASSES{$anon_role_key}) {
26 $class = $ANON_CLASSES{$anon_role_key};
27 }
28 else {
29 $class = Moose::Meta::Class->create_anon_class(
30 superclasses => [ blessed($object) ]
31 );
32 $ANON_CLASSES{$anon_role_key} = $class;
33 $self->SUPER::apply($role, $class);
34 }
35
36 $class->rebless_instance($object);
1c9db35c 37}
fb1e11d5 38
391;
40
41__END__
42
43=pod
44
45=head1 NAME
46
47Moose::Meta::Role::Application::ToInstance
48
49=head1 DESCRIPTION
50
51=head2 METHODS
52
53=over 4
54
55=item B<new>
56
57=item B<meta>
58
1c9db35c 59=item B<apply>
60
fb1e11d5 61=back
62
63=head1 BUGS
64
65All complex software has bugs lurking in it, and this module is no
66exception. If you find a bug please either email me, or add the bug
67to cpan-RT.
68
69=head1 AUTHOR
70
71Stevan Little E<lt>stevan@iinteractive.comE<gt>
72
73=head1 COPYRIGHT AND LICENSE
74
778db3ac 75Copyright 2006-2008 by Infinity Interactive, Inc.
fb1e11d5 76
77L<http://www.iinteractive.com>
78
79This library is free software; you can redistribute it and/or modify
80it under the same terms as Perl itself.
81
82=cut
83