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