1 package Moose::Meta::Role::Application::ToInstance;
7 use Scalar::Util 'blessed';
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Moose::Meta::Role::Application::ToClass';
15 __PACKAGE__->meta->add_attribute('rebless_params' => (
16 reader => 'rebless_params',
23 my ($self, $role, $object) = @_;
25 my $anon_role_key = (blessed($object) . $role->name);
28 if (exists $ANON_CLASSES{$anon_role_key} && defined $ANON_CLASSES{$anon_role_key}) {
29 $class = $ANON_CLASSES{$anon_role_key};
32 my $obj_meta = Class::MOP::class_of($object) || 'Moose::Meta::Class';
34 # This is a special case to handle the case where the object's
35 # metaclass is a Class::MOP::Class, but _not_ a Moose::Meta::Class
36 # (for example, when applying a role to a Moose::Meta::Attribute
38 $obj_meta = 'Moose::Meta::Class'
39 unless $obj_meta->isa('Moose::Meta::Class');
41 $class = $obj_meta->create_anon_class(
42 superclasses => [ blessed($object) ]
44 $ANON_CLASSES{$anon_role_key} = $class;
45 $self->SUPER::apply($role, $class);
48 $class->rebless_instance($object, %{$self->rebless_params});
59 Moose::Meta::Role::Application::ToInstance - Compose a role into an instance
73 =item B<rebless_params>
79 See L<Moose/BUGS> for details on reporting bugs.
83 Stevan Little E<lt>stevan@iinteractive.comE<gt>
85 =head1 COPYRIGHT AND LICENSE
87 Copyright 2006-2010 by Infinity Interactive, Inc.
89 L<http://www.iinteractive.com>
91 This library is free software; you can redistribute it and/or modify
92 it under the same terms as Perl itself.