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