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