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