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