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