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