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