bump version to 0.87
[gitmo/Moose.git] / lib / Moose / Meta / Role / Application / ToInstance.pm
1 package Moose::Meta::Role::Application::ToInstance;
2
3 use strict;
4 use warnings;
5 use metaclass;
6
7 use Scalar::Util 'blessed';
8
9 our $VERSION   = '0.87';
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 use base 'Moose::Meta::Role::Application::ToClass';
14
15 __PACKAGE__->meta->add_attribute('rebless_params' => (
16     reader  => 'rebless_params',
17     default => sub { {} }
18 ));
19
20 my %ANON_CLASSES;
21
22 sub apply {
23     my ($self, $role, $object) = @_;
24
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 {
32         my $obj_meta = Class::MOP::class_of($object) || 'Moose::Meta::Class';
33         $class = $obj_meta->create_anon_class(
34             superclasses => [ blessed($object) ]
35         );
36         $ANON_CLASSES{$anon_role_key} = $class;
37         $self->SUPER::apply($role, $class);
38     }
39
40     $class->rebless_instance($object, %{$self->rebless_params});
41 }
42
43 1;
44
45 __END__
46
47 =pod
48
49 =head1 NAME
50
51 Moose::Meta::Role::Application::ToInstance - Compose a role into an instance
52
53 =head1 DESCRIPTION
54
55 =head2 METHODS
56
57 =over 4
58
59 =item B<new>
60
61 =item B<meta>
62
63 =item B<apply>
64
65 =item B<rebless_params>
66
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
81 Copyright 2006-2009 by Infinity Interactive, Inc.
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