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