Beginning of dzilization
[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
fb1e11d5 9our $AUTHORITY = 'cpan:STEVAN';
10
d5f016af 11use base 'Moose::Meta::Role::Application';
1c9db35c 12
3a79f0e9 13__PACKAGE__->meta->add_attribute('rebless_params' => (
14 reader => 'rebless_params',
15 default => sub { {} }
16));
17
1c9db35c 18sub apply {
f315aab3 19 my ( $self, $role, $object, $args ) = @_;
53b4c697 20
21 my $obj_meta = Class::MOP::class_of($object) || 'Moose::Meta::Class';
22
23 # This is a special case to handle the case where the object's metaclass
24 # is a Class::MOP::Class, but _not_ a Moose::Meta::Class (for example,
25 # when applying a role to a Moose::Meta::Attribute object).
26 $obj_meta = 'Moose::Meta::Class'
27 unless $obj_meta->isa('Moose::Meta::Class');
28
29 my $class = $obj_meta->create_anon_class(
a687ab44 30 superclasses => [ blessed($object) ],
f315aab3 31 roles => [ $role, keys(%$args) ? ($args) : () ],
a687ab44 32 cache => 1,
53b4c697 33 );
34
53b4c697 35 $class->rebless_instance( $object, %{ $self->rebless_params } );
1c9db35c 36}
fb1e11d5 37
381;
39
ad46f524 40# ABSTRACT: Compose a role into an instance
41
fb1e11d5 42__END__
43
44=pod
45
fb1e11d5 46=head1 DESCRIPTION
47
48=head2 METHODS
49
50=over 4
51
52=item B<new>
53
54=item B<meta>
55
1c9db35c 56=item B<apply>
57
3a79f0e9 58=item B<rebless_params>
59
fb1e11d5 60=back
61
62=head1 BUGS
63
d4048ef3 64See L<Moose/BUGS> for details on reporting bugs.
fb1e11d5 65
fb1e11d5 66=cut
67