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