anon-roles are now more efficient about package usage
[gitmo/Moose.git] / lib / Moose / Meta / Role / Application / ToInstance.pm
1 package Moose::Meta::Role::Application::ToInstance;
2
3 use strict;
4 use warnings;
5 use metaclass;
6
7 use Carp            'confess';
8 use Scalar::Util    'blessed';
9
10 use Data::Dumper;
11
12 our $VERSION   = '0.01';
13 our $AUTHORITY = 'cpan:STEVAN';
14
15 use base 'Moose::Meta::Role::Application::ToClass';
16
17 my %ANON_CLASSES;
18
19 sub apply {
20     my ($self, $role, $object) = @_;
21
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);
37 }
38
39 1;
40
41 __END__
42
43 =pod
44
45 =head1 NAME
46
47 Moose::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
59 =item B<apply>
60
61 =back
62
63 =head1 BUGS
64
65 All complex software has bugs lurking in it, and this module is no
66 exception. If you find a bug please either email me, or add the bug
67 to cpan-RT.
68
69 =head1 AUTHOR
70
71 Stevan Little E<lt>stevan@iinteractive.comE<gt>
72
73 =head1 COPYRIGHT AND LICENSE
74
75 Copyright 2006-2008 by Infinity Interactive, Inc.
76
77 L<http://www.iinteractive.com>
78
79 This library is free software; you can redistribute it and/or modify
80 it under the same terms as Perl itself.
81
82 =cut
83