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