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