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