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
6c9f331d 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";
6c9f331d 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
9ec169fe 315sub process_accessors {
c7e28c19 316 Carp::cluck('The process_accessors method has been made private.'
317 . " The public version is deprecated and will be removed in a future release.\n");
e056bd42 318 shift->_process_accessors(@_);
45a183fb 319}
320
321sub _process_accessors {
c0cbf4d9 322 my ($self, $type, $accessor, $generate_as_inline_methods) = @_;
d9d99689 323
324 my $method_ctx;
325
326 if ( my $ctx = $self->definition_context ) {
327 $method_ctx = { %$ctx };
328 }
329
46b1358f 330 my $metaclass = $self->associated_class;
331
9b522fc4 332 if (ref($accessor)) {
333 (ref($accessor) eq 'HASH')
7d28758b 334 || confess "bad accessor/reader/writer/predicate/clearer format, must be a HASH ref";
4d47b77f 335 my ($name, $method) = %{$accessor};
46b1358f 336
4c105333 337 $method = $self->accessor_metaclass->wrap(
46b1358f 338 body => $method,
339 associated_metaclass => $metaclass,
340 package_name => $metaclass->name,
341 name => $name,
342 definition_context => $method_ctx,
4c105333 343 );
3545c727 344 $self->associate_method($method);
1d68af04 345 return ($name, $method);
2eb717d5 346 }
9ec169fe 347 else {
ba38bf08 348 my $method;
349 eval {
d9d99689 350 if ( $method_ctx ) {
351 my $desc = "accessor $accessor";
352 if ( $accessor ne $self->name ) {
353 $desc .= " of attribute " . $self->name;
354 }
355
356 $method_ctx->{description} = $desc;
357 }
358
ba38bf08 359 $method = $self->accessor_metaclass->new(
46b1358f 360 attribute => $self,
361 accessor_type => $type,
362 associated_metaclass => $metaclass,
363 package_name => $metaclass->name,
364 name => $accessor,
365 definition_context => $method_ctx,
d26cfd26 366 is_inline => $metaclass->instance_metaclass->is_inlinable,
1d68af04 367 );
ba38bf08 368 };
1d68af04 369 confess "Could not create the '$type' method for " . $self->name . " because : $@" if $@;
3545c727 370 $self->associate_method($method);
ba38bf08 371 return ($accessor, $method);
1d68af04 372 }
9ec169fe 373}
374
375sub install_accessors {
c0cbf4d9 376 my $self = shift;
377 my $inline = shift;
378 my $class = $self->associated_class;
1d68af04 379
9ec169fe 380 $class->add_method(
45a183fb 381 $self->_process_accessors('accessor' => $self->accessor(), $inline)
9ec169fe 382 ) if $self->has_accessor();
383
1d68af04 384 $class->add_method(
45a183fb 385 $self->_process_accessors('reader' => $self->reader(), $inline)
9ec169fe 386 ) if $self->has_reader();
387
388 $class->add_method(
45a183fb 389 $self->_process_accessors('writer' => $self->writer(), $inline)
9ec169fe 390 ) if $self->has_writer();
391
392 $class->add_method(
45a183fb 393 $self->_process_accessors('predicate' => $self->predicate(), $inline)
9ec169fe 394 ) if $self->has_predicate();
1d68af04 395
7d28758b 396 $class->add_method(
45a183fb 397 $self->_process_accessors('clearer' => $self->clearer(), $inline)
7d28758b 398 ) if $self->has_clearer();
1d68af04 399
9ec169fe 400 return;
2eb717d5 401}
402
b51af7f9 403{
404 my $_remove_accessor = sub {
405 my ($accessor, $class) = @_;
9b522fc4 406 if (ref($accessor) && ref($accessor) eq 'HASH') {
c50c603e 407 ($accessor) = keys %{$accessor};
1d68af04 408 }
409 my $method = $class->get_method($accessor);
410 $class->remove_method($accessor)
b3fa93c7 411 if (ref($method) && $method->isa('Class::MOP::Method::Accessor'));
b51af7f9 412 };
1d68af04 413
b51af7f9 414 sub remove_accessors {
9ec169fe 415 my $self = shift;
2367814a 416 # TODO:
1d68af04 417 # we really need to make sure to remove from the
418 # associates methods here as well. But this is
419 # such a slimly used method, I am not worried
2367814a 420 # about it right now.
9ec169fe 421 $_remove_accessor->($self->accessor(), $self->associated_class()) if $self->has_accessor();
422 $_remove_accessor->($self->reader(), $self->associated_class()) if $self->has_reader();
423 $_remove_accessor->($self->writer(), $self->associated_class()) if $self->has_writer();
424 $_remove_accessor->($self->predicate(), $self->associated_class()) if $self->has_predicate();
7d28758b 425 $_remove_accessor->($self->clearer(), $self->associated_class()) if $self->has_clearer();
1d68af04 426 return;
b51af7f9 427 }
428
8b978dd5 429}
430
4311;
432
433__END__
434
435=pod
436
1d68af04 437=head1 NAME
8b978dd5 438
439Class::MOP::Attribute - Attribute Meta Object
440
441=head1 SYNOPSIS
1d68af04 442
2e23f7dc 443 Class::MOP::Attribute->new(
444 foo => (
445 accessor => 'foo', # dual purpose get/set accessor
446 predicate => 'has_foo', # predicate check for defined-ness
447 init_arg => '-foo', # class->new will look for a -foo key
448 default => 'BAR IS BAZ!' # if no -foo key is provided, use this
449 )
450 );
451
452 Class::MOP::Attribute->new(
453 bar => (
454 reader => 'bar', # getter
455 writer => 'set_bar', # setter
456 predicate => 'has_bar', # predicate check for defined-ness
457 init_arg => ':bar', # class->new will look for a :bar key
458 # no default value means it is undef
459 )
460 );
8b978dd5 461
462=head1 DESCRIPTION
463
2e23f7dc 464The Attribute Protocol is almost entirely an invention of
465C<Class::MOP>. Perl 5 does not have a consistent notion of
466attributes. There are so many ways in which this is done, and very few
467(if any) are easily discoverable by this module.
552e3d24 468
2e23f7dc 469With that said, this module attempts to inject some order into this
1d68af04 470chaos, by introducing a consistent API which can be used to create
fe122940 471object attributes.
552e3d24 472
473=head1 METHODS
474
475=head2 Creation
476
477=over 4
478
2e23f7dc 479=item B<< Class::MOP::Attribute->new($name, ?%options) >>
fe122940 480
1d68af04 481An attribute must (at the very least), have a C<$name>. All other
2e23f7dc 482C<%options> are added as key-value pairs.
fe122940 483
2e23f7dc 484=over 8
fe122940 485
76187047 486=item * init_arg
fe122940 487
2e23f7dc 488This is a string value representing the expected key in an
489initialization hash. For instance, if we have an C<init_arg> value of
490C<-foo>, then the following code will Just Work.
fe122940 491
d69fb6b3 492 MyClass->meta->new_object( -foo => 'Hello There' );
fe122940 493
2e23f7dc 494If an init_arg is not assigned, it will automatically use the
495attribute's name. If C<init_arg> is explicitly set to C<undef>, the
496attribute cannot be specified during initialization.
7b31baf4 497
76187047 498=item * builder
1d68af04 499
2e23f7dc 500This provides the name of a method that will be called to initialize
501the attribute. This method will be called on the object after it is
502constructed. It is expected to return a valid value for the attribute.
fe122940 503
76187047 504=item * default
4c4a6c41 505
2e23f7dc 506This can be used to provide an explicit default for initializing the
507attribute. If the default you provide is a subroutine reference, then
508this reference will be called I<as a method> on the object.
4c4a6c41 509
2e23f7dc 510If the value is a simple scalar (string or number), then it can be
511just passed as is. However, if you wish to initialize it with a HASH
512or ARRAY ref, then you need to wrap that inside a subroutine
513reference:
fe122940 514
2e23f7dc 515 Class::MOP::Attribute->new(
516 'foo' => (
517 default => sub { [] },
518 )
519 );
1d68af04 520
521 # or ...
522
2e23f7dc 523 Class::MOP::Attribute->new(
524 'foo' => (
525 default => sub { {} },
526 )
527 );
528
529If you wish to initialize an attribute with a subroutine reference
530itself, then you need to wrap that in a subroutine as well:
531
532 Class::MOP::Attribute->new(
533 'foo' => (
534 default => sub {
535 sub { print "Hello World" }
536 },
537 )
538 );
539
540And lastly, if the value of your attribute is dependent upon some
541other aspect of the instance structure, then you can take advantage of
542the fact that when the C<default> value is called as a method:
543
544 Class::MOP::Attribute->new(
545 'object_identity' => (
546 default => sub { Scalar::Util::refaddr( $_[0] ) },
547 )
548 );
549
550Note that there is no guarantee that attributes are initialized in any
551particular order, so you cannot rely on the value of some other
552attribute when generating the default.
fe122940 553
76187047 554=item * initializer
0ef07b33 555
2e23f7dc 556This option can be either a method name or a subroutine
557reference. This method will be called when setting the attribute's
558value in the constructor. Unlike C<default> and C<builder>, the
559initializer is only called when a value is provided to the
560constructor. The initializer allows you to munge this value during
561object construction.
562
563The initializer is called as a method with three arguments. The first
564is the value that was passed to the constructor. The second is a
565subroutine reference that can be called to actually set the
566attribute's value, and the last is the associated
567C<Class::MOP::Attribute> object.
568
569This contrived example shows an initializer that sets the attribute to
570twice the given value.
571
572 Class::MOP::Attribute->new(
573 'doubled' => (
574 initializer => sub {
575 my ( $instance, $value, $set ) = @_;
576 $set->( $value * 2 );
577 },
578 )
579 );
580
581Since an initializer can be a method name, you can easily make
0ef07b33 582attribute initialization use the writer:
583
2e23f7dc 584 Class::MOP::Attribute->new(
585 'some_attr' => (
586 writer => 'some_attr',
587 initializer => 'some_attr',
588 )
589 );
0ef07b33 590
2e23f7dc 591Your writer will need to examine C<@_> and determine under which
592context it is being called.
127d39a7 593
fe122940 594=back
595
2e23f7dc 596The C<accessor>, C<reader>, C<writer>, C<predicate> and C<clearer>
597options all accept the same parameters. You can provide the name of
598the method, in which case an appropriate default method will be
599generated for you. Or instead you can also provide hash reference
600containing exactly one key (the method name) and one value. The value
601should be a subroutine reference, which will be installed as the
602method itself.
59e7697f 603
76187047 604=over 8
59e7697f 605
76187047 606=item * accessor
59e7697f 607
2e23f7dc 608An C<accessor> is a standard Perl-style read/write accessor. It will
609return the value of the attribute, and if a value is passed as an
610argument, it will assign that value to the attribute.
fe122940 611
2e23f7dc 612Note that C<undef> is a legitimate value, so this will work:
fe122940 613
614 $object->set_something(undef);
615
76187047 616=item * reader
59e7697f 617
2e23f7dc 618This is a basic read-only accessor. It returns the value of the
619attribute.
fe122940 620
76187047 621=item * writer
59e7697f 622
1d68af04 623This is a basic write accessor, it accepts a single argument, and
2e23f7dc 624assigns that value to the attribute.
59e7697f 625
2e23f7dc 626Note that C<undef> is a legitimate value, so this will work:
59e7697f 627
2e23f7dc 628 $object->set_something(undef);
fe122940 629
76187047 630=item * predicate
fe122940 631
2e23f7dc 632The predicate method returns a boolean indicating whether or not the
633attribute has been explicitly set.
07dca7e3 634
2e23f7dc 635Note that the predicate returns true even if the attribute was set to
636a false value (C<0> or C<undef>).
07dca7e3 637
76187047 638=item * clearer
7d28758b 639
2e23f7dc 640This method will uninitialize the attribute. After an attribute is
641cleared, its C<predicate> will return false.
7d28758b 642
76187047 643=item * definition_context
f8813817 644
645Mostly, this exists as a hook for the benefit of Moose.
646
647This option should be a hash reference containing several keys which
648will be used when inlining the attribute's accessors. The keys should
649include C<line>, the line number where the attribute was created, and
650either C<file> or C<description>.
651
652This information will ultimately be used when eval'ing inlined
653accessor code so that error messages report a useful line and file
654name.
655
59e7697f 656=back
552e3d24 657
2e23f7dc 658=item B<< $attr->clone(%options) >>
bd4e03f9 659
2e23f7dc 660This clones the attribute. Any options you provide will override the
661settings of the original attribute. You can change the name of the new
662attribute by passing a C<name> key in C<%options>.
127d39a7 663
2e23f7dc 664=back
bd4e03f9 665
2e23f7dc 666=head2 Informational
127d39a7 667
2e23f7dc 668These are all basic read-only accessors for the values passed into
669the constructor.
552e3d24 670
2e23f7dc 671=over 4
16e960bd 672
2e23f7dc 673=item B<< $attr->name >>
2367814a 674
76187047 675Returns the attribute's name.
676
2e23f7dc 677=item B<< $attr->accessor >>
2367814a 678
2e23f7dc 679=item B<< $attr->reader >>
16e960bd 680
2e23f7dc 681=item B<< $attr->writer >>
16e960bd 682
2e23f7dc 683=item B<< $attr->predicate >>
16e960bd 684
2e23f7dc 685=item B<< $attr->clearer >>
c0921932 686
2e23f7dc 687The C<accessor>, C<reader>, C<writer>, C<predicate>, and C<clearer>
688methods all return exactly what was passed to the constructor, so it
a6710c60 689can be either a string containing a method name, or a hash reference.
c0921932 690
2e23f7dc 691=item B<< $attr->initializer >>
16e960bd 692
a6710c60 693Returns the initializer as passed to the constructor, so this may be
2e23f7dc 694either a method name or a subroutine reference.
16e960bd 695
2e23f7dc 696=item B<< $attr->init_arg >>
3545c727 697
2e23f7dc 698=item B<< $attr->is_default_a_coderef >>
2367814a 699
2e23f7dc 700=item B<< $attr->default($instance) >>
3545c727 701
2e23f7dc 702The C<$instance> argument is optional. If you don't pass it, the
703return value for this method is exactly what was passed to the
704constructor, either a simple scalar or a subroutine reference.
2367814a 705
2e23f7dc 706If you I<do> pass an C<$instance> and the default is a subroutine
707reference, then the reference is called as a method on the
708C<$instance> and the generated value is returned.
16e960bd 709
2e23f7dc 710=item B<< $attr->slots >>
552e3d24 711
2e23f7dc 712Return a list of slots required by the attribute. This is usually just
713one, the name of the attribute.
fe122940 714
2e23f7dc 715A slot is the name of the hash key used to store the attribute in an
716object instance.
552e3d24 717
2e23f7dc 718=item B<< $attr->get_read_method >>
552e3d24 719
2e23f7dc 720=item B<< $attr->get_write_method >>
552e3d24 721
2e23f7dc 722Returns the name of a method suitable for reading or writing the value
723of the attribute in the associated class.
552e3d24 724
2e23f7dc 725If an attribute is read- or write-only, then these methods can return
726C<undef> as appropriate.
552e3d24 727
2e23f7dc 728=item B<< $attr->has_read_method >>
c50c603e 729
2e23f7dc 730=item B<< $attr->has_write_method >>
7d28758b 731
2e23f7dc 732This returns a boolean indicating whether the attribute has a I<named>
733read or write method.
0ab65f99 734
2e23f7dc 735=item B<< $attr->get_read_method_ref >>
552e3d24 736
2e23f7dc 737=item B<< $attr->get_write_method_ref >>
495af518 738
2e23f7dc 739Returns the subroutine reference of a method suitable for reading or
740writing the attribute's value in the associated class. These methods
741always return a subroutine reference, regardless of whether or not the
742attribute is read- or write-only.
743
eeff7496 744=item B<< $attr->insertion_order >>
745
746If this attribute has been inserted into a class, this returns a zero
747based index regarding the order of insertion.
748
2e23f7dc 749=back
fe122940 750
2e23f7dc 751=head2 Informational predicates
92d2abfa 752
2e23f7dc 753These are all basic predicate methods for the values passed into C<new>.
552e3d24 754
2e23f7dc 755=over 4
c57c8b10 756
2e23f7dc 757=item B<< $attr->has_accessor >>
c57c8b10 758
2e23f7dc 759=item B<< $attr->has_reader >>
b25109b1 760
2e23f7dc 761=item B<< $attr->has_writer >>
b25109b1 762
2e23f7dc 763=item B<< $attr->has_predicate >>
5da16d1b 764
2e23f7dc 765=item B<< $attr->has_clearer >>
5da16d1b 766
2e23f7dc 767=item B<< $attr->has_initializer >>
5da16d1b 768
2e23f7dc 769=item B<< $attr->has_init_arg >>
5da16d1b 770
2e23f7dc 771This will be I<false> if the C<init_arg> was set to C<undef>.
b25109b1 772
2e23f7dc 773=item B<< $attr->has_default >>
9e517e01 774
2e23f7dc 775This will be I<false> if the C<default> was set to C<undef>, since
776C<undef> is the default C<default> anyway.
9e517e01 777
2e23f7dc 778=item B<< $attr->has_builder >>
9e517e01 779
eeff7496 780=item B<< $attr->has_insertion_order >>
781
782This will be I<false> if this attribute has not be inserted into a class
783
552e3d24 784=back
785
2e23f7dc 786=head2 Value management
552e3d24 787
a6710c60 788These methods are basically "back doors" to the instance, and can be
2e23f7dc 789used to bypass the regular accessors, but still stay within the MOP.
790
791These methods are not for general use, and should only be used if you
792really know what you are doing.
fe122940 793
552e3d24 794=over 4
795
2e23f7dc 796=item B<< $attr->initialize_instance_slot($meta_instance, $instance, $params) >>
797
798This method is used internally to initialize the attribute's slot in
799the object C<$instance>.
800
801The C<$params> is a hash reference of the values passed to the object
802constructor.
803
804It's unlikely that you'll need to call this method yourself.
552e3d24 805
2e23f7dc 806=item B<< $attr->set_value($instance, $value) >>
552e3d24 807
2e23f7dc 808Sets the value without going through the accessor. Note that this
809works even with read-only attributes.
552e3d24 810
2e23f7dc 811=item B<< $attr->set_initial_value($instance, $value) >>
c50c603e 812
2e23f7dc 813Sets the value without going through the accessor. This method is only
814called when the instance is first being initialized.
7d28758b 815
2e23f7dc 816=item B<< $attr->get_value($instance) >>
0ab65f99 817
2e23f7dc 818Returns the value without going through the accessor. Note that this
819works even with write-only accessors.
552e3d24 820
2e23f7dc 821=item B<< $attr->has_value($instance) >>
552e3d24 822
2e23f7dc 823Return a boolean indicating whether the attribute has been set in
824C<$instance>. This how the default C<predicate> method works.
825
826=item B<< $attr->clear_value($instance) >>
827
828This will clear the attribute's value in C<$instance>. This is what
829the default C<clearer> calls.
830
831Note that this works even if the attribute does not have any
832associated read, write or clear methods.
bf731086 833
552e3d24 834=back
835
9ec169fe 836=head2 Class association
837
1d68af04 838These methods allow you to manage the attributes association with
839the class that contains it. These methods should not be used
2367814a 840lightly, nor are they very magical, they are mostly used internally
841and by metaclass instances.
842
9ec169fe 843=over 4
844
2e23f7dc 845=item B<< $attr->associated_class >>
846
847This returns the C<Class::MOP::Class> with which this attribute is
848associated, if any.
849
850=item B<< $attr->attach_to_class($metaclass) >>
9ec169fe 851
2e23f7dc 852This method stores a weakened reference to the C<$metaclass> object
853internally.
2367814a 854
2e23f7dc 855This method does not remove the attribute from its old class,
856nor does it create any accessors in the new class.
9ec169fe 857
2e23f7dc 858It is probably best to use the L<Class::MOP::Class> C<add_attribute>
859method instead.
2367814a 860
2e23f7dc 861=item B<< $attr->detach_from_class >>
9ec169fe 862
2e23f7dc 863This method removes the associate metaclass object from the attribute
864it has one.
865
866This method does not remove the attribute itself from the class, or
867remove its accessors.
868
869It is probably best to use the L<Class::MOP::Class>
870C<remove_attribute> method instead.
2367814a 871
9ec169fe 872=back
873
552e3d24 874=head2 Attribute Accessor generation
875
876=over 4
877
2e23f7dc 878=item B<< $attr->accessor_metaclass >>
ba38bf08 879
2e23f7dc 880Accessor methods are generated using an accessor metaclass. By
881default, this is L<Class::MOP::Method::Accessor>. This method returns
2367814a 882the name of the accessor metaclass that this attribute uses.
883
2e23f7dc 884=item B<< $attr->associate_method($method) >>
2367814a 885
2e23f7dc 886This associates a L<Class::MOP::Method> object with the
887attribute. Typically, this is called internally when an attribute
888generates its accessors.
3545c727 889
2e23f7dc 890=item B<< $attr->associated_methods >>
3545c727 891
2e23f7dc 892This returns the list of methods which have been associated with the
893attribute.
2367814a 894
2e23f7dc 895=item B<< $attr->install_accessors >>
2eb717d5 896
2e23f7dc 897This method generates and installs code the attributes various
898accessors. It is typically called from the L<Class::MOP::Class>
899C<add_attribute> method.
2eb717d5 900
2e23f7dc 901=item B<< $attr->remove_accessors >>
2eb717d5 902
2e23f7dc 903This method removes all of the accessors associated with the
904attribute.
2eb717d5 905
2e23f7dc 906This does not currently remove methods from the list returned by
907C<associated_methods>.
2367814a 908
2eb717d5 909=back
910
911=head2 Introspection
912
913=over 4
552e3d24 914
45b4c423 915=item B<< Class::MOP::Attribute->meta >>
552e3d24 916
2e23f7dc 917This will return a L<Class::MOP::Class> instance for this class.
fe122940 918
2e23f7dc 919It should also be noted that L<Class::MOP> will actually bootstrap
920this module by installing a number of attribute meta-objects into its
921metaclass.
fe122940 922
552e3d24 923=back
924
1a09d9cc 925=head1 AUTHORS
8b978dd5 926
a2e85e6c 927Stevan Little E<lt>stevan@iinteractive.comE<gt>
8b978dd5 928
929=head1 COPYRIGHT AND LICENSE
930
070bb6c9 931Copyright 2006-2009 by Infinity Interactive, Inc.
8b978dd5 932
933L<http://www.iinteractive.com>
934
935This library is free software; you can redistribute it and/or modify
1d68af04 936it under the same terms as Perl itself.
8b978dd5 937
16e960bd 938=cut
939
7d28758b 940