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