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