281e9dc2f38723e2d887ec4c82eba362d7d643ce
[gitmo/Moose.git] / lib / Moose / Meta / Role / Composite.pm
1 package Moose::Meta::Role::Composite;
2
3 use strict;
4 use warnings;
5 use metaclass;
6
7 use Carp         'confess';
8 use Scalar::Util 'blessed', 'reftype';
9
10 our $VERSION   = '0.01';
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 use base 'Moose::Meta::Role';
14
15 # NOTE:
16 # we need to override the ->name 
17 # method from Class::MOP::Package
18 # since we don't have an actual 
19 # package for this.
20 # - SL
21 __PACKAGE__->meta->add_attribute('name' => (reader => 'name'));
22
23 # NOTE:
24 # Again, since we don't have a real 
25 # package to store our methods in, 
26 # we use a HASH ref instead. 
27 # - SL
28 __PACKAGE__->meta->add_attribute('methods' => (
29     reader  => 'get_method_map',
30     default => sub { {} }
31 ));
32
33 sub new {
34     my ($class, %params) = @_;
35     # the roles param is required ...
36     ($_->isa('Moose::Meta::Role'))
37         || confess "The list of roles must be instances of Moose::Meta::Role, not $_"
38             foreach @{$params{roles}};
39     # and the name is created from the
40     # roles if one has not been provided
41     $params{name} ||= (join "|" => map { $_->name } @{$params{roles}});
42     $class->meta->new_object(%params);
43 }
44
45 # NOTE:
46 # we need to override this cause 
47 # we dont have that package I was
48 # talking about above.
49 # - SL
50 sub alias_method {
51     my ($self, $method_name, $method) = @_;
52     (defined $method_name && $method_name)
53         || confess "You must define a method name";
54
55     # make sure to bless the 
56     # method if nessecary 
57     $method = $self->method_metaclass->wrap($method) 
58         if !blessed($method);
59
60     $self->get_method_map->{$method_name} = $method;
61 }
62
63 1;
64
65 __END__
66
67 =pod
68
69 =head1 NAME
70
71 Moose::Meta::Role::Composite - An object to represent the set of roles
72
73 =head1 DESCRIPTION
74
75 =head2 METHODS
76
77 =over 4
78
79 =item B<new>
80
81 =item B<meta>
82
83 =item B<name>
84
85 =item B<get_method_map>
86
87 =item B<alias_method>
88
89 =back
90
91 =head1 BUGS
92
93 All complex software has bugs lurking in it, and this module is no
94 exception. If you find a bug please either email me, or add the bug
95 to cpan-RT.
96
97 =head1 AUTHOR
98
99 Stevan Little E<lt>stevan@iinteractive.comE<gt>
100
101 =head1 COPYRIGHT AND LICENSE
102
103 Copyright 2006-2008 by Infinity Interactive, Inc.
104
105 L<http://www.iinteractive.com>
106
107 This library is free software; you can redistribute it and/or modify
108 it under the same terms as Perl itself.
109
110 =cut