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