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