Commit | Line | Data |
8b978dd5 |
1 | |
2 | package Class::MOP::Attribute; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
ba38bf08 |
7 | use Class::MOP::Method::Accessor; |
8 | |
2eb717d5 |
9 | use Carp 'confess'; |
9b522fc4 |
10 | use Scalar::Util 'blessed', 'weaken'; |
2eb717d5 |
11 | |
eca95e04 |
12 | our $VERSION = '0.78'; |
d519662a |
13 | $VERSION = eval $VERSION; |
f0480c45 |
14 | our $AUTHORITY = 'cpan:STEVAN'; |
8b978dd5 |
15 | |
b1897d4d |
16 | use base 'Class::MOP::Object'; |
17 | |
727919c5 |
18 | # NOTE: (meta-circularity) |
1d68af04 |
19 | # This method will be replaced in the |
20 | # boostrap section of Class::MOP, by |
21 | # a new version which uses the |
727919c5 |
22 | # &Class::MOP::Class::construct_instance |
23 | # method to build an attribute meta-object |
24 | # which itself is described with attribute |
1d68af04 |
25 | # meta-objects. |
727919c5 |
26 | # - Ain't meta-circularity grand? :) |
8b978dd5 |
27 | sub new { |
649efb63 |
28 | my ( $class, @args ) = @_; |
29 | |
30 | unshift @args, "name" if @args % 2 == 1; |
31 | my %options = @args; |
32 | |
33 | my $name = $options{name}; |
1d68af04 |
34 | |
cbd9f942 |
35 | (defined $name && $name) |
8b978dd5 |
36 | || confess "You must provide a name for the attribute"; |
1d68af04 |
37 | |
38 | $options{init_arg} = $name |
5659d76e |
39 | if not exists $options{init_arg}; |
1d68af04 |
40 | if(exists $options{builder}){ |
41 | confess("builder must be a defined scalar value which is a method name") |
42 | if ref $options{builder} || !(defined $options{builder}); |
43 | confess("Setting both default and builder is not allowed.") |
44 | if exists $options{default}; |
8fe581e5 |
45 | } else { |
46 | (is_default_a_coderef(\%options)) |
47 | || confess("References are not allowed as default values, you must ". |
3c0a8087 |
48 | "wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])") |
8fe581e5 |
49 | if exists $options{default} && ref $options{default}; |
1d68af04 |
50 | } |
2e877f58 |
51 | if( $options{required} and not( defined($options{builder}) || defined($options{init_arg}) || exists $options{default} ) ) { |
52 | confess("A required attribute must have either 'init_arg', 'builder', or 'default'"); |
53 | } |
8683db0e |
54 | |
cb75020f |
55 | $class->_new(\%options); |
4b698b1a |
56 | } |
57 | |
58 | sub _new { |
0bfc85b8 |
59 | my $class = shift; |
60 | my $options = @_ == 1 ? $_[0] : {@_}; |
4b698b1a |
61 | |
8b978dd5 |
62 | bless { |
d9d99689 |
63 | 'name' => $options->{name}, |
64 | 'accessor' => $options->{accessor}, |
65 | 'reader' => $options->{reader}, |
66 | 'writer' => $options->{writer}, |
67 | 'predicate' => $options->{predicate}, |
68 | 'clearer' => $options->{clearer}, |
69 | 'builder' => $options->{builder}, |
70 | 'init_arg' => $options->{init_arg}, |
71 | 'default' => $options->{default}, |
72 | 'initializer' => $options->{initializer}, |
73 | 'definition_context' => $options->{definition_context}, |
1d68af04 |
74 | # keep a weakened link to the |
9ec169fe |
75 | # class we are associated with |
8683db0e |
76 | 'associated_class' => undef, |
1d68af04 |
77 | # and a list of the methods |
3545c727 |
78 | # associated with this attr |
8683db0e |
79 | 'associated_methods' => [], |
0bfc85b8 |
80 | }, $class; |
8b978dd5 |
81 | } |
82 | |
7b31baf4 |
83 | # NOTE: |
1d68af04 |
84 | # this is a primative (and kludgy) clone operation |
16e960bd |
85 | # for now, it will be replaced in the Class::MOP |
1d68af04 |
86 | # bootstrap with a proper one, however we know |
5659d76e |
87 | # that this one will work fine for now. |
88 | sub clone { |
89 | my $self = shift; |
90 | my %options = @_; |
91 | (blessed($self)) |
92 | || confess "Can only clone an instance"; |
b3fa93c7 |
93 | return bless { %{$self}, %options } => ref($self); |
5659d76e |
94 | } |
95 | |
bd4e03f9 |
96 | sub initialize_instance_slot { |
f892c0f0 |
97 | my ($self, $meta_instance, $instance, $params) = @_; |
8683db0e |
98 | my $init_arg = $self->{'init_arg'}; |
99 | |
bd4e03f9 |
100 | # try to fetch the init arg from the %params ... |
8d2d4c67 |
101 | |
1d68af04 |
102 | # if nothing was in the %params, we can use the |
bd4e03f9 |
103 | # attribute's default value (if it has one) |
2e877f58 |
104 | if(defined $init_arg and exists $params->{$init_arg}){ |
8ee74136 |
105 | $self->_set_initial_slot_value( |
106 | $meta_instance, |
0ab65f99 |
107 | $instance, |
0ab65f99 |
108 | $params->{$init_arg}, |
0ab65f99 |
109 | ); |
b7bdffc3 |
110 | } |
8683db0e |
111 | elsif (defined $self->{'default'}) { |
8ee74136 |
112 | $self->_set_initial_slot_value( |
113 | $meta_instance, |
0ab65f99 |
114 | $instance, |
0ab65f99 |
115 | $self->default($instance), |
0ab65f99 |
116 | ); |
b7bdffc3 |
117 | } |
8683db0e |
118 | elsif (defined( my $builder = $self->{'builder'})) { |
b7bdffc3 |
119 | if ($builder = $instance->can($builder)) { |
8ee74136 |
120 | $self->_set_initial_slot_value( |
121 | $meta_instance, |
0ab65f99 |
122 | $instance, |
0ab65f99 |
123 | $instance->$builder, |
0ab65f99 |
124 | ); |
b7bdffc3 |
125 | } |
126 | else { |
b3fa93c7 |
127 | confess(ref($instance)." does not support builder method '". $self->{'builder'} ."' for attribute '" . $self->name . "'"); |
8fe581e5 |
128 | } |
1d68af04 |
129 | } |
bd4e03f9 |
130 | } |
131 | |
8ee74136 |
132 | sub _set_initial_slot_value { |
133 | my ($self, $meta_instance, $instance, $value) = @_; |
134 | |
135 | my $slot_name = $self->name; |
136 | |
137 | return $meta_instance->set_slot_value($instance, $slot_name, $value) |
138 | unless $self->has_initializer; |
139 | |
140 | my $callback = sub { |
141 | $meta_instance->set_slot_value($instance, $slot_name, $_[0]); |
142 | }; |
143 | |
144 | my $initializer = $self->initializer; |
145 | |
146 | # most things will just want to set a value, so make it first arg |
147 | $instance->$initializer($value, $callback, $self); |
148 | } |
149 | |
5659d76e |
150 | # NOTE: |
1d68af04 |
151 | # the next bunch of methods will get bootstrapped |
7b31baf4 |
152 | # away in the Class::MOP bootstrapping section |
153 | |
8683db0e |
154 | sub associated_class { $_[0]->{'associated_class'} } |
155 | sub associated_methods { $_[0]->{'associated_methods'} } |
156 | |
b3fa93c7 |
157 | sub has_accessor { defined($_[0]->{'accessor'}) } |
158 | sub has_reader { defined($_[0]->{'reader'}) } |
159 | sub has_writer { defined($_[0]->{'writer'}) } |
160 | sub has_predicate { defined($_[0]->{'predicate'}) } |
161 | sub has_clearer { defined($_[0]->{'clearer'}) } |
162 | sub has_builder { defined($_[0]->{'builder'}) } |
163 | sub has_init_arg { defined($_[0]->{'init_arg'}) } |
164 | sub has_default { defined($_[0]->{'default'}) } |
165 | sub has_initializer { defined($_[0]->{'initializer'}) } |
8683db0e |
166 | |
d9d99689 |
167 | sub accessor { $_[0]->{'accessor'} } |
168 | sub reader { $_[0]->{'reader'} } |
169 | sub writer { $_[0]->{'writer'} } |
170 | sub predicate { $_[0]->{'predicate'} } |
171 | sub clearer { $_[0]->{'clearer'} } |
172 | sub builder { $_[0]->{'builder'} } |
173 | sub init_arg { $_[0]->{'init_arg'} } |
174 | sub initializer { $_[0]->{'initializer'} } |
175 | sub definition_context { $_[0]->{'definition_context'} } |
c50c603e |
176 | |
7b31baf4 |
177 | # end bootstrapped away method section. |
178 | # (all methods below here are kept intact) |
179 | |
9e517e01 |
180 | sub has_read_method { $_[0]->has_reader || $_[0]->has_accessor } |
181 | sub has_write_method { $_[0]->has_writer || $_[0]->has_accessor } |
182 | |
d14f6cbe |
183 | sub get_read_method { |
184 | my $self = shift; |
185 | my $reader = $self->reader || $self->accessor; |
186 | # normal case ... |
187 | return $reader unless ref $reader; |
188 | # the HASH ref case |
189 | my ($name) = %$reader; |
190 | return $name; |
191 | } |
192 | |
193 | sub get_write_method { |
194 | my $self = shift; |
195 | my $writer = $self->writer || $self->accessor; |
196 | # normal case ... |
197 | return $writer unless ref $writer; |
198 | # the HASH ref case |
199 | my ($name) = %$writer; |
200 | return $name; |
201 | } |
b25109b1 |
202 | |
5da16d1b |
203 | sub get_read_method_ref { |
204 | my $self = shift; |
742fb371 |
205 | if ((my $reader = $self->get_read_method) && $self->associated_class) { |
5da16d1b |
206 | return $self->associated_class->get_method($reader); |
207 | } |
208 | else { |
def5c0b5 |
209 | my $code = sub { $self->get_value(@_) }; |
210 | if (my $class = $self->associated_class) { |
211 | return $class->method_metaclass->wrap( |
212 | $code, |
213 | package_name => $class->name, |
214 | name => '__ANON__' |
215 | ); |
216 | } |
217 | else { |
218 | return $code; |
219 | } |
5da16d1b |
220 | } |
221 | } |
222 | |
223 | sub get_write_method_ref { |
224 | my $self = shift; |
d14f6cbe |
225 | if ((my $writer = $self->get_write_method) && $self->associated_class) { |
742fb371 |
226 | return $self->associated_class->get_method($writer); |
5da16d1b |
227 | } |
228 | else { |
def5c0b5 |
229 | my $code = sub { $self->set_value(@_) }; |
230 | if (my $class = $self->associated_class) { |
231 | return $class->method_metaclass->wrap( |
232 | $code, |
233 | package_name => $class->name, |
234 | name => '__ANON__' |
235 | ); |
236 | } |
237 | else { |
238 | return $code; |
239 | } |
5da16d1b |
240 | } |
241 | } |
242 | |
1d68af04 |
243 | sub is_default_a_coderef { |
4e55a580 |
244 | ('CODE' eq ref($_[0]->{'default'})) |
c0cbf4d9 |
245 | } |
246 | |
1d68af04 |
247 | sub default { |
c0cbf4d9 |
248 | my ($self, $instance) = @_; |
9363ea89 |
249 | if (defined $instance && $self->is_default_a_coderef) { |
1d68af04 |
250 | # if the default is a CODE ref, then |
727919c5 |
251 | # we pass in the instance and default |
1d68af04 |
252 | # can return a value based on that |
727919c5 |
253 | # instance. Somewhat crude, but works. |
8683db0e |
254 | return $self->{'default'}->($instance); |
1d68af04 |
255 | } |
8683db0e |
256 | $self->{'default'}; |
c50c603e |
257 | } |
8b978dd5 |
258 | |
c57c8b10 |
259 | # slots |
260 | |
261 | sub slots { (shift)->name } |
262 | |
1d68af04 |
263 | # class association |
727919c5 |
264 | |
9ec169fe |
265 | sub attach_to_class { |
266 | my ($self, $class) = @_; |
267 | (blessed($class) && $class->isa('Class::MOP::Class')) |
268 | || confess "You must pass a Class::MOP::Class instance (or a subclass)"; |
8683db0e |
269 | weaken($self->{'associated_class'} = $class); |
9ec169fe |
270 | } |
271 | |
272 | sub detach_from_class { |
273 | my $self = shift; |
8683db0e |
274 | $self->{'associated_class'} = undef; |
9ec169fe |
275 | } |
276 | |
1d68af04 |
277 | # method association |
3545c727 |
278 | |
279 | sub associate_method { |
280 | my ($self, $method) = @_; |
8683db0e |
281 | push @{$self->{'associated_methods'}} => $method; |
3545c727 |
282 | } |
283 | |
16e960bd |
284 | ## Slot management |
285 | |
ef91a0e2 |
286 | sub set_initial_value { |
287 | my ($self, $instance, $value) = @_; |
e76b01fb |
288 | $self->_set_initial_slot_value( |
b3fa93c7 |
289 | Class::MOP::Class->initialize(ref($instance))->get_meta_instance, |
8ee74136 |
290 | $instance, |
291 | $value |
292 | ); |
ef91a0e2 |
293 | } |
294 | |
16e960bd |
295 | sub set_value { |
1396f86b |
296 | my ($self, $instance, $value) = @_; |
16e960bd |
297 | |
b3fa93c7 |
298 | Class::MOP::Class->initialize(ref($instance)) |
da34f054 |
299 | ->get_meta_instance |
300 | ->set_slot_value($instance, $self->name, $value); |
16e960bd |
301 | } |
302 | |
303 | sub get_value { |
1396f86b |
304 | my ($self, $instance) = @_; |
16e960bd |
305 | |
b3fa93c7 |
306 | Class::MOP::Class->initialize(ref($instance)) |
da34f054 |
307 | ->get_meta_instance |
308 | ->get_slot_value($instance, $self->name); |
16e960bd |
309 | } |
310 | |
3545c727 |
311 | sub has_value { |
312 | my ($self, $instance) = @_; |
1d68af04 |
313 | |
b3fa93c7 |
314 | Class::MOP::Class->initialize(ref($instance)) |
da34f054 |
315 | ->get_meta_instance |
316 | ->is_slot_initialized($instance, $self->name); |
3545c727 |
317 | } |
318 | |
319 | sub clear_value { |
320 | my ($self, $instance) = @_; |
1d68af04 |
321 | |
b3fa93c7 |
322 | Class::MOP::Class->initialize(ref($instance)) |
da34f054 |
323 | ->get_meta_instance |
324 | ->deinitialize_slot($instance, $self->name); |
3545c727 |
325 | } |
326 | |
ba38bf08 |
327 | ## load em up ... |
c0cbf4d9 |
328 | |
ba38bf08 |
329 | sub accessor_metaclass { 'Class::MOP::Method::Accessor' } |
c0cbf4d9 |
330 | |
9ec169fe |
331 | sub process_accessors { |
c0cbf4d9 |
332 | my ($self, $type, $accessor, $generate_as_inline_methods) = @_; |
d9d99689 |
333 | |
334 | my $method_ctx; |
335 | |
336 | if ( my $ctx = $self->definition_context ) { |
337 | $method_ctx = { %$ctx }; |
338 | } |
339 | |
9b522fc4 |
340 | if (ref($accessor)) { |
341 | (ref($accessor) eq 'HASH') |
7d28758b |
342 | || confess "bad accessor/reader/writer/predicate/clearer format, must be a HASH ref"; |
4d47b77f |
343 | my ($name, $method) = %{$accessor}; |
4c105333 |
344 | $method = $self->accessor_metaclass->wrap( |
345 | $method, |
346 | package_name => $self->associated_class->name, |
347 | name => $name, |
d9d99689 |
348 | definition_context => $method_ctx, |
4c105333 |
349 | ); |
3545c727 |
350 | $self->associate_method($method); |
1d68af04 |
351 | return ($name, $method); |
2eb717d5 |
352 | } |
9ec169fe |
353 | else { |
1d68af04 |
354 | my $inline_me = ($generate_as_inline_methods && $self->associated_class->instance_metaclass->is_inlinable); |
ba38bf08 |
355 | my $method; |
356 | eval { |
d9d99689 |
357 | if ( $method_ctx ) { |
358 | my $desc = "accessor $accessor"; |
359 | if ( $accessor ne $self->name ) { |
360 | $desc .= " of attribute " . $self->name; |
361 | } |
362 | |
363 | $method_ctx->{description} = $desc; |
364 | } |
365 | |
ba38bf08 |
366 | $method = $self->accessor_metaclass->new( |
367 | attribute => $self, |
d90b42a6 |
368 | is_inline => $inline_me, |
ba38bf08 |
369 | accessor_type => $type, |
4c105333 |
370 | package_name => $self->associated_class->name, |
371 | name => $accessor, |
d9d99689 |
372 | definition_context => $method_ctx, |
1d68af04 |
373 | ); |
ba38bf08 |
374 | }; |
1d68af04 |
375 | confess "Could not create the '$type' method for " . $self->name . " because : $@" if $@; |
3545c727 |
376 | $self->associate_method($method); |
ba38bf08 |
377 | return ($accessor, $method); |
1d68af04 |
378 | } |
9ec169fe |
379 | } |
380 | |
381 | sub install_accessors { |
c0cbf4d9 |
382 | my $self = shift; |
383 | my $inline = shift; |
384 | my $class = $self->associated_class; |
1d68af04 |
385 | |
9ec169fe |
386 | $class->add_method( |
c0cbf4d9 |
387 | $self->process_accessors('accessor' => $self->accessor(), $inline) |
9ec169fe |
388 | ) if $self->has_accessor(); |
389 | |
1d68af04 |
390 | $class->add_method( |
c0cbf4d9 |
391 | $self->process_accessors('reader' => $self->reader(), $inline) |
9ec169fe |
392 | ) if $self->has_reader(); |
393 | |
394 | $class->add_method( |
c0cbf4d9 |
395 | $self->process_accessors('writer' => $self->writer(), $inline) |
9ec169fe |
396 | ) if $self->has_writer(); |
397 | |
398 | $class->add_method( |
c0cbf4d9 |
399 | $self->process_accessors('predicate' => $self->predicate(), $inline) |
9ec169fe |
400 | ) if $self->has_predicate(); |
1d68af04 |
401 | |
7d28758b |
402 | $class->add_method( |
403 | $self->process_accessors('clearer' => $self->clearer(), $inline) |
404 | ) if $self->has_clearer(); |
1d68af04 |
405 | |
9ec169fe |
406 | return; |
2eb717d5 |
407 | } |
408 | |
b51af7f9 |
409 | { |
410 | my $_remove_accessor = sub { |
411 | my ($accessor, $class) = @_; |
9b522fc4 |
412 | if (ref($accessor) && ref($accessor) eq 'HASH') { |
c50c603e |
413 | ($accessor) = keys %{$accessor}; |
1d68af04 |
414 | } |
415 | my $method = $class->get_method($accessor); |
416 | $class->remove_method($accessor) |
b3fa93c7 |
417 | if (ref($method) && $method->isa('Class::MOP::Method::Accessor')); |
b51af7f9 |
418 | }; |
1d68af04 |
419 | |
b51af7f9 |
420 | sub remove_accessors { |
9ec169fe |
421 | my $self = shift; |
2367814a |
422 | # TODO: |
1d68af04 |
423 | # we really need to make sure to remove from the |
424 | # associates methods here as well. But this is |
425 | # such a slimly used method, I am not worried |
2367814a |
426 | # about it right now. |
9ec169fe |
427 | $_remove_accessor->($self->accessor(), $self->associated_class()) if $self->has_accessor(); |
428 | $_remove_accessor->($self->reader(), $self->associated_class()) if $self->has_reader(); |
429 | $_remove_accessor->($self->writer(), $self->associated_class()) if $self->has_writer(); |
430 | $_remove_accessor->($self->predicate(), $self->associated_class()) if $self->has_predicate(); |
7d28758b |
431 | $_remove_accessor->($self->clearer(), $self->associated_class()) if $self->has_clearer(); |
1d68af04 |
432 | return; |
b51af7f9 |
433 | } |
434 | |
8b978dd5 |
435 | } |
436 | |
437 | 1; |
438 | |
439 | __END__ |
440 | |
441 | =pod |
442 | |
1d68af04 |
443 | =head1 NAME |
8b978dd5 |
444 | |
445 | Class::MOP::Attribute - Attribute Meta Object |
446 | |
447 | =head1 SYNOPSIS |
1d68af04 |
448 | |
f91a23dd |
449 | Class::MOP::Attribute->new( foo => ( |
fe122940 |
450 | accessor => 'foo', # dual purpose get/set accessor |
1d68af04 |
451 | predicate => 'has_foo' # predicate check for defined-ness |
fe122940 |
452 | init_arg => '-foo', # class->new will look for a -foo key |
453 | default => 'BAR IS BAZ!' # if no -foo key is provided, use this |
8b978dd5 |
454 | )); |
1d68af04 |
455 | |
f91a23dd |
456 | Class::MOP::Attribute->new( bar => ( |
fe122940 |
457 | reader => 'bar', # getter |
1d68af04 |
458 | writer => 'set_bar', # setter |
459 | predicate => 'has_bar' # predicate check for defined-ness |
fe122940 |
460 | init_arg => ':bar', # class->new will look for a :bar key |
8b978dd5 |
461 | # no default value means it is undef |
462 | )); |
463 | |
464 | =head1 DESCRIPTION |
465 | |
fe122940 |
466 | The Attribute Protocol is almost entirely an invention of this module, |
1d68af04 |
467 | and is completely optional to this MOP. This is because Perl 5 does not |
468 | have consistent notion of what is an attribute of a class. There are |
469 | so many ways in which this is done, and very few (if any) are |
fe122940 |
470 | easily discoverable by this module. |
552e3d24 |
471 | |
1d68af04 |
472 | So, all that said, this module attempts to inject some order into this |
473 | chaos, by introducing a consistent API which can be used to create |
fe122940 |
474 | object attributes. |
552e3d24 |
475 | |
476 | =head1 METHODS |
477 | |
478 | =head2 Creation |
479 | |
480 | =over 4 |
481 | |
fe122940 |
482 | =item B<new ($name, ?%options)> |
483 | |
1d68af04 |
484 | An attribute must (at the very least), have a C<$name>. All other |
a2e85e6c |
485 | C<%options> are contained added as key-value pairs. Acceptable keys |
fe122940 |
486 | are as follows: |
487 | |
488 | =over 4 |
489 | |
490 | =item I<init_arg> |
491 | |
1d68af04 |
492 | This should be a string value representing the expected key in |
493 | an initialization hash. For instance, if we have an I<init_arg> |
fe122940 |
494 | value of C<-foo>, then the following code will Just Work. |
495 | |
496 | MyClass->meta->construct_instance(-foo => "Hello There"); |
497 | |
1d68af04 |
498 | In an init_arg is not assigned, it will automatically use the |
0ef07b33 |
499 | value of C<$name>. If an explicit C<undef> is given for an init_arg, |
500 | an attribute value can't be specified during initialization. |
7b31baf4 |
501 | |
1d68af04 |
502 | =item I<builder> |
503 | |
504 | The value of this key is the name of the method that will be |
505 | called to obtain the value used to initialize the attribute. |
506 | This should be a method in the class associated with the attribute, |
507 | not a method in the attribute class itself. |
fe122940 |
508 | |
4c4a6c41 |
509 | =item I<default> |
510 | |
511 | The value of this key is the default value which |
512 | C<Class::MOP::Class::construct_instance> will initialize the |
513 | attribute to. |
514 | |
fe122940 |
515 | B<NOTE:> |
1d68af04 |
516 | If the value is a simple scalar (string or number), then it can |
517 | be just passed as is. However, if you wish to initialize it with |
518 | a HASH or ARRAY ref, then you need to wrap that inside a CODE |
fe122940 |
519 | reference, like so: |
520 | |
521 | Class::MOP::Attribute->new('@foo' => ( |
522 | default => sub { [] }, |
523 | )); |
1d68af04 |
524 | |
525 | # or ... |
526 | |
fe122940 |
527 | Class::MOP::Attribute->new('%foo' => ( |
528 | default => sub { {} }, |
1d68af04 |
529 | )); |
fe122940 |
530 | |
1d68af04 |
531 | If you wish to initialize an attribute with a CODE reference |
fe122940 |
532 | itself, then you need to wrap that in a subroutine as well, like |
533 | so: |
1d68af04 |
534 | |
fe122940 |
535 | Class::MOP::Attribute->new('&foo' => ( |
536 | default => sub { sub { print "Hello World" } }, |
537 | )); |
538 | |
1d68af04 |
539 | And lastly, if the value of your attribute is dependent upon |
540 | some other aspect of the instance structure, then you can take |
541 | advantage of the fact that when the I<default> value is a CODE |
127d39a7 |
542 | reference, it is passed the (as yet unfinished) instance structure |
fe122940 |
543 | as it's only argument. So you can do things like this: |
544 | |
545 | Class::MOP::Attribute->new('$object_identity' => ( |
546 | default => sub { Scalar::Util::refaddr($_[0]) }, |
547 | )); |
548 | |
1d68af04 |
549 | This last feature is fairly limited as there is no gurantee of |
550 | the order of attribute initializations, so you cannot perform |
551 | any kind of dependent initializations. However, if this is |
552 | something you need, you could subclass B<Class::MOP::Class> and |
553 | this class to acheive it. However, this is currently left as |
fe122940 |
554 | an exercise to the reader :). |
555 | |
0ef07b33 |
556 | =item I<initializer> |
557 | |
558 | This may be a method name (referring to a method on the class with this |
559 | attribute) or a CODE ref. The initializer is used to set the attribute value |
560 | on an instance when the attribute is set during instance initialization. When |
561 | called, it is passed the instance (as the invocant), the value to set, a |
562 | slot-setting CODE ref, and the attribute meta-instance. The slot-setting code |
563 | is provided to make it easy to set the (possibly altered) value on the instance |
564 | without going through several more method calls. |
565 | |
0ef07b33 |
566 | This contrived example shows an initializer that sets the attribute to twice |
567 | the given value. |
568 | |
569 | Class::MOP::Attribute->new('$doubled' => ( |
570 | initializer => sub { |
571 | my ($instance, $value, $set) = @_; |
572 | $set->($value * 2); |
573 | }, |
574 | )); |
575 | |
576 | As method names can be given as initializers, one can easily make |
577 | attribute initialization use the writer: |
578 | |
579 | Class::MOP::Attribute->new('$some_attr' => ( |
580 | writer => 'some_attr', |
581 | initializer => 'some_attr', |
582 | )); |
583 | |
127d39a7 |
584 | Your writer will simply need to examine it's C<@_> and determine under |
585 | which context it is being called. |
586 | |
fe122940 |
587 | =back |
588 | |
7d28758b |
589 | The I<accessor>, I<reader>, I<writer>, I<predicate> and I<clearer> keys can |
590 | contain either; the name of the method and an appropriate default one will be |
591 | generated for you, B<or> a HASH ref containing exactly one key (which will be |
592 | used as the name of the method) and one value, which should contain a CODE |
593 | reference which will be installed as the method itself. |
59e7697f |
594 | |
595 | =over 4 |
596 | |
597 | =item I<accessor> |
598 | |
1d68af04 |
599 | The I<accessor> is a standard perl-style read/write accessor. It will |
600 | return the value of the attribute, and if a value is passed as an argument, |
fe122940 |
601 | it will assign that value to the attribute. |
602 | |
603 | B<NOTE:> |
1d68af04 |
604 | This method will properly handle the following code, by assigning an |
fe122940 |
605 | C<undef> value to the attribute. |
606 | |
607 | $object->set_something(undef); |
608 | |
59e7697f |
609 | =item I<reader> |
610 | |
1d68af04 |
611 | This is a basic read-only accessor, it will just return the value of |
fe122940 |
612 | the attribute. |
613 | |
59e7697f |
614 | =item I<writer> |
615 | |
1d68af04 |
616 | This is a basic write accessor, it accepts a single argument, and |
617 | assigns that value to the attribute. This method does not intentially |
618 | return a value, however perl will return the result of the last |
619 | expression in the subroutine, which returns in this returning the |
620 | same value that it was passed. |
59e7697f |
621 | |
fe122940 |
622 | B<NOTE:> |
1d68af04 |
623 | This method will properly handle the following code, by assigning an |
fe122940 |
624 | C<undef> value to the attribute. |
59e7697f |
625 | |
fe122940 |
626 | $object->set_something(); |
627 | |
628 | =item I<predicate> |
629 | |
07dca7e3 |
630 | This is a basic test to see if any value has been set for the |
631 | attribute. It will return true (C<1>) if the attribute has been set |
632 | to any value (even C<undef>), and false (C<0>) otherwise. |
633 | |
634 | B<NOTE:> |
635 | The predicate will return true even when you set an attribute's |
636 | value to C<undef>. This behaviour has changed as of version 0.43. In |
637 | older versions, the predicate (erroneously) checked for attribute |
638 | value definedness, instead of presence as it is now. |
639 | |
640 | If you really want to get rid of the value, you have to define and |
641 | use a I<clearer> (see below). |
642 | |
7d28758b |
643 | =item I<clearer> |
644 | |
645 | This is the a method that will uninitialize the attr, reverting lazy values |
646 | back to their "unfulfilled" state. |
647 | |
59e7697f |
648 | =back |
552e3d24 |
649 | |
bd4e03f9 |
650 | =item B<clone (%options)> |
651 | |
127d39a7 |
652 | This will return a clone of the attribute instance, allowing the overriding |
653 | of various attributes through the C<%options> supplied. |
654 | |
bd4e03f9 |
655 | =item B<initialize_instance_slot ($instance, $params)> |
656 | |
127d39a7 |
657 | This method is used internally to initialize the approriate slot for this |
658 | attribute in a given C<$instance>, the C<$params> passed are those that were |
659 | passed to the constructor. |
660 | |
1d68af04 |
661 | =back |
552e3d24 |
662 | |
16e960bd |
663 | =head2 Value management |
664 | |
1d68af04 |
665 | These methods are basically "backdoors" to the instance, which can be used |
666 | to bypass the regular accessors, but still stay within the context of the MOP. |
2367814a |
667 | |
1d68af04 |
668 | These methods are not for general use, and should only be used if you really |
2367814a |
669 | know what you are doing. |
670 | |
16e960bd |
671 | =over 4 |
672 | |
3545c727 |
673 | =item B<set_value ($instance, $value)> |
16e960bd |
674 | |
675 | Set the value without going through the accessor. Note that this may be done to |
676 | even attributes with just read only accessors. |
677 | |
c0921932 |
678 | =item B<set_initial_value ($instance, $value)> |
679 | |
680 | This method sets the value without going through the accessor -- but it is only |
681 | called when the instance data is first initialized. |
682 | |
3545c727 |
683 | =item B<get_value ($instance)> |
16e960bd |
684 | |
685 | Return the value without going through the accessor. Note that this may be done |
686 | even to attributes with just write only accessors. |
687 | |
3545c727 |
688 | =item B<has_value ($instance)> |
689 | |
92d2abfa |
690 | Return a boolean indicating if the item in the C<$instance> has a value in it. |
2367814a |
691 | This is basically what the default C<predicate> method calls. |
692 | |
3545c727 |
693 | =item B<clear_value ($instance)> |
694 | |
2367814a |
695 | This will clear the value in the C<$instance>. This is basically what the default |
1d68af04 |
696 | C<clearer> would call. Note that this may be done even if the attirbute does not |
2367814a |
697 | have any associated read, write or clear methods. |
698 | |
16e960bd |
699 | =back |
700 | |
552e3d24 |
701 | =head2 Informational |
702 | |
1d68af04 |
703 | These are all basic read-only value accessors for the values |
fe122940 |
704 | passed into C<new>. I think they are pretty much self-explanitory. |
705 | |
552e3d24 |
706 | =over 4 |
707 | |
708 | =item B<name> |
709 | |
710 | =item B<accessor> |
711 | |
712 | =item B<reader> |
713 | |
714 | =item B<writer> |
715 | |
c50c603e |
716 | =item B<predicate> |
717 | |
7d28758b |
718 | =item B<clearer> |
719 | |
0ab65f99 |
720 | =item B<initializer> |
721 | |
552e3d24 |
722 | =item B<init_arg> |
723 | |
495af518 |
724 | =item B<is_default_a_coderef> |
725 | |
fe122940 |
726 | =item B<default (?$instance)> |
727 | |
92d2abfa |
728 | Return the default value for the attribute. |
729 | |
730 | If you pass in an C<$instance> argument to this accessor and the |
731 | I<default> is a CODE reference, then the CODE reference will be |
732 | executed with the C<$instance> as its argument. |
552e3d24 |
733 | |
c57c8b10 |
734 | =item B<slots> |
735 | |
92d2abfa |
736 | Return a list of slots required by the attribute. This is usually |
c57c8b10 |
737 | just one, which is the name of the attribute. |
738 | |
b25109b1 |
739 | =item B<get_read_method> |
740 | |
741 | =item B<get_write_method> |
742 | |
5da16d1b |
743 | Return the name of a method name suitable for reading / writing the value |
744 | of the attribute in the associated class. Suitable for use whether |
745 | C<reader> and C<writer> or C<accessor> was used. |
746 | |
747 | =item B<get_read_method_ref> |
748 | |
749 | =item B<get_write_method_ref> |
750 | |
751 | Return the CODE reference of a method suitable for reading / writing the |
752 | value of the attribute in the associated class. Suitable for use whether |
753 | C<reader> and C<writer> or C<accessor> was specified or not. |
754 | |
127d39a7 |
755 | NOTE: If no reader/writer/accessor was specified, this will use the |
5da16d1b |
756 | attribute get_value/set_value methods, which can be very inefficient. |
b25109b1 |
757 | |
9e517e01 |
758 | =item B<has_read_method> |
759 | |
760 | =item B<has_write_method> |
761 | |
762 | Return whether a method exists suitable for reading / writing the value |
763 | of the attribute in the associated class. Suitable for use whether |
764 | C<reader> and C<writer> or C<accessor> was used. |
765 | |
552e3d24 |
766 | =back |
767 | |
768 | =head2 Informational predicates |
769 | |
a2e85e6c |
770 | These are all basic predicate methods for the values passed into C<new>. |
fe122940 |
771 | |
552e3d24 |
772 | =over 4 |
773 | |
774 | =item B<has_accessor> |
775 | |
552e3d24 |
776 | =item B<has_reader> |
777 | |
552e3d24 |
778 | =item B<has_writer> |
779 | |
c50c603e |
780 | =item B<has_predicate> |
781 | |
7d28758b |
782 | =item B<has_clearer> |
783 | |
0ab65f99 |
784 | =item B<has_initializer> |
785 | |
552e3d24 |
786 | =item B<has_init_arg> |
787 | |
552e3d24 |
788 | =item B<has_default> |
789 | |
bf731086 |
790 | =item B<has_builder> |
791 | |
552e3d24 |
792 | =back |
793 | |
9ec169fe |
794 | =head2 Class association |
795 | |
1d68af04 |
796 | These methods allow you to manage the attributes association with |
797 | the class that contains it. These methods should not be used |
2367814a |
798 | lightly, nor are they very magical, they are mostly used internally |
799 | and by metaclass instances. |
800 | |
9ec169fe |
801 | =over 4 |
802 | |
803 | =item B<associated_class> |
804 | |
2367814a |
805 | This returns the metaclass this attribute is associated with. |
806 | |
9ec169fe |
807 | =item B<attach_to_class ($class)> |
808 | |
1d68af04 |
809 | This will store a weaken reference to C<$class> internally. You should |
2367814a |
810 | note that just changing the class assocation will not remove the attribute |
811 | from it's old class, and initialize it (and it's accessors) in the new |
812 | C<$class>. It is up to you to do this manually. |
813 | |
9ec169fe |
814 | =item B<detach_from_class> |
815 | |
1d68af04 |
816 | This will remove the weakened reference to the class. It does B<not> |
817 | remove the attribute itself from the class (or remove it's accessors), |
818 | you must do that yourself if you want too. Actually if that is what |
819 | you want to do, you should probably be looking at |
2367814a |
820 | L<Class::MOP::Class::remove_attribute> instead. |
821 | |
9ec169fe |
822 | =back |
823 | |
552e3d24 |
824 | =head2 Attribute Accessor generation |
825 | |
826 | =over 4 |
827 | |
ba38bf08 |
828 | =item B<accessor_metaclass> |
829 | |
2367814a |
830 | Accessors are generated by an accessor metaclass, which is usually |
1d68af04 |
831 | a subclass of C<Class::MOP::Method::Accessor>. This method returns |
2367814a |
832 | the name of the accessor metaclass that this attribute uses. |
833 | |
834 | =item B<associate_method ($method)> |
835 | |
1d68af04 |
836 | This will associate a C<$method> with the given attribute which is |
837 | used internally by the accessor generator. |
3545c727 |
838 | |
839 | =item B<associated_methods> |
840 | |
1d68af04 |
841 | This will return the list of methods which have been associated with |
127d39a7 |
842 | the C<associate_method> methods. This is a good way of seeing what |
843 | methods are used to manage a given attribute. |
2367814a |
844 | |
9ec169fe |
845 | =item B<install_accessors> |
2eb717d5 |
846 | |
1d68af04 |
847 | This allows the attribute to generate and install code for it's own |
848 | I<accessor/reader/writer/predicate> methods. This is called by |
fe122940 |
849 | C<Class::MOP::Class::add_attribute>. |
2eb717d5 |
850 | |
1d68af04 |
851 | This method will call C<process_accessors> for each of the possible |
9ec169fe |
852 | method types (accessor, reader, writer & predicate). |
853 | |
854 | =item B<process_accessors ($type, $value)> |
855 | |
1d68af04 |
856 | This takes a C<$type> (accessor, reader, writer or predicate), and |
9ec169fe |
857 | a C<$value> (the value passed into the constructor for each of the |
1d68af04 |
858 | different types). It will then either generate the method itself |
859 | (using the C<generate_*_method> methods listed below) or it will |
860 | use the custom method passed through the constructor. |
9ec169fe |
861 | |
9ec169fe |
862 | =item B<remove_accessors> |
2eb717d5 |
863 | |
1d68af04 |
864 | This allows the attribute to remove the method for it's own |
865 | I<accessor/reader/writer/predicate/clearer>. This is called by |
fe122940 |
866 | C<Class::MOP::Class::remove_attribute>. |
2eb717d5 |
867 | |
1d68af04 |
868 | NOTE: This does not currently remove methods from the list returned |
2367814a |
869 | by C<associated_methods>, that is on the TODO list. |
870 | |
2eb717d5 |
871 | =back |
872 | |
873 | =head2 Introspection |
874 | |
875 | =over 4 |
552e3d24 |
876 | |
2eb717d5 |
877 | =item B<meta> |
552e3d24 |
878 | |
1d68af04 |
879 | This will return a B<Class::MOP::Class> instance which is related |
fe122940 |
880 | to this class. |
881 | |
1d68af04 |
882 | It should also be noted that B<Class::MOP> will actually bootstrap |
883 | this module by installing a number of attribute meta-objects into |
127d39a7 |
884 | it's metaclass. This will allow this class to reap all the benefits |
1d68af04 |
885 | of the MOP when subclassing it. |
fe122940 |
886 | |
552e3d24 |
887 | =back |
888 | |
1a09d9cc |
889 | =head1 AUTHORS |
8b978dd5 |
890 | |
a2e85e6c |
891 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
8b978dd5 |
892 | |
893 | =head1 COPYRIGHT AND LICENSE |
894 | |
69e3ab0a |
895 | Copyright 2006-2008 by Infinity Interactive, Inc. |
8b978dd5 |
896 | |
897 | L<http://www.iinteractive.com> |
898 | |
899 | This library is free software; you can redistribute it and/or modify |
1d68af04 |
900 | it under the same terms as Perl itself. |
8b978dd5 |
901 | |
16e960bd |
902 | =cut |
903 | |
7d28758b |
904 | |