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