Update Makefile.PL
[gitmo/Mouse.git] / lib / Mouse / Role.pm
CommitLineData
f9e68395 1package Mouse::Role;
bc69ee88 2use Mouse::Util qw(not_supported); # enables strict and warnings
f3bb863f 3
43523060 4use Carp qw(confess);
5use Scalar::Util qw(blessed);
f9e68395 6
6d28c5cf 7use Mouse ();
fea36fd2 8use Mouse::Exporter;
9
10Mouse::Exporter->setup_import_methods(
11 as_is => [qw(
12 extends with
13 has
14 before after around
15 override super
16 augment inner
17
18 requires excludes
19 ),
20 \&Scalar::Util::blessed,
21 \&Carp::confess,
22 ],
2cb8b713 23);
24
43523060 25# XXX: for backward compatibility
26our @EXPORT = qw(
27 extends with
28 has
29 before after around
30 override super
31 augment inner
32
33 requires excludes
34
35 blessed confess
36);
b32e8fb9 37
38sub before {
8bc2760b 39 my $meta = Mouse::Meta::Role->initialize(scalar caller);
b32e8fb9 40
41 my $code = pop;
42 for (@_) {
43 $meta->add_before_method_modifier($_ => $code);
44 }
45}
46
47sub after {
8bc2760b 48 my $meta = Mouse::Meta::Role->initialize(scalar caller);
b32e8fb9 49
50 my $code = pop;
51 for (@_) {
52 $meta->add_after_method_modifier($_ => $code);
f9e68395 53 }
b32e8fb9 54}
55
56sub around {
8bc2760b 57 my $meta = Mouse::Meta::Role->initialize(scalar caller);
b32e8fb9 58
59 my $code = pop;
60 for (@_) {
61 $meta->add_around_method_modifier($_ => $code);
62 }
63}
64
67199842 65
66sub super {
85bd3f44 67 return if !defined $Mouse::SUPER_BODY;
67199842 68 $Mouse::SUPER_BODY->(@Mouse::SUPER_ARGS);
69}
70
71sub override {
85bd3f44 72 # my($name, $code) = @_;
73 Mouse::Meta::Role->initialize(scalar caller)->add_override_method_modifier(@_);
67199842 74}
75
76# We keep the same errors messages as Moose::Role emits, here.
77sub inner {
6d28c5cf 78 Carp::croak "Roles cannot support 'inner'";
67199842 79}
80
81sub augment {
6d28c5cf 82 Carp::croak "Roles cannot support 'augment'";
67199842 83}
84
b32e8fb9 85sub has {
8bc2760b 86 my $meta = Mouse::Meta::Role->initialize(scalar caller);
b32e8fb9 87 my $name = shift;
b32e8fb9 88
1b9e472d 89 $meta->add_attribute($_ => @_) for ref($name) ? @{$name} : $name;
b32e8fb9 90}
91
6d28c5cf 92sub extends {
93 Carp::croak "Roles do not support 'extends'"
94}
b32e8fb9 95
b1b81553 96sub with {
8bc2760b 97 my $meta = Mouse::Meta::Role->initialize(scalar caller);
ff687069 98 Mouse::Util::apply_all_roles($meta->name, @_);
b1b81553 99}
b32e8fb9 100
59089ec3 101sub requires {
8bc2760b 102 my $meta = Mouse::Meta::Role->initialize(scalar caller);
6d28c5cf 103 $meta->throw_error("Must specify at least one method") unless @_;
59089ec3 104 $meta->add_required_methods(@_);
105}
b32e8fb9 106
6d28c5cf 107sub excludes {
108 not_supported;
109}
b32e8fb9 110
fea36fd2 111sub init_meta{
9704fe9d 112 shift;
113 my %args = @_;
7daedfff 114
9704fe9d 115 my $class = $args{for_class}
fea36fd2 116 or Carp::confess("Cannot call init_meta without specifying a for_class");
b32e8fb9 117
fea36fd2 118 my $metaclass = $args{metaclass} || 'Mouse::Meta::Role';
7daedfff 119
9704fe9d 120 my $meta = $metaclass->initialize($class);
7daedfff 121
fea36fd2 122 $meta->add_method(meta => sub{
123 $metaclass->initialize(ref($_[0]) || $_[0]);
3a63a2e7 124 });
b32e8fb9 125
9704fe9d 126 # make a role type for each Mouse role
127 Mouse::Util::TypeConstraints::role_type($class)
128 unless Mouse::Util::TypeConstraints::find_type_constraint($class);
129
fea36fd2 130 return $meta;
b32e8fb9 131}
f9e68395 132
1331;
134
cadd5b5e 135__END__
136
137=head1 NAME
138
1820fffe 139Mouse::Role - The Mouse Role
140
a25ca8d6 141=head1 VERSION
142
143This document describes Mouse version 0.37_06
144
1820fffe 145=head1 SYNOPSIS
146
147 package MyRole;
148 use Mouse::Role;
cadd5b5e 149
150=head1 KEYWORDS
151
1820fffe 152=head2 C<< meta -> Mouse::Meta::Role >>
cadd5b5e 153
154Returns this role's metaclass instance.
155
1820fffe 156=head2 C<< before (method|methods) -> CodeRef >>
cadd5b5e 157
1820fffe 158Sets up a B<before> method modifier. See L<Moose/before> or
cadd5b5e 159L<Class::Method::Modifiers/before>.
160
1820fffe 161=head2 C<< after (method|methods) => CodeRef >>
cadd5b5e 162
1820fffe 163Sets up an B<after> method modifier. See L<Moose/after> or
cadd5b5e 164L<Class::Method::Modifiers/after>.
165
1820fffe 166=head2 C<< around (method|methods) => CodeRef >>
cadd5b5e 167
1820fffe 168Sets up an B<around> method modifier. See L<Moose/around> or
cadd5b5e 169L<Class::Method::Modifiers/around>.
170
1820fffe 171=head2 C<super>
67199842 172
1820fffe 173Sets up the B<super> keyword. See L<Moose/super>.
67199842 174
1820fffe 175=head2 C<< override method => CodeRef >>
67199842 176
1820fffe 177Sets up an B<override> method modifier. See L<Moose/Role/override>.
67199842 178
1820fffe 179=head2 C<inner>
67199842 180
1820fffe 181This is not supported in roles and emits an error. See L<Moose/Role>.
67199842 182
1820fffe 183=head2 C<< augment method => CodeRef >>
67199842 184
1820fffe 185This is not supported in roles and emits an error. See L<Moose/Role>.
67199842 186
1820fffe 187=head2 C<< has (name|names) => parameters >>
cadd5b5e 188
189Sets up an attribute (or if passed an arrayref of names, multiple attributes) to
190this role. See L<Mouse/has>.
191
1820fffe 192=head2 C<< confess(error) -> BOOM >>
cadd5b5e 193
194L<Carp/confess> for your convenience.
195
1820fffe 196=head2 C<< blessed(value) -> ClassName | undef >>
cadd5b5e 197
198L<Scalar::Util/blessed> for your convenience.
199
200=head1 MISC
201
202=head2 import
203
204Importing Mouse::Role will give you sugar.
205
206=head2 unimport
207
1820fffe 208Please unimport (C<< no Mouse::Role >>) so that if someone calls one of the
cadd5b5e 209keywords (such as L</has>) it will break loudly instead breaking subtly.
210
1820fffe 211=head1 SEE ALSO
212
213L<Moose::Role>
214
cadd5b5e 215=cut
216