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