Commit | Line | Data |
fb1e11d5 |
1 | package Moose::Meta::Role::Application::ToInstance; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use metaclass; |
6 | |
9b28d20b |
7 | use Scalar::Util 'blessed'; |
fb1e11d5 |
8 | |
b6cca0d5 |
9 | our $VERSION = '1.14'; |
e606ae5f |
10 | $VERSION = eval $VERSION; |
fb1e11d5 |
11 | our $AUTHORITY = 'cpan:STEVAN'; |
12 | |
d5f016af |
13 | use 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 |
20 | sub 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 | |
40 | 1; |
41 | |
42 | __END__ |
43 | |
44 | =pod |
45 | |
46 | =head1 NAME |
47 | |
ab76842e |
48 | Moose::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 |
68 | See L<Moose/BUGS> for details on reporting bugs. |
fb1e11d5 |
69 | |
70 | =head1 AUTHOR |
71 | |
72 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
73 | |
74 | =head1 COPYRIGHT AND LICENSE |
75 | |
7e0492d3 |
76 | Copyright 2006-2010 by Infinity Interactive, Inc. |
fb1e11d5 |
77 | |
78 | L<http://www.iinteractive.com> |
79 | |
80 | This library is free software; you can redistribute it and/or modify |
81 | it under the same terms as Perl itself. |
82 | |
83 | =cut |
84 | |