also remove perl6isms from examples
[gitmo/Class-MOP.git] / lib / Class / MOP / Attribute.pm
CommitLineData
8b978dd5 1
2package Class::MOP::Attribute;
3
4use strict;
5use warnings;
6
ba38bf08 7use Class::MOP::Method::Accessor;
8
2eb717d5 9use Carp 'confess';
9b522fc4 10use Scalar::Util 'blessed', 'weaken';
2eb717d5 11
2e5c1a3f 12our $VERSION = '0.65';
f0480c45 13our $AUTHORITY = 'cpan:STEVAN';
8b978dd5 14
b1897d4d 15use 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 26sub 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.
76sub 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 84sub 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 120sub _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 142sub name { $_[0]->{'name'} }
143
144sub associated_class { $_[0]->{'associated_class'} }
145sub associated_methods { $_[0]->{'associated_methods'} }
146
147sub has_accessor { defined($_[0]->{'accessor'}) ? 1 : 0 }
148sub has_reader { defined($_[0]->{'reader'}) ? 1 : 0 }
149sub has_writer { defined($_[0]->{'writer'}) ? 1 : 0 }
150sub has_predicate { defined($_[0]->{'predicate'}) ? 1 : 0 }
151sub has_clearer { defined($_[0]->{'clearer'}) ? 1 : 0 }
152sub has_builder { defined($_[0]->{'builder'}) ? 1 : 0 }
153sub has_init_arg { defined($_[0]->{'init_arg'}) ? 1 : 0 }
154sub has_default { defined($_[0]->{'default'}) ? 1 : 0 }
155sub has_initializer { defined($_[0]->{'initializer'}) ? 1 : 0 }
156
157sub accessor { $_[0]->{'accessor'} }
158sub reader { $_[0]->{'reader'} }
159sub writer { $_[0]->{'writer'} }
160sub predicate { $_[0]->{'predicate'} }
161sub clearer { $_[0]->{'clearer'} }
162sub builder { $_[0]->{'builder'} }
163sub init_arg { $_[0]->{'init_arg'} }
164sub initializer { $_[0]->{'initializer'} }
c50c603e 165
7b31baf4 166# end bootstrapped away method section.
167# (all methods below here are kept intact)
168
9e517e01 169sub has_read_method { $_[0]->has_reader || $_[0]->has_accessor }
170sub has_write_method { $_[0]->has_writer || $_[0]->has_accessor }
171
d14f6cbe 172sub 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
182sub 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 192sub 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
212sub 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 232sub is_default_a_coderef {
8683db0e 233 ('CODE' eq ref($_[0]->{'default'} || $_[0]->{default}))
c0cbf4d9 234}
235
1d68af04 236sub 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
250sub slots { (shift)->name }
251
1d68af04 252# class association
727919c5 253
9ec169fe 254sub 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
261sub detach_from_class {
262 my $self = shift;
8683db0e 263 $self->{'associated_class'} = undef;
9ec169fe 264}
265
1d68af04 266# method association
3545c727 267
268sub associate_method {
269 my ($self, $method) = @_;
8683db0e 270 push @{$self->{'associated_methods'}} => $method;
3545c727 271}
272
16e960bd 273## Slot management
274
ef91a0e2 275sub 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 284sub 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
292sub 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 300sub 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
308sub 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 318sub accessor_metaclass { 'Class::MOP::Method::Accessor' }
c0cbf4d9 319
9ec169fe 320sub 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
352sub 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
4081;
409
410__END__
411
412=pod
413
1d68af04 414=head1 NAME
8b978dd5 415
416Class::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 437The Attribute Protocol is almost entirely an invention of this module,
1d68af04 438and is completely optional to this MOP. This is because Perl 5 does not
439have consistent notion of what is an attribute of a class. There are
440so many ways in which this is done, and very few (if any) are
fe122940 441easily discoverable by this module.
552e3d24 442
1d68af04 443So, all that said, this module attempts to inject some order into this
444chaos, by introducing a consistent API which can be used to create
fe122940 445object attributes.
552e3d24 446
447=head1 METHODS
448
449=head2 Creation
450
451=over 4
452
fe122940 453=item B<new ($name, ?%options)>
454
1d68af04 455An attribute must (at the very least), have a C<$name>. All other
a2e85e6c 456C<%options> are contained added as key-value pairs. Acceptable keys
fe122940 457are as follows:
458
459=over 4
460
461=item I<init_arg>
462
1d68af04 463This should be a string value representing the expected key in
464an initialization hash. For instance, if we have an I<init_arg>
fe122940 465value of C<-foo>, then the following code will Just Work.
466
467 MyClass->meta->construct_instance(-foo => "Hello There");
468
1d68af04 469In an init_arg is not assigned, it will automatically use the
0ef07b33 470value of C<$name>. If an explicit C<undef> is given for an init_arg,
471an attribute value can't be specified during initialization.
7b31baf4 472
1d68af04 473=item I<builder>
474
475The value of this key is the name of the method that will be
476called to obtain the value used to initialize the attribute.
477This should be a method in the class associated with the attribute,
478not a method in the attribute class itself.
fe122940 479
4c4a6c41 480=item I<default>
481
482The value of this key is the default value which
483C<Class::MOP::Class::construct_instance> will initialize the
484attribute to.
485
fe122940 486B<NOTE:>
1d68af04 487If the value is a simple scalar (string or number), then it can
488be just passed as is. However, if you wish to initialize it with
489a HASH or ARRAY ref, then you need to wrap that inside a CODE
fe122940 490reference, 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 502If you wish to initialize an attribute with a CODE reference
fe122940 503itself, then you need to wrap that in a subroutine as well, like
504so:
1d68af04 505
fe122940 506 Class::MOP::Attribute->new('&foo' => (
507 default => sub { sub { print "Hello World" } },
508 ));
509
1d68af04 510And lastly, if the value of your attribute is dependent upon
511some other aspect of the instance structure, then you can take
512advantage of the fact that when the I<default> value is a CODE
127d39a7 513reference, it is passed the (as yet unfinished) instance structure
fe122940 514as 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 520This last feature is fairly limited as there is no gurantee of
521the order of attribute initializations, so you cannot perform
522any kind of dependent initializations. However, if this is
523something you need, you could subclass B<Class::MOP::Class> and
524this class to acheive it. However, this is currently left as
fe122940 525an exercise to the reader :).
526
0ef07b33 527=item I<initializer>
528
529This may be a method name (referring to a method on the class with this
530attribute) or a CODE ref. The initializer is used to set the attribute value
531on an instance when the attribute is set during instance initialization. When
532called, it is passed the instance (as the invocant), the value to set, a
533slot-setting CODE ref, and the attribute meta-instance. The slot-setting code
534is provided to make it easy to set the (possibly altered) value on the instance
535without going through several more method calls.
536
0ef07b33 537This contrived example shows an initializer that sets the attribute to twice
538the given value.
539
540 Class::MOP::Attribute->new('$doubled' => (
541 initializer => sub {
542 my ($instance, $value, $set) = @_;
543 $set->($value * 2);
544 },
545 ));
546
547As method names can be given as initializers, one can easily make
548attribute initialization use the writer:
549
550 Class::MOP::Attribute->new('$some_attr' => (
551 writer => 'some_attr',
552 initializer => 'some_attr',
553 ));
554
127d39a7 555Your writer will simply need to examine it's C<@_> and determine under
556which context it is being called.
557
fe122940 558=back
559
7d28758b 560The I<accessor>, I<reader>, I<writer>, I<predicate> and I<clearer> keys can
561contain either; the name of the method and an appropriate default one will be
562generated for you, B<or> a HASH ref containing exactly one key (which will be
563used as the name of the method) and one value, which should contain a CODE
564reference which will be installed as the method itself.
59e7697f 565
566=over 4
567
568=item I<accessor>
569
1d68af04 570The I<accessor> is a standard perl-style read/write accessor. It will
571return the value of the attribute, and if a value is passed as an argument,
fe122940 572it will assign that value to the attribute.
573
574B<NOTE:>
1d68af04 575This method will properly handle the following code, by assigning an
fe122940 576C<undef> value to the attribute.
577
578 $object->set_something(undef);
579
59e7697f 580=item I<reader>
581
1d68af04 582This is a basic read-only accessor, it will just return the value of
fe122940 583the attribute.
584
59e7697f 585=item I<writer>
586
1d68af04 587This is a basic write accessor, it accepts a single argument, and
588assigns that value to the attribute. This method does not intentially
589return a value, however perl will return the result of the last
590expression in the subroutine, which returns in this returning the
591same value that it was passed.
59e7697f 592
fe122940 593B<NOTE:>
1d68af04 594This method will properly handle the following code, by assigning an
fe122940 595C<undef> value to the attribute.
59e7697f 596
fe122940 597 $object->set_something();
598
599=item I<predicate>
600
07dca7e3 601This is a basic test to see if any value has been set for the
602attribute. It will return true (C<1>) if the attribute has been set
603to any value (even C<undef>), and false (C<0>) otherwise.
604
605B<NOTE:>
606The predicate will return true even when you set an attribute's
607value to C<undef>. This behaviour has changed as of version 0.43. In
608older versions, the predicate (erroneously) checked for attribute
609value definedness, instead of presence as it is now.
610
611If you really want to get rid of the value, you have to define and
612use a I<clearer> (see below).
613
7d28758b 614=item I<clearer>
615
616This is the a method that will uninitialize the attr, reverting lazy values
617back to their "unfulfilled" state.
618
59e7697f 619=back
552e3d24 620
bd4e03f9 621=item B<clone (%options)>
622
127d39a7 623This will return a clone of the attribute instance, allowing the overriding
624of various attributes through the C<%options> supplied.
625
bd4e03f9 626=item B<initialize_instance_slot ($instance, $params)>
627
127d39a7 628This method is used internally to initialize the approriate slot for this
629attribute in a given C<$instance>, the C<$params> passed are those that were
630passed to the constructor.
631
1d68af04 632=back
552e3d24 633
16e960bd 634=head2 Value management
635
1d68af04 636These methods are basically "backdoors" to the instance, which can be used
637to bypass the regular accessors, but still stay within the context of the MOP.
2367814a 638
1d68af04 639These methods are not for general use, and should only be used if you really
2367814a 640know what you are doing.
641
16e960bd 642=over 4
643
3545c727 644=item B<set_value ($instance, $value)>
16e960bd 645
646Set the value without going through the accessor. Note that this may be done to
647even attributes with just read only accessors.
648
c0921932 649=item B<set_initial_value ($instance, $value)>
650
651This method sets the value without going through the accessor -- but it is only
652called when the instance data is first initialized.
653
3545c727 654=item B<get_value ($instance)>
16e960bd 655
656Return the value without going through the accessor. Note that this may be done
657even to attributes with just write only accessors.
658
3545c727 659=item B<has_value ($instance)>
660
92d2abfa 661Return a boolean indicating if the item in the C<$instance> has a value in it.
2367814a 662This is basically what the default C<predicate> method calls.
663
3545c727 664=item B<clear_value ($instance)>
665
2367814a 666This will clear the value in the C<$instance>. This is basically what the default
1d68af04 667C<clearer> would call. Note that this may be done even if the attirbute does not
2367814a 668have any associated read, write or clear methods.
669
16e960bd 670=back
671
552e3d24 672=head2 Informational
673
1d68af04 674These are all basic read-only value accessors for the values
fe122940 675passed 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 699Return the default value for the attribute.
700
701If you pass in an C<$instance> argument to this accessor and the
702I<default> is a CODE reference, then the CODE reference will be
703executed with the C<$instance> as its argument.
552e3d24 704
c57c8b10 705=item B<slots>
706
92d2abfa 707Return a list of slots required by the attribute. This is usually
c57c8b10 708just 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 714Return the name of a method name suitable for reading / writing the value
715of the attribute in the associated class. Suitable for use whether
716C<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
722Return the CODE reference of a method suitable for reading / writing the
723value of the attribute in the associated class. Suitable for use whether
724C<reader> and C<writer> or C<accessor> was specified or not.
725
127d39a7 726NOTE: If no reader/writer/accessor was specified, this will use the
5da16d1b 727attribute 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
733Return whether a method exists suitable for reading / writing the value
734of the attribute in the associated class. Suitable for use whether
735C<reader> and C<writer> or C<accessor> was used.
736
552e3d24 737=back
738
739=head2 Informational predicates
740
a2e85e6c 741These 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 767These methods allow you to manage the attributes association with
768the class that contains it. These methods should not be used
2367814a 769lightly, nor are they very magical, they are mostly used internally
770and by metaclass instances.
771
9ec169fe 772=over 4
773
774=item B<associated_class>
775
2367814a 776This returns the metaclass this attribute is associated with.
777
9ec169fe 778=item B<attach_to_class ($class)>
779
1d68af04 780This will store a weaken reference to C<$class> internally. You should
2367814a 781note that just changing the class assocation will not remove the attribute
782from it's old class, and initialize it (and it's accessors) in the new
783C<$class>. It is up to you to do this manually.
784
9ec169fe 785=item B<detach_from_class>
786
1d68af04 787This will remove the weakened reference to the class. It does B<not>
788remove the attribute itself from the class (or remove it's accessors),
789you must do that yourself if you want too. Actually if that is what
790you want to do, you should probably be looking at
2367814a 791L<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 801Accessors are generated by an accessor metaclass, which is usually
1d68af04 802a subclass of C<Class::MOP::Method::Accessor>. This method returns
2367814a 803the name of the accessor metaclass that this attribute uses.
804
805=item B<associate_method ($method)>
806
1d68af04 807This will associate a C<$method> with the given attribute which is
808used internally by the accessor generator.
3545c727 809
810=item B<associated_methods>
811
1d68af04 812This will return the list of methods which have been associated with
127d39a7 813the C<associate_method> methods. This is a good way of seeing what
814methods are used to manage a given attribute.
2367814a 815
9ec169fe 816=item B<install_accessors>
2eb717d5 817
1d68af04 818This allows the attribute to generate and install code for it's own
819I<accessor/reader/writer/predicate> methods. This is called by
fe122940 820C<Class::MOP::Class::add_attribute>.
2eb717d5 821
1d68af04 822This method will call C<process_accessors> for each of the possible
9ec169fe 823method types (accessor, reader, writer & predicate).
824
825=item B<process_accessors ($type, $value)>
826
1d68af04 827This takes a C<$type> (accessor, reader, writer or predicate), and
9ec169fe 828a C<$value> (the value passed into the constructor for each of the
1d68af04 829different types). It will then either generate the method itself
830(using the C<generate_*_method> methods listed below) or it will
831use the custom method passed through the constructor.
9ec169fe 832
9ec169fe 833=item B<remove_accessors>
2eb717d5 834
1d68af04 835This allows the attribute to remove the method for it's own
836I<accessor/reader/writer/predicate/clearer>. This is called by
fe122940 837C<Class::MOP::Class::remove_attribute>.
2eb717d5 838
1d68af04 839NOTE: This does not currently remove methods from the list returned
2367814a 840by 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 850This will return a B<Class::MOP::Class> instance which is related
fe122940 851to this class.
852
1d68af04 853It should also be noted that B<Class::MOP> will actually bootstrap
854this module by installing a number of attribute meta-objects into
127d39a7 855it's metaclass. This will allow this class to reap all the benefits
1d68af04 856of the MOP when subclassing it.
fe122940 857
552e3d24 858=back
859
1a09d9cc 860=head1 AUTHORS
8b978dd5 861
a2e85e6c 862Stevan Little E<lt>stevan@iinteractive.comE<gt>
8b978dd5 863
864=head1 COPYRIGHT AND LICENSE
865
69e3ab0a 866Copyright 2006-2008 by Infinity Interactive, Inc.
8b978dd5 867
868L<http://www.iinteractive.com>
869
870This library is free software; you can redistribute it and/or modify
1d68af04 871it under the same terms as Perl itself.
8b978dd5 872
16e960bd 873=cut
874
7d28758b 875