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