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