Checking in changes prior to tagging of version 0.37_05. Changelog diff is:
[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{
112 my($class, %args) = @_;
7daedfff 113
fea36fd2 114 my $for_class = $args{for_class}
115 or Carp::confess("Cannot call init_meta without specifying a for_class");
b32e8fb9 116
fea36fd2 117 my $metaclass = $args{metaclass} || 'Mouse::Meta::Role';
7daedfff 118
fea36fd2 119 my $meta = $metaclass->initialize($for_class);
7daedfff 120
fea36fd2 121 $meta->add_method(meta => sub{
122 $metaclass->initialize(ref($_[0]) || $_[0]);
3a63a2e7 123 });
b32e8fb9 124
fea36fd2 125 return $meta;
b32e8fb9 126}
f9e68395 127
1281;
129
cadd5b5e 130__END__
131
132=head1 NAME
133
1820fffe 134Mouse::Role - The Mouse Role
135
136=head1 SYNOPSIS
137
138 package MyRole;
139 use Mouse::Role;
cadd5b5e 140
141=head1 KEYWORDS
142
1820fffe 143=head2 C<< meta -> Mouse::Meta::Role >>
cadd5b5e 144
145Returns this role's metaclass instance.
146
1820fffe 147=head2 C<< before (method|methods) -> CodeRef >>
cadd5b5e 148
1820fffe 149Sets up a B<before> method modifier. See L<Moose/before> or
cadd5b5e 150L<Class::Method::Modifiers/before>.
151
1820fffe 152=head2 C<< after (method|methods) => CodeRef >>
cadd5b5e 153
1820fffe 154Sets up an B<after> method modifier. See L<Moose/after> or
cadd5b5e 155L<Class::Method::Modifiers/after>.
156
1820fffe 157=head2 C<< around (method|methods) => CodeRef >>
cadd5b5e 158
1820fffe 159Sets up an B<around> method modifier. See L<Moose/around> or
cadd5b5e 160L<Class::Method::Modifiers/around>.
161
1820fffe 162=head2 C<super>
67199842 163
1820fffe 164Sets up the B<super> keyword. See L<Moose/super>.
67199842 165
1820fffe 166=head2 C<< override method => CodeRef >>
67199842 167
1820fffe 168Sets up an B<override> method modifier. See L<Moose/Role/override>.
67199842 169
1820fffe 170=head2 C<inner>
67199842 171
1820fffe 172This is not supported in roles and emits an error. See L<Moose/Role>.
67199842 173
1820fffe 174=head2 C<< augment method => CodeRef >>
67199842 175
1820fffe 176This is not supported in roles and emits an error. See L<Moose/Role>.
67199842 177
1820fffe 178=head2 C<< has (name|names) => parameters >>
cadd5b5e 179
180Sets up an attribute (or if passed an arrayref of names, multiple attributes) to
181this role. See L<Mouse/has>.
182
1820fffe 183=head2 C<< confess(error) -> BOOM >>
cadd5b5e 184
185L<Carp/confess> for your convenience.
186
1820fffe 187=head2 C<< blessed(value) -> ClassName | undef >>
cadd5b5e 188
189L<Scalar::Util/blessed> for your convenience.
190
191=head1 MISC
192
193=head2 import
194
195Importing Mouse::Role will give you sugar.
196
197=head2 unimport
198
1820fffe 199Please unimport (C<< no Mouse::Role >>) so that if someone calls one of the
cadd5b5e 200keywords (such as L</has>) it will break loudly instead breaking subtly.
201
1820fffe 202=head1 SEE ALSO
203
204L<Moose::Role>
205
cadd5b5e 206=cut
207