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