Document definition_context
[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.78';
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 associated_class   { $_[0]->{'associated_class'}   }
155 sub associated_methods { $_[0]->{'associated_methods'} }
156
157 sub has_accessor    { defined($_[0]->{'accessor'}) }
158 sub has_reader      { defined($_[0]->{'reader'}) }
159 sub has_writer      { defined($_[0]->{'writer'}) }
160 sub has_predicate   { defined($_[0]->{'predicate'}) }
161 sub has_clearer     { defined($_[0]->{'clearer'}) }
162 sub has_builder     { defined($_[0]->{'builder'}) }
163 sub has_init_arg    { defined($_[0]->{'init_arg'}) }
164 sub has_default     { defined($_[0]->{'default'}) }
165 sub has_initializer { defined($_[0]->{'initializer'}) }
166
167 sub accessor           { $_[0]->{'accessor'}    }
168 sub reader             { $_[0]->{'reader'}      }
169 sub writer             { $_[0]->{'writer'}      }
170 sub predicate          { $_[0]->{'predicate'}   }
171 sub clearer            { $_[0]->{'clearer'}     }
172 sub builder            { $_[0]->{'builder'}     }
173 sub init_arg           { $_[0]->{'init_arg'}    }
174 sub initializer        { $_[0]->{'initializer'} }
175 sub definition_context { $_[0]->{'definition_context'} }
176
177 # end bootstrapped away method section.
178 # (all methods below here are kept intact)
179
180 sub has_read_method  { $_[0]->has_reader || $_[0]->has_accessor }
181 sub has_write_method { $_[0]->has_writer || $_[0]->has_accessor }
182
183 sub get_read_method  { 
184     my $self   = shift;    
185     my $reader = $self->reader || $self->accessor;
186     # normal case ...
187     return $reader unless ref $reader;
188     # the HASH ref case
189     my ($name) = %$reader;
190     return $name;
191 }
192
193 sub get_write_method { 
194     my $self   = shift;
195     my $writer = $self->writer || $self->accessor; 
196     # normal case ...
197     return $writer unless ref $writer;
198     # the HASH ref case
199     my ($name) = %$writer;
200     return $name;    
201 }
202
203 sub get_read_method_ref {
204     my $self = shift;
205     if ((my $reader = $self->get_read_method) && $self->associated_class) {   
206         return $self->associated_class->get_method($reader);
207     }
208     else {
209         my $code = sub { $self->get_value(@_) };
210         if (my $class = $self->associated_class) {
211             return $class->method_metaclass->wrap(
212                 $code,
213                 package_name => $class->name,
214                 name         => '__ANON__'
215             );
216         }
217         else {
218             return $code;
219         }
220     }
221 }
222
223 sub get_write_method_ref {
224     my $self = shift;    
225     if ((my $writer = $self->get_write_method) && $self->associated_class) {         
226         return $self->associated_class->get_method($writer);
227     }
228     else {
229         my $code = sub { $self->set_value(@_) };
230         if (my $class = $self->associated_class) {
231             return $class->method_metaclass->wrap(
232                 $code,
233                 package_name => $class->name,
234                 name         => '__ANON__'
235             );
236         }
237         else {
238             return $code;
239         }
240     }
241 }
242
243 sub is_default_a_coderef {
244     ('CODE' eq ref($_[0]->{'default'}))
245 }
246
247 sub default {
248     my ($self, $instance) = @_;
249     if (defined $instance && $self->is_default_a_coderef) {
250         # if the default is a CODE ref, then
251         # we pass in the instance and default
252         # can return a value based on that
253         # instance. Somewhat crude, but works.
254         return $self->{'default'}->($instance);
255     }
256     $self->{'default'};
257 }
258
259 # slots
260
261 sub slots { (shift)->name }
262
263 # class association
264
265 sub attach_to_class {
266     my ($self, $class) = @_;
267     (blessed($class) && $class->isa('Class::MOP::Class'))
268         || confess "You must pass a Class::MOP::Class instance (or a subclass)";
269     weaken($self->{'associated_class'} = $class);
270 }
271
272 sub detach_from_class {
273     my $self = shift;
274     $self->{'associated_class'} = undef;
275 }
276
277 # method association
278
279 sub associate_method {
280     my ($self, $method) = @_;
281     push @{$self->{'associated_methods'}} => $method;
282 }
283
284 ## Slot management
285
286 sub set_initial_value {
287     my ($self, $instance, $value) = @_;
288     $self->_set_initial_slot_value(
289         Class::MOP::Class->initialize(ref($instance))->get_meta_instance,
290         $instance,
291         $value
292     );
293 }
294
295 sub set_value {
296     my ($self, $instance, $value) = @_;
297
298     Class::MOP::Class->initialize(ref($instance))
299                      ->get_meta_instance
300                      ->set_slot_value($instance, $self->name, $value);
301 }
302
303 sub get_value {
304     my ($self, $instance) = @_;
305
306     Class::MOP::Class->initialize(ref($instance))
307                      ->get_meta_instance
308                      ->get_slot_value($instance, $self->name);
309 }
310
311 sub has_value {
312     my ($self, $instance) = @_;
313
314     Class::MOP::Class->initialize(ref($instance))
315                      ->get_meta_instance
316                      ->is_slot_initialized($instance, $self->name);
317 }
318
319 sub clear_value {
320     my ($self, $instance) = @_;
321
322     Class::MOP::Class->initialize(ref($instance))
323                      ->get_meta_instance
324                      ->deinitialize_slot($instance, $self->name);
325 }
326
327 ## load em up ...
328
329 sub accessor_metaclass { 'Class::MOP::Method::Accessor' }
330
331 sub process_accessors {
332     my ($self, $type, $accessor, $generate_as_inline_methods) = @_;
333
334     my $method_ctx;
335
336     if ( my $ctx = $self->definition_context ) {
337         $method_ctx = { %$ctx };
338     }
339
340     if (ref($accessor)) {
341         (ref($accessor) eq 'HASH')
342             || confess "bad accessor/reader/writer/predicate/clearer format, must be a HASH ref";
343         my ($name, $method) = %{$accessor};
344         $method = $self->accessor_metaclass->wrap(
345             $method,
346             package_name => $self->associated_class->name,
347             name         => $name,
348             definition_context => $method_ctx,
349         );
350         $self->associate_method($method);
351         return ($name, $method);
352     }
353     else {
354         my $inline_me = ($generate_as_inline_methods && $self->associated_class->instance_metaclass->is_inlinable);
355         my $method;
356         eval {
357             if ( $method_ctx ) {
358                 my $desc = "accessor $accessor";
359                 if ( $accessor ne $self->name ) {
360                     $desc .= " of attribute " . $self->name;
361                 }
362
363                 $method_ctx->{description} = $desc;
364             }
365
366             $method = $self->accessor_metaclass->new(
367                 attribute     => $self,
368                 is_inline     => $inline_me,
369                 accessor_type => $type,
370                 package_name  => $self->associated_class->name,
371                 name          => $accessor,
372                 definition_context => $method_ctx,
373             );
374         };
375         confess "Could not create the '$type' method for " . $self->name . " because : $@" if $@;
376         $self->associate_method($method);
377         return ($accessor, $method);
378     }
379 }
380
381 sub install_accessors {
382     my $self   = shift;
383     my $inline = shift;
384     my $class  = $self->associated_class;
385
386     $class->add_method(
387         $self->process_accessors('accessor' => $self->accessor(), $inline)
388     ) if $self->has_accessor();
389
390     $class->add_method(
391         $self->process_accessors('reader' => $self->reader(), $inline)
392     ) if $self->has_reader();
393
394     $class->add_method(
395         $self->process_accessors('writer' => $self->writer(), $inline)
396     ) if $self->has_writer();
397
398     $class->add_method(
399         $self->process_accessors('predicate' => $self->predicate(), $inline)
400     ) if $self->has_predicate();
401
402     $class->add_method(
403         $self->process_accessors('clearer' => $self->clearer(), $inline)
404     ) if $self->has_clearer();
405
406     return;
407 }
408
409 {
410     my $_remove_accessor = sub {
411         my ($accessor, $class) = @_;
412         if (ref($accessor) && ref($accessor) eq 'HASH') {
413             ($accessor) = keys %{$accessor};
414         }
415         my $method = $class->get_method($accessor);
416         $class->remove_method($accessor)
417             if (ref($method) && $method->isa('Class::MOP::Method::Accessor'));
418     };
419
420     sub remove_accessors {
421         my $self = shift;
422         # TODO:
423         # we really need to make sure to remove from the
424         # associates methods here as well. But this is
425         # such a slimly used method, I am not worried
426         # about it right now.
427         $_remove_accessor->($self->accessor(),  $self->associated_class()) if $self->has_accessor();
428         $_remove_accessor->($self->reader(),    $self->associated_class()) if $self->has_reader();
429         $_remove_accessor->($self->writer(),    $self->associated_class()) if $self->has_writer();
430         $_remove_accessor->($self->predicate(), $self->associated_class()) if $self->has_predicate();
431         $_remove_accessor->($self->clearer(),   $self->associated_class()) if $self->has_clearer();
432         return;
433     }
434
435 }
436
437 1;
438
439 __END__
440
441 =pod
442
443 =head1 NAME
444
445 Class::MOP::Attribute - Attribute Meta Object
446
447 =head1 SYNOPSIS
448
449   Class::MOP::Attribute->new(
450       foo => (
451           accessor  => 'foo',           # dual purpose get/set accessor
452           predicate => 'has_foo',       # predicate check for defined-ness
453           init_arg  => '-foo',          # class->new will look for a -foo key
454           default   => 'BAR IS BAZ!'    # if no -foo key is provided, use this
455       )
456   );
457
458   Class::MOP::Attribute->new(
459       bar => (
460           reader    => 'bar',           # getter
461           writer    => 'set_bar',       # setter
462           predicate => 'has_bar',       # predicate check for defined-ness
463           init_arg  => ':bar',          # class->new will look for a :bar key
464                                         # no default value means it is undef
465       )
466   );
467
468 =head1 DESCRIPTION
469
470 The Attribute Protocol is almost entirely an invention of
471 C<Class::MOP>. Perl 5 does not have a consistent notion of
472 attributes. There are so many ways in which this is done, and very few
473 (if any) are easily discoverable by this module.
474
475 With that said, this module attempts to inject some order into this
476 chaos, by introducing a consistent API which can be used to create
477 object attributes.
478
479 =head1 METHODS
480
481 =head2 Creation
482
483 =over 4
484
485 =item B<< Class::MOP::Attribute->new($name, ?%options) >>
486
487 An attribute must (at the very least), have a C<$name>. All other
488 C<%options> are added as key-value pairs.
489
490 =over 8
491
492 =item I<init_arg>
493
494 This is a string value representing the expected key in an
495 initialization hash. For instance, if we have an C<init_arg> value of
496 C<-foo>, then the following code will Just Work.
497
498   MyClass->meta->construct_instance( -foo => 'Hello There' );
499
500 If an init_arg is not assigned, it will automatically use the
501 attribute's name. If C<init_arg> is explicitly set to C<undef>, the
502 attribute cannot be specified during initialization.
503
504 =item I<builder>
505
506 This provides the name of a method that will be called to initialize
507 the attribute. This method will be called on the object after it is
508 constructed. It is expected to return a valid value for the attribute.
509
510 =item I<default>
511
512 This can be used to provide an explicit default for initializing the
513 attribute. If the default you provide is a subroutine reference, then
514 this reference will be called I<as a method> on the object.
515
516 If the value is a simple scalar (string or number), then it can be
517 just passed as is. However, if you wish to initialize it with a HASH
518 or ARRAY ref, then you need to wrap that inside a subroutine
519 reference:
520
521   Class::MOP::Attribute->new(
522       'foo' => (
523           default => sub { [] },
524       )
525   );
526
527   # or ...
528
529   Class::MOP::Attribute->new(
530       'foo' => (
531           default => sub { {} },
532       )
533   );
534
535 If you wish to initialize an attribute with a subroutine reference
536 itself, then you need to wrap that in a subroutine as well:
537
538   Class::MOP::Attribute->new(
539       'foo' => (
540           default => sub {
541               sub { print "Hello World" }
542           },
543       )
544   );
545
546 And lastly, if the value of your attribute is dependent upon some
547 other aspect of the instance structure, then you can take advantage of
548 the fact that when the C<default> value is called as a method:
549
550   Class::MOP::Attribute->new(
551       'object_identity' => (
552           default => sub { Scalar::Util::refaddr( $_[0] ) },
553       )
554   );
555
556 Note that there is no guarantee that attributes are initialized in any
557 particular order, so you cannot rely on the value of some other
558 attribute when generating the default.
559
560 =item I<initializer>
561
562 This option can be either a method name or a subroutine
563 reference. This method will be called when setting the attribute's
564 value in the constructor. Unlike C<default> and C<builder>, the
565 initializer is only called when a value is provided to the
566 constructor. The initializer allows you to munge this value during
567 object construction.
568
569 The initializer is called as a method with three arguments. The first
570 is the value that was passed to the constructor. The second is a
571 subroutine reference that can be called to actually set the
572 attribute's value, and the last is the associated
573 C<Class::MOP::Attribute> object.
574
575 This contrived example shows an initializer that sets the attribute to
576 twice the given value.
577
578   Class::MOP::Attribute->new(
579       'doubled' => (
580           initializer => sub {
581               my ( $instance, $value, $set ) = @_;
582               $set->( $value * 2 );
583           },
584       )
585   );
586
587 Since an initializer can be a method name, you can easily make
588 attribute initialization use the writer:
589
590   Class::MOP::Attribute->new(
591       'some_attr' => (
592           writer      => 'some_attr',
593           initializer => 'some_attr',
594       )
595   );
596
597 Your writer will need to examine C<@_> and determine under which
598 context it is being called.
599
600 =back
601
602 The C<accessor>, C<reader>, C<writer>, C<predicate> and C<clearer>
603 options all accept the same parameters. You can provide the name of
604 the method, in which case an appropriate default method will be
605 generated for you. Or instead you can also provide hash reference
606 containing exactly one key (the method name) and one value. The value
607 should be a subroutine reference, which will be installed as the
608 method itself.
609
610 =over 4
611
612 =item I<accessor>
613
614 An C<accessor> is a standard Perl-style read/write accessor. It will
615 return the value of the attribute, and if a value is passed as an
616 argument, it will assign that value to the attribute.
617
618 Note that C<undef> is a legitimate value, so this will work:
619
620   $object->set_something(undef);
621
622 =item I<reader>
623
624 This is a basic read-only accessor. It returns the value of the
625 attribute.
626
627 =item I<writer>
628
629 This is a basic write accessor, it accepts a single argument, and
630 assigns that value to the attribute.
631
632 Note that C<undef> is a legitimate value, so this will work:
633
634   $object->set_something(undef);
635
636 =item I<predicate>
637
638 The predicate method returns a boolean indicating whether or not the
639 attribute has been explicitly set.
640
641 Note that the predicate returns true even if the attribute was set to
642 a false value (C<0> or C<undef>).
643
644 =item I<clearer>
645
646 This method will uninitialize the attribute. After an attribute is
647 cleared, its C<predicate> will return false.
648
649 =item I<definition_context>
650
651 Mostly, this exists as a hook for the benefit of Moose.
652
653 This option should be a hash reference containing several keys which
654 will be used when inlining the attribute's accessors. The keys should
655 include C<line>, the line number where the attribute was created, and
656 either C<file> or C<description>.
657
658 This information will ultimately be used when eval'ing inlined
659 accessor code so that error messages report a useful line and file
660 name.
661
662 =back
663
664 =item B<< $attr->clone(%options) >>
665
666 This clones the attribute. Any options you provide will override the
667 settings of the original attribute. You can change the name of the new
668 attribute by passing a C<name> key in C<%options>.
669
670 =back
671
672 =head2 Informational
673
674 These are all basic read-only accessors for the values passed into
675 the constructor.
676
677 =over 4
678
679 =item B<< $attr->name >>
680
681 =item B<< $attr->accessor >>
682
683 =item B<< $attr->reader >>
684
685 =item B<< $attr->writer >>
686
687 =item B<< $attr->predicate >>
688
689 =item B<< $attr->clearer >>
690
691 The C<accessor>, C<reader>, C<writer>, C<predicate>, and C<clearer>
692 methods all return exactly what was passed to the constructor, so it
693 can be either a string containing a method name, or a hash refrence.
694
695 =item B<< $attr->initializer >>
696
697 Returns the intializer as passed to the constructor, so this may be
698 either a method name or a subroutine reference.
699
700 =item B<< $attr->init_arg >>
701
702 =item B<< $attr->is_default_a_coderef >>
703
704 =item B<< $attr->default($instance) >>
705
706 The C<$instance> argument is optional. If you don't pass it, the
707 return value for this method is exactly what was passed to the
708 constructor, either a simple scalar or a subroutine reference.
709
710 If you I<do> pass an C<$instance> and the default is a subroutine
711 reference, then the reference is called as a method on the
712 C<$instance> and the generated value is returned.
713
714 =item B<< $attr->slots >>
715
716 Return a list of slots required by the attribute. This is usually just
717 one, the name of the attribute.
718
719 A slot is the name of the hash key used to store the attribute in an
720 object instance.
721
722 =item B<< $attr->get_read_method >>
723
724 =item B<< $attr->get_write_method >>
725
726 Returns the name of a method suitable for reading or writing the value
727 of the attribute in the associated class.
728
729 If an attribute is read- or write-only, then these methods can return
730 C<undef> as appropriate.
731
732 =item B<< $attr->has_read_method >>
733
734 =item B<< $attr->has_write_method >>
735
736 This returns a boolean indicating whether the attribute has a I<named>
737 read or write method.
738
739 =item B<< $attr->get_read_method_ref >>
740
741 =item B<< $attr->get_write_method_ref >>
742
743 Returns the subroutine reference of a method suitable for reading or
744 writing the attribute's value in the associated class. These methods
745 always return a subroutine reference, regardless of whether or not the
746 attribute is read- or write-only.
747
748 =back
749
750 =head2 Informational predicates
751
752 These are all basic predicate methods for the values passed into C<new>.
753
754 =over 4
755
756 =item B<< $attr->has_accessor >>
757
758 =item B<< $attr->has_reader >>
759
760 =item B<< $attr->has_writer >>
761
762 =item B<< $attr->has_predicate >>
763
764 =item B<< $attr->has_clearer >>
765
766 =item B<< $attr->has_initializer >>
767
768 =item B<< $attr->has_init_arg >>
769
770 This will be I<false> if the C<init_arg> was set to C<undef>.
771
772 =item B<< $attr->has_default >>
773
774 This will be I<false> if the C<default> was set to C<undef>, since
775 C<undef> is the default C<default> anyway.
776
777 =item B<< $attr->has_builder >>
778
779 =back
780
781 =head2 Value management
782
783 These methods are basically "backdoors" to the instance, and can be
784 used to bypass the regular accessors, but still stay within the MOP.
785
786 These methods are not for general use, and should only be used if you
787 really know what you are doing.
788
789 =over 4
790
791 =item B<< $attr->initialize_instance_slot($meta_instance, $instance, $params) >>
792
793 This method is used internally to initialize the attribute's slot in
794 the object C<$instance>.
795
796 The C<$params> is a hash reference of the values passed to the object
797 constructor.
798
799 It's unlikely that you'll need to call this method yourself.
800
801 =item B<< $attr->set_value($instance, $value) >>
802
803 Sets the value without going through the accessor. Note that this
804 works even with read-only attributes.
805
806 =item B<< $attr->set_initial_value($instance, $value) >>
807
808 Sets the value without going through the accessor. This method is only
809 called when the instance is first being initialized.
810
811 =item B<< $attr->get_value($instance) >>
812
813 Returns the value without going through the accessor. Note that this
814 works even with write-only accessors.
815
816 =item B<< $attr->has_value($instance) >>
817
818 Return a boolean indicating whether the attribute has been set in
819 C<$instance>. This how the default C<predicate> method works.
820
821 =item B<< $attr->clear_value($instance) >>
822
823 This will clear the attribute's value in C<$instance>. This is what
824 the default C<clearer> calls.
825
826 Note that this works even if the attribute does not have any
827 associated read, write or clear methods.
828
829 =back
830
831 =head2 Class association
832
833 These methods allow you to manage the attributes association with
834 the class that contains it. These methods should not be used
835 lightly, nor are they very magical, they are mostly used internally
836 and by metaclass instances.
837
838 =over 4
839
840 =item B<< $attr->associated_class >>
841
842 This returns the C<Class::MOP::Class> with which this attribute is
843 associated, if any.
844
845 =item B<< $attr->attach_to_class($metaclass) >>
846
847 This method stores a weakened reference to the C<$metaclass> object
848 internally.
849
850 This method does not remove the attribute from its old class,
851 nor does it create any accessors in the new class.
852
853 It is probably best to use the L<Class::MOP::Class> C<add_attribute>
854 method instead.
855
856 =item B<< $attr->detach_from_class >>
857
858 This method removes the associate metaclass object from the attribute
859 it has one.
860
861 This method does not remove the attribute itself from the class, or
862 remove its accessors.
863
864 It is probably best to use the L<Class::MOP::Class>
865 C<remove_attribute> method instead.
866
867 =back
868
869 =head2 Attribute Accessor generation
870
871 =over 4
872
873 =item B<< $attr->accessor_metaclass >>
874
875 Accessor methods are generated using an accessor metaclass. By
876 default, this is L<Class::MOP::Method::Accessor>. This method returns
877 the name of the accessor metaclass that this attribute uses.
878
879 =item B<< $attr->associate_method($method) >>
880
881 This associates a L<Class::MOP::Method> object with the
882 attribute. Typically, this is called internally when an attribute
883 generates its accessors.
884
885 =item B<< $attr->associated_methods >>
886
887 This returns the list of methods which have been associated with the
888 attribute.
889
890 =item B<< $attr->install_accessors >>
891
892 This method generates and installs code the attributes various
893 accessors. It is typically called from the L<Class::MOP::Class>
894 C<add_attribute> method.
895
896 This method will call C<< $attr->process_accessors >> for each of the
897 possible method types (accessor, reader, writer & predicate).
898
899 =item B<< $attr->process_accessors($type, $value) >>
900
901 This method takes a C<$type> (accessor, reader, writer or predicate), and
902 a C<$value> (the value passed into the constructor for each of the
903 different types).
904
905 It will then either generate the method itself (using the
906 C<generate_*_method> methods listed below) or it will use the custom
907 method passed through the constructor.
908
909 This method is mostly intended for internal use from the C<<
910 $attr->install_accessors >> method.
911
912 =item B<< $attr->remove_accessors >>
913
914 This method removes all of the accessors associated with the
915 attribute.
916
917 This does not currently remove methods from the list returned by
918 C<associated_methods>.
919
920 =back
921
922 =head2 Introspection
923
924 =over 4
925
926 =item B<< $attr->meta >>
927
928 This will return a L<Class::MOP::Class> instance for this class.
929
930 It should also be noted that L<Class::MOP> will actually bootstrap
931 this module by installing a number of attribute meta-objects into its
932 metaclass.
933
934 =back
935
936 =head1 AUTHORS
937
938 Stevan Little E<lt>stevan@iinteractive.comE<gt>
939
940 =head1 COPYRIGHT AND LICENSE
941
942 Copyright 2006-2009 by Infinity Interactive, Inc.
943
944 L<http://www.iinteractive.com>
945
946 This library is free software; you can redistribute it and/or modify
947 it under the same terms as Perl itself.
948
949 =cut
950
951