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