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