Commit | Line | Data |
306290e8 |
1 | package Mouse::Meta::Attribute; |
c3398f5b |
2 | use strict; |
3 | use warnings; |
3b8fe109 |
4 | require overload; |
c3398f5b |
5 | |
6 | use Carp 'confess'; |
6c169c50 |
7 | use Scalar::Util (); |
684db121 |
8 | use Mouse::Meta::TypeConstraint; |
c3398f5b |
9 | |
10 | sub new { |
2608b115 |
11 | my ($class, $name, %options) = @_; |
c3398f5b |
12 | |
2608b115 |
13 | $options{name} = $name; |
2e7e86c6 |
14 | |
2608b115 |
15 | $options{init_arg} = $name |
16 | unless exists $options{init_arg}; |
45959ffa |
17 | |
2608b115 |
18 | $options{is} ||= ''; |
c3398f5b |
19 | |
2608b115 |
20 | bless \%options, $class; |
c3398f5b |
21 | } |
22 | |
f6715552 |
23 | sub name { $_[0]->{name} } |
24 | sub associated_class { $_[0]->{associated_class} } |
25 | sub _is_metadata { $_[0]->{is} } |
26 | sub is_required { $_[0]->{required} } |
27 | sub default { $_[0]->{default} } |
28 | sub is_lazy { $_[0]->{lazy} } |
29 | sub is_lazy_build { $_[0]->{lazy_build} } |
30 | sub predicate { $_[0]->{predicate} } |
31 | sub clearer { $_[0]->{clearer} } |
32 | sub handles { $_[0]->{handles} } |
33 | sub is_weak_ref { $_[0]->{weak_ref} } |
34 | sub init_arg { $_[0]->{init_arg} } |
35 | sub type_constraint { $_[0]->{type_constraint} } |
36 | sub trigger { $_[0]->{trigger} } |
37 | sub builder { $_[0]->{builder} } |
38 | sub should_auto_deref { $_[0]->{auto_deref} } |
39 | sub should_coerce { $_[0]->{should_coerce} } |
c3398f5b |
40 | |
f6715552 |
41 | sub has_default { exists $_[0]->{default} } |
42 | sub has_predicate { exists $_[0]->{predicate} } |
43 | sub has_clearer { exists $_[0]->{clearer} } |
44 | sub has_handles { exists $_[0]->{handles} } |
45 | sub has_type_constraint { exists $_[0]->{type_constraint} } |
46 | sub has_trigger { exists $_[0]->{trigger} } |
47 | sub has_builder { exists $_[0]->{builder} } |
eec1bb49 |
48 | |
1bfebf5f |
49 | sub _create_args { |
50 | $_[0]->{_create_args} = $_[1] if @_ > 1; |
51 | $_[0]->{_create_args} |
52 | } |
53 | |
7f7406a5 |
54 | sub inlined_name { |
55 | my $self = shift; |
56 | my $name = $self->name; |
57 | my $key = "'" . $name . "'"; |
58 | return $key; |
59 | } |
60 | |
c3398f5b |
61 | sub generate_accessor { |
62 | my $attribute = shift; |
63 | |
32af3489 |
64 | my $name = $attribute->name; |
65 | my $default = $attribute->default; |
684db121 |
66 | my $constraint = $attribute->type_constraint; |
32af3489 |
67 | my $builder = $attribute->builder; |
68 | my $trigger = $attribute->trigger; |
69 | my $is_weak = $attribute->is_weak_ref; |
70 | my $should_deref = $attribute->should_auto_deref; |
71 | my $should_coerce = $attribute->should_coerce; |
4e757c98 |
72 | |
85ccd3e9 |
73 | my $self = '$_[0]'; |
85ccd3e9 |
74 | my $key = $attribute->inlined_name; |
c3398f5b |
75 | |
321e5271 |
76 | my $accessor = |
77 | '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" . |
78 | "sub {\n"; |
2434d21b |
79 | if ($attribute->_is_metadata eq 'rw') { |
321e5271 |
80 | $accessor .= |
81 | '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" . |
82 | 'if (@_ >= 2) {' . "\n"; |
7de13475 |
83 | |
84 | my $value = '$_[1]'; |
a707f587 |
85 | |
a08e715f |
86 | if ($constraint) { |
eec1bb49 |
87 | $accessor .= 'my $val = '; |
32af3489 |
88 | if ($should_coerce) { |
321e5271 |
89 | $accessor .= |
90 | "\n". |
91 | '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" . |
684db121 |
92 | 'Mouse::Util::TypeConstraints->typecast_constraints("'.$attribute->associated_class->name.'", $attribute->{type_constraint}, '.$value.');'; |
eec1bb49 |
93 | } else { |
94 | $accessor .= $value.';'; |
4188b837 |
95 | } |
321e5271 |
96 | $accessor .= |
97 | "\n". |
98 | '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" . |
684db121 |
99 | 'unless ($constraint->check($val)) { |
100 | $attribute->verify_type_constraint_error($name, $val, $attribute->{type_constraint}); |
b3b74cc6 |
101 | }' . "\n"; |
eec1bb49 |
102 | $value = '$val'; |
a707f587 |
103 | } |
104 | |
ccd73de7 |
105 | # if there's nothing left to do for the attribute we can return during |
106 | # this setter |
107 | $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref; |
c3398f5b |
108 | |
2b9094e8 |
109 | $accessor .= $self.'->{'.$key.'} = '.$value.';' . "\n"; |
110 | |
ccd73de7 |
111 | if ($is_weak) { |
6c169c50 |
112 | $accessor .= 'Scalar::Util::weaken('.$self.'->{'.$key.'}) if ref('.$self.'->{'.$key.'});' . "\n"; |
c3398f5b |
113 | } |
114 | |
a08e715f |
115 | if ($trigger) { |
88b6c018 |
116 | $accessor .= '$trigger->('.$self.', '.$value.');' . "\n"; |
c3398f5b |
117 | } |
118 | |
85ccd3e9 |
119 | $accessor .= "}\n"; |
c3398f5b |
120 | } |
121 | else { |
85ccd3e9 |
122 | $accessor .= 'confess "Cannot assign a value to a read-only accessor" if scalar(@_) >= 2;' . "\n"; |
c3398f5b |
123 | } |
124 | |
3aadb773 |
125 | if ($attribute->is_lazy) { |
85ccd3e9 |
126 | $accessor .= $self.'->{'.$key.'} = '; |
9367e029 |
127 | |
128 | $accessor .= $attribute->has_builder |
85ccd3e9 |
129 | ? $self.'->$builder' |
130 | : ref($default) eq 'CODE' |
131 | ? '$default->('.$self.')' |
132 | : '$default'; |
133 | $accessor .= ' if !exists '.$self.'->{'.$key.'};' . "\n"; |
c3398f5b |
134 | } |
135 | |
ccd73de7 |
136 | if ($should_deref) { |
684db121 |
137 | my $type_constraint = $attribute->{type_constraint}; |
138 | if (ref($type_constraint) && $type_constraint->name eq 'ArrayRef') { |
3cf68001 |
139 | $accessor .= 'if (wantarray) { |
85ccd3e9 |
140 | return @{ '.$self.'->{'.$key.'} || [] }; |
7de13475 |
141 | }'; |
3cf68001 |
142 | } |
143 | else { |
144 | $accessor .= 'if (wantarray) { |
85ccd3e9 |
145 | return %{ '.$self.'->{'.$key.'} || {} }; |
7de13475 |
146 | }'; |
3cf68001 |
147 | } |
148 | } |
149 | |
85ccd3e9 |
150 | $accessor .= 'return '.$self.'->{'.$key.'}; |
c3398f5b |
151 | }'; |
152 | |
71b948d1 |
153 | my $sub = eval $accessor; |
154 | confess $@ if $@; |
155 | return $sub; |
c3398f5b |
156 | } |
157 | |
158 | sub generate_predicate { |
159 | my $attribute = shift; |
d10fd510 |
160 | my $key = $attribute->inlined_name; |
c3398f5b |
161 | |
d10fd510 |
162 | my $predicate = 'sub { exists($_[0]->{'.$key.'}) }'; |
c3398f5b |
163 | |
71b948d1 |
164 | my $sub = eval $predicate; |
165 | confess $@ if $@; |
166 | return $sub; |
c3398f5b |
167 | } |
168 | |
169 | sub generate_clearer { |
170 | my $attribute = shift; |
d10fd510 |
171 | my $key = $attribute->inlined_name; |
c3398f5b |
172 | |
d10fd510 |
173 | my $clearer = 'sub { delete($_[0]->{'.$key.'}) }'; |
c3398f5b |
174 | |
71b948d1 |
175 | my $sub = eval $clearer; |
176 | confess $@ if $@; |
177 | return $sub; |
c3398f5b |
178 | } |
179 | |
180 | sub generate_handles { |
181 | my $attribute = shift; |
2434d21b |
182 | my $reader = $attribute->name; |
c3cc3642 |
183 | my %handles = $attribute->_canonicalize_handles($attribute->handles); |
c3398f5b |
184 | |
185 | my %method_map; |
186 | |
c3cc3642 |
187 | for my $local_method (keys %handles) { |
188 | my $remote_method = $handles{$local_method}; |
c3398f5b |
189 | |
190 | my $method = 'sub { |
191 | my $self = shift; |
0ecefe82 |
192 | $self->'.$reader.'->'.$remote_method.'(@_) |
c3398f5b |
193 | }'; |
194 | |
195 | $method_map{$local_method} = eval $method; |
71b948d1 |
196 | confess $@ if $@; |
c3398f5b |
197 | } |
198 | |
199 | return \%method_map; |
200 | } |
201 | |
202 | sub create { |
203 | my ($self, $class, $name, %args) = @_; |
204 | |
1bfebf5f |
205 | $args{name} = $name; |
181502b9 |
206 | $args{associated_class} = $class; |
1bfebf5f |
207 | |
93d190e0 |
208 | %args = $self->canonicalize_args($name, %args); |
1bbaa8ed |
209 | $self->validate_args($name, \%args); |
45ea8620 |
210 | |
32af3489 |
211 | $args{should_coerce} = delete $args{coerce} |
4188b837 |
212 | if exists $args{coerce}; |
213 | |
eec1bb49 |
214 | if (exists $args{isa}) { |
c1c94293 |
215 | confess "Got isa => $args{isa}, but Mouse does not yet support parameterized types for containers other than ArrayRef and HashRef (rt.cpan.org #39795)" |
315b97c2 |
216 | if $args{isa} =~ /^([^\[]+)\[.+\]$/ && |
217 | $1 ne 'ArrayRef' && |
a510e63c |
218 | $1 ne 'HashRef' && |
219 | $1 ne 'Maybe' |
220 | ; |
5fa003bf |
221 | |
eec1bb49 |
222 | my $type_constraint = delete $args{isa}; |
684db121 |
223 | $args{type_constraint}= Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($type_constraint); |
eec1bb49 |
224 | } |
186657a9 |
225 | |
2608b115 |
226 | my $attribute = $self->new($name, %args); |
1bfebf5f |
227 | |
724c77c0 |
228 | $attribute->_create_args(\%args); |
c3398f5b |
229 | |
724c77c0 |
230 | $class->add_attribute($attribute); |
b2500191 |
231 | |
c3398f5b |
232 | # install an accessor |
2434d21b |
233 | if ($attribute->_is_metadata eq 'rw' || $attribute->_is_metadata eq 'ro') { |
c3398f5b |
234 | my $accessor = $attribute->generate_accessor; |
724c77c0 |
235 | $class->add_method($name => $accessor); |
c3398f5b |
236 | } |
237 | |
c3398f5b |
238 | for my $method (qw/predicate clearer/) { |
2434d21b |
239 | my $predicate = "has_$method"; |
240 | if ($attribute->$predicate) { |
c3398f5b |
241 | my $generator = "generate_$method"; |
242 | my $coderef = $attribute->$generator; |
724c77c0 |
243 | $class->add_method($attribute->$method => $coderef); |
c3398f5b |
244 | } |
245 | } |
246 | |
2434d21b |
247 | if ($attribute->has_handles) { |
c3398f5b |
248 | my $method_map = $attribute->generate_handles; |
249 | for my $method_name (keys %$method_map) { |
724c77c0 |
250 | $class->add_method($method_name => $method_map->{$method_name}); |
c3398f5b |
251 | } |
252 | } |
253 | |
254 | return $attribute; |
255 | } |
256 | |
93d190e0 |
257 | sub canonicalize_args { |
258 | my $self = shift; |
259 | my $name = shift; |
260 | my %args = @_; |
261 | |
262 | if ($args{lazy_build}) { |
263 | $args{lazy} = 1; |
264 | $args{required} = 1; |
265 | $args{builder} = "_build_${name}" |
266 | if !exists($args{builder}); |
267 | if ($name =~ /^_/) { |
268 | $args{clearer} = "_clear${name}" if !exists($args{clearer}); |
269 | $args{predicate} = "_has${name}" if !exists($args{predicate}); |
270 | } |
271 | else { |
272 | $args{clearer} = "clear_${name}" if !exists($args{clearer}); |
273 | $args{predicate} = "has_${name}" if !exists($args{predicate}); |
274 | } |
275 | } |
276 | |
277 | return %args; |
278 | } |
279 | |
8fd9e611 |
280 | sub validate_args { |
281 | my $self = shift; |
282 | my $name = shift; |
1bbaa8ed |
283 | my $args = shift; |
8fd9e611 |
284 | |
93d190e0 |
285 | confess "You can not use lazy_build and default for the same attribute ($name)" |
1bbaa8ed |
286 | if $args->{lazy_build} && exists $args->{default}; |
93d190e0 |
287 | |
8fd9e611 |
288 | confess "You cannot have lazy attribute ($name) without specifying a default value for it" |
1bbaa8ed |
289 | if $args->{lazy} |
290 | && !exists($args->{default}) |
291 | && !exists($args->{builder}); |
8fd9e611 |
292 | |
293 | confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])" |
1bbaa8ed |
294 | if ref($args->{default}) |
295 | && ref($args->{default}) ne 'CODE'; |
8fd9e611 |
296 | |
97661b77 |
297 | confess "You cannot auto-dereference without specifying a type constraint on attribute ($name)" |
1bbaa8ed |
298 | if $args->{auto_deref} && !exists($args->{isa}); |
8fd9e611 |
299 | |
97661b77 |
300 | confess "You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)" |
1bbaa8ed |
301 | if $args->{auto_deref} |
302 | && $args->{isa} ne 'ArrayRef' |
303 | && $args->{isa} ne 'HashRef'; |
8fd9e611 |
304 | |
506db557 |
305 | if ($args->{trigger}) { |
a08e715f |
306 | if (ref($args->{trigger}) eq 'HASH') { |
307 | Carp::carp "HASH-based form of trigger has been removed. Only the coderef form of triggers are now supported."; |
844fa049 |
308 | } |
506db557 |
309 | |
844fa049 |
310 | confess "Trigger must be a CODE ref on attribute ($name)" |
a08e715f |
311 | if ref($args->{trigger}) ne 'CODE'; |
506db557 |
312 | } |
6c5498d0 |
313 | |
8fd9e611 |
314 | return 1; |
315 | } |
316 | |
20e25eb9 |
317 | sub verify_against_type_constraint { |
8a7f2a8a |
318 | return 1 unless $_[0]->{type_constraint}; |
5aa30ced |
319 | |
8a7f2a8a |
320 | local $_ = $_[1]; |
684db121 |
321 | return 1 if $_[0]->{type_constraint}->check($_); |
5aa30ced |
322 | |
8a7f2a8a |
323 | my $self = shift; |
684db121 |
324 | $self->verify_type_constraint_error($self->name, $_, $self->{type_constraint}); |
b3b74cc6 |
325 | } |
5aa30ced |
326 | |
b3b74cc6 |
327 | sub verify_type_constraint_error { |
328 | my($self, $name, $value, $type) = @_; |
684db121 |
329 | $type = ref($type) eq 'ARRAY' ? join '|', map { $_->name } @{ $type } : $type->name; |
c91d12e0 |
330 | my $display = defined($value) ? overload::StrVal($value) : 'undef'; |
f3e05dfd |
331 | Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display"); |
5aa30ced |
332 | } |
333 | |
8a7f2a8a |
334 | sub coerce_constraint { ## my($self, $value) = @_; |
335 | my $type = $_[0]->{type_constraint} |
336 | or return $_[1]; |
684db121 |
337 | return Mouse::Util::TypeConstraints->typecast_constraints($_[0]->associated_class->name, $_[0]->type_constraint, $_[1]); |
4188b837 |
338 | } |
339 | |
af745d5a |
340 | sub _canonicalize_handles { |
341 | my $self = shift; |
342 | my $handles = shift; |
343 | |
344 | if (ref($handles) eq 'HASH') { |
345 | return %$handles; |
346 | } |
347 | elsif (ref($handles) eq 'ARRAY') { |
348 | return map { $_ => $_ } @$handles; |
349 | } |
350 | else { |
351 | confess "Unable to canonicalize the 'handles' option with $handles"; |
352 | } |
353 | } |
354 | |
1bfebf5f |
355 | sub clone_parent { |
356 | my $self = shift; |
357 | my $class = shift; |
358 | my $name = shift; |
359 | my %args = ($self->get_parent_args($class, $name), @_); |
360 | |
361 | $self->create($class, $name, %args); |
362 | } |
363 | |
364 | sub get_parent_args { |
365 | my $self = shift; |
366 | my $class = shift; |
367 | my $name = shift; |
368 | |
724c77c0 |
369 | for my $super ($class->linearized_isa) { |
bb733405 |
370 | my $super_attr = $super->can("meta") && $super->meta->get_attribute($name) |
1bfebf5f |
371 | or next; |
372 | return %{ $super_attr->_create_args }; |
373 | } |
374 | |
375 | confess "Could not find an attribute by the name of '$name' to inherit from"; |
376 | } |
377 | |
c3398f5b |
378 | 1; |
379 | |
380 | __END__ |
381 | |
382 | =head1 NAME |
383 | |
306290e8 |
384 | Mouse::Meta::Attribute - attribute metaclass |
c3398f5b |
385 | |
386 | =head1 METHODS |
387 | |
306290e8 |
388 | =head2 new %args -> Mouse::Meta::Attribute |
c3398f5b |
389 | |
306290e8 |
390 | Instantiates a new Mouse::Meta::Attribute. Does nothing else. |
c3398f5b |
391 | |
306290e8 |
392 | =head2 create OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute |
c3398f5b |
393 | |
394 | Creates a new attribute in OwnerClass. Accessors and helper methods are |
395 | installed. Some error checking is done. |
396 | |
397 | =head2 name -> AttributeName |
398 | |
181502b9 |
399 | =head2 associated_class -> OwnerClass |
c3398f5b |
400 | |
ab27a55e |
401 | =head2 is_required -> Bool |
c3398f5b |
402 | |
ab27a55e |
403 | =head2 default -> Item |
c3398f5b |
404 | |
ab27a55e |
405 | =head2 has_default -> Bool |
406 | |
407 | =head2 is_lazy -> Bool |
408 | |
409 | =head2 predicate -> MethodName | Undef |
410 | |
411 | =head2 has_predicate -> Bool |
412 | |
413 | =head2 clearer -> MethodName | Undef |
414 | |
415 | =head2 has_clearer -> Bool |
c3398f5b |
416 | |
417 | =head2 handles -> { LocalName => RemoteName } |
418 | |
ab27a55e |
419 | =head2 has_handles -> Bool |
420 | |
3645b316 |
421 | =head2 is_weak_ref -> Bool |
c3398f5b |
422 | |
423 | =head2 init_arg -> Str |
424 | |
ab27a55e |
425 | =head2 type_constraint -> Str |
426 | |
427 | =head2 has_type_constraint -> Bool |
428 | |
429 | =head2 trigger => CODE | Undef |
430 | |
431 | =head2 has_trigger -> Bool |
432 | |
433 | =head2 builder => MethodName | Undef |
434 | |
435 | =head2 has_builder -> Bool |
436 | |
93f08899 |
437 | =head2 is_lazy_build => Bool |
438 | |
0fff36e6 |
439 | =head2 should_auto_deref -> Bool |
440 | |
c3398f5b |
441 | Informational methods. |
442 | |
443 | =head2 generate_accessor -> CODE |
444 | |
445 | Creates a new code reference for the attribute's accessor. |
446 | |
447 | =head2 generate_predicate -> CODE |
448 | |
449 | Creates a new code reference for the attribute's predicate. |
450 | |
451 | =head2 generate_clearer -> CODE |
452 | |
453 | Creates a new code reference for the attribute's clearer. |
454 | |
455 | =head2 generate_handles -> { MethodName => CODE } |
456 | |
457 | Creates a new code reference for each of the attribute's handles methods. |
458 | |
20e25eb9 |
459 | =head2 verify_against_type_constraint Item -> 1 | ERROR |
fb706f5c |
460 | |
461 | Checks that the given value passes this attribute's type constraint. Returns 1 |
462 | on success, otherwise C<confess>es. |
463 | |
93d190e0 |
464 | =head2 canonicalize_args Name, %args -> %args |
465 | |
466 | Canonicalizes some arguments to create. In particular, C<lazy_build> is |
467 | canonicalized into C<lazy>, C<builder>, etc. |
468 | |
1bbaa8ed |
469 | =head2 validate_args Name, \%args -> 1 | ERROR |
93d190e0 |
470 | |
471 | Checks that the arguments to create the attribute (ie those specified by |
472 | C<has>) are valid. |
473 | |
f7b11a21 |
474 | =head2 clone_parent OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute |
475 | |
476 | Creates a new attribute in OwnerClass, inheriting options from parent classes. |
477 | Accessors and helper methods are installed. Some error checking is done. |
478 | |
479 | =head2 get_parent_args OwnerClass, AttributeName -> Hash |
480 | |
481 | Returns the options that the parent class of C<OwnerClass> used for attribute |
482 | C<AttributeName>. |
483 | |
c3398f5b |
484 | =cut |
485 | |