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