Commit | Line | Data |
306290e8 |
1 | package Mouse::Meta::Attribute; |
bc69ee88 |
2 | use Mouse::Util qw(:meta); # enables strict and warnings |
c3398f5b |
3 | |
2efc0af1 |
4 | use Carp (); |
2efc0af1 |
5 | |
684db121 |
6 | use Mouse::Meta::TypeConstraint; |
d7d8d49b |
7 | |
7d2a0f10 |
8 | my %valid_options = map { $_ => undef } ( |
9 | 'accessor', |
10 | 'auto_deref', |
11 | 'builder', |
12 | 'clearer', |
13 | 'coerce', |
14 | 'default', |
15 | 'documentation', |
16 | 'does', |
17 | 'handles', |
18 | 'init_arg', |
19 | 'is', |
20 | 'isa', |
21 | 'lazy', |
22 | 'lazy_build', |
23 | 'name', |
24 | 'predicate', |
25 | 'reader', |
26 | 'required', |
27 | 'traits', |
28 | 'trigger', |
29 | 'type_constraint', |
30 | 'weak_ref', |
31 | 'writer', |
32 | |
33 | # internally used |
34 | 'associated_class', |
35 | 'associated_methods', |
36 | |
37 | # Moose defines, but Mouse doesn't |
38 | #'definition_context', |
39 | #'initializer', |
40 | #'insertion_order', |
41 | |
42 | # special case for AttributeHelpers |
43 | 'provides', |
44 | 'curries', |
45 | ); |
87ca293b |
46 | |
431e4817 |
47 | our @CARP_NOT = qw(Mouse::Meta::Class); |
48 | |
87ca293b |
49 | sub new { |
50 | my $class = shift; |
51 | my $name = shift; |
52 | |
2053291d |
53 | my $args = $class->Mouse::Object::BUILDARGS(@_); |
ba1f50a2 |
54 | |
55 | # XXX: for backward compatibility (with method modifiers) |
56 | if($class->can('canonicalize_args') != \&canonicalize_args){ |
2053291d |
57 | %{$args} = $class->canonicalize_args($name, %{$args}); |
ba1f50a2 |
58 | } |
59 | |
2053291d |
60 | $class->_process_options($name, $args); |
87ca293b |
61 | |
2053291d |
62 | $args->{name} = $name; |
87ca293b |
63 | |
7d2a0f10 |
64 | # check options |
65 | # (1) known by core |
66 | my @bad = grep{ !exists $valid_options{$_} } keys %{$args}; |
67 | |
68 | # (2) known by subclasses |
69 | if(@bad && $class ne __PACKAGE__){ |
70 | my %valid_attrs = ( |
71 | map { $_ => undef } |
72 | grep { defined } |
73 | map { $_->init_arg() } |
74 | $class->meta->get_all_attributes() |
75 | ); |
76 | @bad = grep{ !exists $valid_attrs{$_} } @bad; |
77 | } |
78 | |
79 | # (3) bad options found |
80 | if(@bad){ |
431e4817 |
81 | Carp::carp( |
82 | "Found unknown argument(s) passed to '$name' attribute constructor in '$class': " |
83 | . Mouse::Util::english_list(@bad)); |
7d2a0f10 |
84 | } |
85 | |
2053291d |
86 | my $self = bless $args, $class; |
2efc0af1 |
87 | |
1b9e472d |
88 | # extra attributes |
89 | if($class ne __PACKAGE__){ |
2053291d |
90 | $class->meta->_initialize_object($self, $args); |
90fe520e |
91 | } |
c3398f5b |
92 | |
926290ac |
93 | return $self; |
c3398f5b |
94 | } |
95 | |
43165725 |
96 | sub has_read_method { $_[0]->has_reader || $_[0]->has_accessor } |
97 | sub has_write_method { $_[0]->has_writer || $_[0]->has_accessor } |
1b9e472d |
98 | |
df77fd72 |
99 | sub _create_args { # DEPRECATED |
1bfebf5f |
100 | $_[0]->{_create_args} = $_[1] if @_ > 1; |
101 | $_[0]->{_create_args} |
102 | } |
103 | |
87ca293b |
104 | sub interpolate_class{ |
8cf51b82 |
105 | my($class, $args) = @_; |
c3398f5b |
106 | |
1b9e472d |
107 | if(my $metaclass = delete $args->{metaclass}){ |
108 | $class = Mouse::Util::resolve_metaclass_alias( Attribute => $metaclass ); |
109 | } |
1bfebf5f |
110 | |
87ca293b |
111 | my @traits; |
1b9e472d |
112 | if(my $traits_ref = delete $args->{traits}){ |
87ca293b |
113 | |
f3f04eed |
114 | for (my $i = 0; $i < @{$traits_ref}; $i++) { |
115 | my $trait = Mouse::Util::resolve_metaclass_alias(Attribute => $traits_ref->[$i], trait => 1); |
b2500191 |
116 | |
f3f04eed |
117 | next if $class->does($trait); |
74be9f76 |
118 | |
f3f04eed |
119 | push @traits, $trait; |
1b9e472d |
120 | |
f3f04eed |
121 | # are there options? |
122 | push @traits, $traits_ref->[++$i] |
123 | if ref($traits_ref->[$i+1]); |
124 | } |
c3398f5b |
125 | |
1b9e472d |
126 | if (@traits) { |
127 | $class = Mouse::Meta::Class->create_anon_class( |
128 | superclasses => [ $class ], |
129 | roles => \@traits, |
130 | cache => 1, |
131 | )->name; |
1b9e472d |
132 | } |
74be9f76 |
133 | } |
134 | |
87ca293b |
135 | return( $class, @traits ); |
1b9e472d |
136 | } |
137 | |
df77fd72 |
138 | sub canonicalize_args{ # DEPRECATED |
1b9e472d |
139 | my ($self, $name, %args) = @_; |
140 | |
141 | Carp::cluck("$self->canonicalize_args has been deprecated." |
da23cd4a |
142 | . "Use \$self->_process_options instead."); |
1b9e472d |
143 | |
144 | return %args; |
145 | } |
146 | |
ce5920b7 |
147 | sub create { # DEPRECATED |
1b9e472d |
148 | my ($self, $class, $name, %args) = @_; |
149 | |
150 | Carp::cluck("$self->create has been deprecated." |
da23cd4a |
151 | . "Use \$meta->add_attribute and \$attr->install_accessors instead."); |
1b9e472d |
152 | |
153 | # noop |
154 | return $self; |
c3398f5b |
155 | } |
156 | |
ffbbf459 |
157 | sub _coerce_and_verify { |
158 | my($self, $value, $instance) = @_; |
159 | |
160 | my $type_constraint = $self->{type_constraint}; |
4331a3f9 |
161 | return $value if !defined $type_constraint; |
ffbbf459 |
162 | |
163 | if ($self->should_coerce && $type_constraint->has_coercion) { |
164 | $value = $type_constraint->coerce($value); |
165 | } |
166 | |
ffbbf459 |
167 | $self->verify_against_type_constraint($value); |
168 | |
169 | return $value; |
170 | } |
171 | |
20e25eb9 |
172 | sub verify_against_type_constraint { |
f55f60dd |
173 | my ($self, $value) = @_; |
5aa30ced |
174 | |
ffbbf459 |
175 | my $type_constraint = $self->{type_constraint}; |
4331a3f9 |
176 | return 1 if !$type_constraint; |
ffbbf459 |
177 | return 1 if $type_constraint->check($value); |
5aa30ced |
178 | |
da23cd4a |
179 | $self->_throw_type_constraint_error($value, $type_constraint); |
b3b74cc6 |
180 | } |
5aa30ced |
181 | |
da23cd4a |
182 | sub _throw_type_constraint_error { |
2a96ea85 |
183 | my($self, $value, $type) = @_; |
184 | |
185 | $self->throw_error( |
186 | sprintf q{Attribute (%s) does not pass the type constraint because: %s}, |
187 | $self->name, |
188 | $type->get_message($value), |
189 | ); |
5aa30ced |
190 | } |
191 | |
1b9e472d |
192 | sub clone_and_inherit_options{ |
2053291d |
193 | my $self = shift; |
194 | my $args = $self->Mouse::Object::BUILDARGS(@_); |
8cf51b82 |
195 | |
2053291d |
196 | my($attribute_class, @traits) = ref($self)->interpolate_class($args); |
1b9e472d |
197 | |
2053291d |
198 | $args->{traits} = \@traits if @traits; |
4f41d79b |
199 | # do not inherit the 'handles' attribute |
200 | foreach my $name(keys %{$self}){ |
2053291d |
201 | if(!exists $args->{$name} && $name ne 'handles'){ |
202 | $args->{$name} = $self->{$name}; |
4f41d79b |
203 | } |
204 | } |
7d2a0f10 |
205 | |
206 | # remove temporary caches |
207 | foreach my $attr(keys %{$args}){ |
208 | if($attr =~ /\A _/xms){ |
209 | delete $args->{$attr}; |
210 | } |
211 | } |
212 | |
2053291d |
213 | return $attribute_class->new($self->name, $args); |
1b9e472d |
214 | } |
215 | |
df77fd72 |
216 | sub clone_parent { # DEPRECATED |
1bfebf5f |
217 | my $self = shift; |
218 | my $class = shift; |
219 | my $name = shift; |
220 | my %args = ($self->get_parent_args($class, $name), @_); |
221 | |
1b9e472d |
222 | Carp::cluck("$self->clone_parent has been deprecated." |
da23cd4a |
223 | . "Use \$meta->add_attribute and \$attr->install_accessors instead."); |
1b9e472d |
224 | |
7ca5c5fb |
225 | $self->clone_and_inherited_args($class, $name, %args); |
1bfebf5f |
226 | } |
227 | |
df77fd72 |
228 | sub get_parent_args { # DEPRECATED |
1bfebf5f |
229 | my $self = shift; |
230 | my $class = shift; |
231 | my $name = shift; |
232 | |
724c77c0 |
233 | for my $super ($class->linearized_isa) { |
bb733405 |
234 | my $super_attr = $super->can("meta") && $super->meta->get_attribute($name) |
1bfebf5f |
235 | or next; |
236 | return %{ $super_attr->_create_args }; |
237 | } |
238 | |
fce211ae |
239 | $self->throw_error("Could not find an attribute by the name of '$name' to inherit from"); |
240 | } |
241 | |
2a464664 |
242 | |
0126c27c |
243 | sub get_read_method { |
751de1a1 |
244 | return $_[0]->reader || $_[0]->accessor |
df77fd72 |
245 | } |
0126c27c |
246 | sub get_write_method { |
751de1a1 |
247 | return $_[0]->writer || $_[0]->accessor |
248 | } |
249 | |
250 | sub _get_accessor_method_ref { |
251 | my($self, $type, $generator) = @_; |
252 | |
253 | my $metaclass = $self->associated_class |
254 | || $self->throw_error('No asocciated class for ' . $self->name); |
255 | |
256 | my $accessor = $self->$type(); |
257 | if($accessor){ |
258 | return $metaclass->get_method_body($accessor); |
259 | } |
260 | else{ |
261 | return $self->accessor_metaclass->$generator($self, $metaclass); |
262 | } |
df77fd72 |
263 | } |
2a464664 |
264 | |
265 | sub get_read_method_ref{ |
266 | my($self) = @_; |
751de1a1 |
267 | return $self->{_read_method_ref} ||= $self->_get_accessor_method_ref('get_read_method', '_generate_reader'); |
2a464664 |
268 | } |
269 | |
270 | sub get_write_method_ref{ |
271 | my($self) = @_; |
751de1a1 |
272 | return $self->{_write_method_ref} ||= $self->_get_accessor_method_ref('get_write_method', '_generate_writer'); |
273 | } |
2a464664 |
274 | |
751de1a1 |
275 | sub set_value { |
276 | my($self, $object, $value) = @_; |
277 | return $self->get_write_method_ref()->($object, $value); |
278 | } |
279 | |
280 | sub get_value { |
281 | my($self, $object) = @_; |
282 | return $self->get_read_method_ref()->($object); |
2a464664 |
283 | } |
284 | |
751de1a1 |
285 | sub has_value { |
286 | my($self, $object) = @_; |
060f9228 |
287 | my $accessor_ref = $self->{_predicate_ref} |
751de1a1 |
288 | ||= $self->_get_accessor_method_ref('predicate', '_generate_predicate'); |
289 | |
060f9228 |
290 | return $accessor_ref->($object); |
751de1a1 |
291 | } |
292 | |
293 | sub clear_value { |
294 | my($self, $object) = @_; |
060f9228 |
295 | my $accessor_ref = $self->{_crealer_ref} |
751de1a1 |
296 | ||= $self->_get_accessor_method_ref('clearer', '_generate_clearer'); |
297 | |
060f9228 |
298 | return $accessor_ref->($object); |
751de1a1 |
299 | } |
300 | |
301 | |
04493075 |
302 | sub associate_method{ |
e5e22afd |
303 | my ($attribute, $method_name) = @_; |
04493075 |
304 | $attribute->{associated_methods}++; |
305 | return; |
306 | } |
307 | |
1b9e472d |
308 | sub install_accessors{ |
309 | my($attribute) = @_; |
310 | |
93540011 |
311 | my $metaclass = $attribute->associated_class; |
4ab51fb0 |
312 | my $accessor_class = $attribute->accessor_metaclass; |
1b9e472d |
313 | |
4ab51fb0 |
314 | foreach my $type(qw(accessor reader writer predicate clearer)){ |
1b9e472d |
315 | if(exists $attribute->{$type}){ |
4ab51fb0 |
316 | my $generator = '_generate_' . $type; |
317 | my $code = $accessor_class->$generator($attribute, $metaclass); |
318 | $metaclass->add_method($attribute->{$type} => $code); |
e5e22afd |
319 | $attribute->associate_method($attribute->{$type}); |
4ab51fb0 |
320 | } |
321 | } |
7ca5c5fb |
322 | |
4ab51fb0 |
323 | # install delegation |
324 | if(exists $attribute->{handles}){ |
325 | my %handles = $attribute->_canonicalize_handles($attribute->{handles}); |
feaa7084 |
326 | |
cbb81058 |
327 | while(my($handle, $method_to_call) = each %handles){ |
328 | $metaclass->add_method($handle => |
329 | $attribute->_make_delegation_method( |
330 | $handle, $method_to_call)); |
4ab51fb0 |
331 | |
cbb81058 |
332 | $attribute->associate_method($handle); |
1b9e472d |
333 | } |
334 | } |
335 | |
336 | if($attribute->can('create') != \&create){ |
7ca5c5fb |
337 | # backword compatibility |
1b9e472d |
338 | $attribute->create($metaclass, $attribute->name, %{$attribute}); |
339 | } |
340 | |
341 | return; |
342 | } |
343 | |
cbb81058 |
344 | sub delegation_metaclass() { 'Mouse::Meta::Method::Delegation' } |
345 | |
346 | sub _canonicalize_handles { |
347 | my($self, $handles) = @_; |
348 | |
349 | if (ref($handles) eq 'HASH') { |
350 | return %$handles; |
351 | } |
352 | elsif (ref($handles) eq 'ARRAY') { |
353 | return map { $_ => $_ } @$handles; |
354 | } |
7bc01428 |
355 | elsif ( ref($handles) eq 'CODE' ) { |
356 | my $class_or_role = ( $self->{isa} || $self->{does} ) |
357 | || $self->throw_error( "Cannot find delegate metaclass for attribute " . $self->name ); |
358 | return $handles->( $self, Mouse::Meta::Class->initialize("$class_or_role")); |
359 | } |
cbb81058 |
360 | elsif (ref($handles) eq 'Regexp') { |
361 | my $class_or_role = ($self->{isa} || $self->{does}) |
362 | || $self->throw_error("Cannot delegate methods based on a Regexp without a type constraint (isa)"); |
363 | |
364 | my $meta = Mouse::Meta::Class->initialize("$class_or_role"); # "" for stringify |
365 | return map { $_ => $_ } |
366 | grep { !Mouse::Object->can($_) && $_ =~ $handles } |
367 | Mouse::Util::is_a_metarole($meta) |
368 | ? $meta->get_method_list |
369 | : $meta->get_all_method_names; |
370 | } |
371 | else { |
372 | $self->throw_error("Unable to canonicalize the 'handles' option with $handles"); |
373 | } |
374 | } |
375 | |
376 | sub _make_delegation_method { |
377 | my($self, $handle, $method_to_call) = @_; |
637d4f17 |
378 | return Mouse::Util::load_class($self->delegation_metaclass) |
379 | ->_generate_delegation($self, $handle, $method_to_call); |
cbb81058 |
380 | } |
381 | |
fce211ae |
382 | sub throw_error{ |
383 | my $self = shift; |
384 | |
385 | my $metaclass = (ref $self && $self->associated_class) || 'Mouse::Meta::Class'; |
386 | $metaclass->throw_error(@_, depth => 1); |
1bfebf5f |
387 | } |
388 | |
c3398f5b |
389 | 1; |
c3398f5b |
390 | __END__ |
391 | |
392 | =head1 NAME |
393 | |
bedd575c |
394 | Mouse::Meta::Attribute - The Mouse attribute metaclass |
c3398f5b |
395 | |
a25ca8d6 |
396 | =head1 VERSION |
397 | |
98a8fd12 |
398 | This document describes Mouse version 0.50_05 |
a25ca8d6 |
399 | |
c3398f5b |
400 | =head1 METHODS |
401 | |
1820fffe |
402 | =head2 C<< new(%options) -> Mouse::Meta::Attribute >> |
c3398f5b |
403 | |
306290e8 |
404 | Instantiates a new Mouse::Meta::Attribute. Does nothing else. |
c3398f5b |
405 | |
1820fffe |
406 | It adds the following options to the constructor: |
c3398f5b |
407 | |
1820fffe |
408 | =over 4 |
c3398f5b |
409 | |
612d3e1a |
410 | =item C<< is => 'ro', 'rw', 'bare' >> |
c3398f5b |
411 | |
1820fffe |
412 | This provides a shorthand for specifying the C<reader>, C<writer>, or |
413 | C<accessor> names. If the attribute is read-only ('ro') then it will |
414 | have a C<reader> method with the same attribute as the name. |
c3398f5b |
415 | |
1820fffe |
416 | If it is read-write ('rw') then it will have an C<accessor> method |
417 | with the same name. If you provide an explicit C<writer> for a |
418 | read-write attribute, then you will have a C<reader> with the same |
419 | name as the attribute, and a C<writer> with the name you provided. |
c3398f5b |
420 | |
1820fffe |
421 | Use 'bare' when you are deliberately not installing any methods |
422 | (accessor, reader, etc.) associated with this attribute; otherwise, |
423 | Moose will issue a deprecation warning when this attribute is added to a |
424 | metaclass. |
c3398f5b |
425 | |
612d3e1a |
426 | =item C<< isa => Type >> |
ab27a55e |
427 | |
1820fffe |
428 | This option accepts a type. The type can be a string, which should be |
429 | a type name. If the type name is unknown, it is assumed to be a class |
430 | name. |
ab27a55e |
431 | |
1820fffe |
432 | This option can also accept a L<Moose::Meta::TypeConstraint> object. |
ab27a55e |
433 | |
1820fffe |
434 | If you I<also> provide a C<does> option, then your C<isa> option must |
435 | be a class name, and that class must do the role specified with |
436 | C<does>. |
ab27a55e |
437 | |
612d3e1a |
438 | =item C<< does => Role >> |
ab27a55e |
439 | |
1820fffe |
440 | This is short-hand for saying that the attribute's type must be an |
441 | object which does the named role. |
c3398f5b |
442 | |
1820fffe |
443 | B<This option is not yet supported.> |
c3398f5b |
444 | |
612d3e1a |
445 | =item C<< coerce => Bool >> |
ab27a55e |
446 | |
1820fffe |
447 | This option is only valid for objects with a type constraint |
448 | (C<isa>). If this is true, then coercions will be applied whenever |
449 | this attribute is set. |
c3398f5b |
450 | |
1820fffe |
451 | You can make both this and the C<weak_ref> option true. |
c3398f5b |
452 | |
612d3e1a |
453 | =item C<< trigger => CodeRef >> |
ab27a55e |
454 | |
1820fffe |
455 | This option accepts a subroutine reference, which will be called after |
456 | the attribute is set. |
ab27a55e |
457 | |
612d3e1a |
458 | =item C<< required => Bool >> |
ab27a55e |
459 | |
1820fffe |
460 | An attribute which is required must be provided to the constructor. An |
461 | attribute which is required can also have a C<default> or C<builder>, |
462 | which will satisfy its required-ness. |
ab27a55e |
463 | |
1820fffe |
464 | A required attribute must have a C<default>, C<builder> or a |
465 | non-C<undef> C<init_arg> |
ab27a55e |
466 | |
612d3e1a |
467 | =item C<< lazy => Bool >> |
ab27a55e |
468 | |
1820fffe |
469 | A lazy attribute must have a C<default> or C<builder>. When an |
470 | attribute is lazy, the default value will not be calculated until the |
471 | attribute is read. |
93f08899 |
472 | |
612d3e1a |
473 | =item C<< weak_ref => Bool >> |
0fff36e6 |
474 | |
1820fffe |
475 | If this is true, the attribute's value will be stored as a weak |
476 | reference. |
c3398f5b |
477 | |
612d3e1a |
478 | =item C<< auto_deref => Bool >> |
fb706f5c |
479 | |
1820fffe |
480 | If this is true, then the reader will dereference the value when it is |
481 | called. The attribute must have a type constraint which defines the |
482 | attribute as an array or hash reference. |
483 | |
612d3e1a |
484 | =item C<< lazy_build => Bool >> |
1820fffe |
485 | |
486 | Setting this to true makes the attribute lazy and provides a number of |
487 | default methods. |
fb706f5c |
488 | |
1820fffe |
489 | has 'size' => ( |
490 | is => 'ro', |
491 | lazy_build => 1, |
492 | ); |
93d190e0 |
493 | |
1820fffe |
494 | is equivalent to this: |
93d190e0 |
495 | |
1820fffe |
496 | has 'size' => ( |
497 | is => 'ro', |
498 | lazy => 1, |
499 | builder => '_build_size', |
500 | clearer => 'clear_size', |
501 | predicate => 'has_size', |
502 | ); |
93d190e0 |
503 | |
1820fffe |
504 | =back |
505 | |
e5e22afd |
506 | =head2 C<< associate_method(MethodName) >> |
31c5194b |
507 | |
508 | Associates a method with the attribute. Typically, this is called internally |
509 | when an attribute generates its accessors. |
510 | |
e5e22afd |
511 | Currently the argument I<MethodName> is ignored in Mouse. |
31c5194b |
512 | |
1820fffe |
513 | =head2 C<< verify_against_type_constraint(Item) -> TRUE | ERROR >> |
514 | |
515 | Checks that the given value passes this attribute's type constraint. Returns C<true> |
516 | on success, otherwise C<confess>es. |
93d190e0 |
517 | |
1820fffe |
518 | =head2 C<< clone_and_inherit_options(options) -> Mouse::Meta::Attribute >> |
f7b11a21 |
519 | |
612d3e1a |
520 | Creates a new attribute in the owner class, inheriting options from parent classes. |
f7b11a21 |
521 | Accessors and helper methods are installed. Some error checking is done. |
522 | |
9ae9702e |
523 | =head2 C<< get_read_method_ref >> |
524 | |
525 | =head2 C<< get_write_method_ref >> |
526 | |
527 | Returns the subroutine reference of a method suitable for reading or |
528 | writing the attribute's value in the associated class. These methods |
529 | always return a subroutine reference, regardless of whether or not the |
df77fd72 |
530 | attribute is read- or write-only. |
531 | |
1820fffe |
532 | =head1 SEE ALSO |
f7b11a21 |
533 | |
1820fffe |
534 | L<Moose::Meta::Attribute> |
f7b11a21 |
535 | |
31c5194b |
536 | L<Class::MOP::Attribute> |
537 | |
c3398f5b |
538 | =cut |
539 | |