version bump
[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
72643035 9our $VERSION = '1.08';
e606ae5f 10$VERSION = eval $VERSION;
fb1e11d5 11our $AUTHORITY = 'cpan:STEVAN';
12
d5f016af 13use base 'Moose::Meta::Role::Application';
1c9db35c 14
3a79f0e9 15__PACKAGE__->meta->add_attribute('rebless_params' => (
16 reader => 'rebless_params',
17 default => sub { {} }
18));
19
1c9db35c 20sub apply {
f315aab3 21 my ( $self, $role, $object, $args ) = @_;
53b4c697 22
23 my $obj_meta = Class::MOP::class_of($object) || 'Moose::Meta::Class';
24
25 # This is a special case to handle the case where the object's metaclass
26 # is a Class::MOP::Class, but _not_ a Moose::Meta::Class (for example,
27 # when applying a role to a Moose::Meta::Attribute object).
28 $obj_meta = 'Moose::Meta::Class'
29 unless $obj_meta->isa('Moose::Meta::Class');
30
31 my $class = $obj_meta->create_anon_class(
a687ab44 32 superclasses => [ blessed($object) ],
f315aab3 33 roles => [ $role, keys(%$args) ? ($args) : () ],
a687ab44 34 cache => 1,
53b4c697 35 );
36
53b4c697 37 $class->rebless_instance( $object, %{ $self->rebless_params } );
1c9db35c 38}
fb1e11d5 39
401;
41
42__END__
43
44=pod
45
46=head1 NAME
47
ab76842e 48Moose::Meta::Role::Application::ToInstance - Compose a role into an instance
fb1e11d5 49
50=head1 DESCRIPTION
51
52=head2 METHODS
53
54=over 4
55
56=item B<new>
57
58=item B<meta>
59
1c9db35c 60=item B<apply>
61
3a79f0e9 62=item B<rebless_params>
63
fb1e11d5 64=back
65
66=head1 BUGS
67
d4048ef3 68See L<Moose/BUGS> for details on reporting bugs.
fb1e11d5 69
70=head1 AUTHOR
71
72Stevan Little E<lt>stevan@iinteractive.comE<gt>
73
74=head1 COPYRIGHT AND LICENSE
75
7e0492d3 76Copyright 2006-2010 by Infinity Interactive, Inc.
fb1e11d5 77
78L<http://www.iinteractive.com>
79
80This library is free software; you can redistribute it and/or modify
81it under the same terms as Perl itself.
82
83=cut
84