Clean up code a bit
[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 {
45a183fb 332 warn "The process_accessors method has been made private and this public alias will be removed in a future release.";
333 goto &_process_accessors;
334}
335
336sub _process_accessors {
c0cbf4d9 337 my ($self, $type, $accessor, $generate_as_inline_methods) = @_;
d9d99689 338
339 my $method_ctx;
340
341 if ( my $ctx = $self->definition_context ) {
342 $method_ctx = { %$ctx };
343 }
344
9b522fc4 345 if (ref($accessor)) {
346 (ref($accessor) eq 'HASH')
7d28758b 347 || confess "bad accessor/reader/writer/predicate/clearer format, must be a HASH ref";
4d47b77f 348 my ($name, $method) = %{$accessor};
4c105333 349 $method = $self->accessor_metaclass->wrap(
350 $method,
351 package_name => $self->associated_class->name,
352 name => $name,
d9d99689 353 definition_context => $method_ctx,
4c105333 354 );
3545c727 355 $self->associate_method($method);
1d68af04 356 return ($name, $method);
2eb717d5 357 }
9ec169fe 358 else {
1d68af04 359 my $inline_me = ($generate_as_inline_methods && $self->associated_class->instance_metaclass->is_inlinable);
ba38bf08 360 my $method;
361 eval {
d9d99689 362 if ( $method_ctx ) {
363 my $desc = "accessor $accessor";
364 if ( $accessor ne $self->name ) {
365 $desc .= " of attribute " . $self->name;
366 }
367
368 $method_ctx->{description} = $desc;
369 }
370
ba38bf08 371 $method = $self->accessor_metaclass->new(
372 attribute => $self,
d90b42a6 373 is_inline => $inline_me,
ba38bf08 374 accessor_type => $type,
4c105333 375 package_name => $self->associated_class->name,
376 name => $accessor,
d9d99689 377 definition_context => $method_ctx,
1d68af04 378 );
ba38bf08 379 };
1d68af04 380 confess "Could not create the '$type' method for " . $self->name . " because : $@" if $@;
3545c727 381 $self->associate_method($method);
ba38bf08 382 return ($accessor, $method);
1d68af04 383 }
9ec169fe 384}
385
386sub install_accessors {
c0cbf4d9 387 my $self = shift;
388 my $inline = shift;
389 my $class = $self->associated_class;
1d68af04 390
9ec169fe 391 $class->add_method(
45a183fb 392 $self->_process_accessors('accessor' => $self->accessor(), $inline)
9ec169fe 393 ) if $self->has_accessor();
394
1d68af04 395 $class->add_method(
45a183fb 396 $self->_process_accessors('reader' => $self->reader(), $inline)
9ec169fe 397 ) if $self->has_reader();
398
399 $class->add_method(
45a183fb 400 $self->_process_accessors('writer' => $self->writer(), $inline)
9ec169fe 401 ) if $self->has_writer();
402
403 $class->add_method(
45a183fb 404 $self->_process_accessors('predicate' => $self->predicate(), $inline)
9ec169fe 405 ) if $self->has_predicate();
1d68af04 406
7d28758b 407 $class->add_method(
45a183fb 408 $self->_process_accessors('clearer' => $self->clearer(), $inline)
7d28758b 409 ) if $self->has_clearer();
1d68af04 410
9ec169fe 411 return;
2eb717d5 412}
413
b51af7f9 414{
415 my $_remove_accessor = sub {
416 my ($accessor, $class) = @_;
9b522fc4 417 if (ref($accessor) && ref($accessor) eq 'HASH') {
c50c603e 418 ($accessor) = keys %{$accessor};
1d68af04 419 }
420 my $method = $class->get_method($accessor);
421 $class->remove_method($accessor)
b3fa93c7 422 if (ref($method) && $method->isa('Class::MOP::Method::Accessor'));
b51af7f9 423 };
1d68af04 424
b51af7f9 425 sub remove_accessors {
9ec169fe 426 my $self = shift;
2367814a 427 # TODO:
1d68af04 428 # we really need to make sure to remove from the
429 # associates methods here as well. But this is
430 # such a slimly used method, I am not worried
2367814a 431 # about it right now.
9ec169fe 432 $_remove_accessor->($self->accessor(), $self->associated_class()) if $self->has_accessor();
433 $_remove_accessor->($self->reader(), $self->associated_class()) if $self->has_reader();
434 $_remove_accessor->($self->writer(), $self->associated_class()) if $self->has_writer();
435 $_remove_accessor->($self->predicate(), $self->associated_class()) if $self->has_predicate();
7d28758b 436 $_remove_accessor->($self->clearer(), $self->associated_class()) if $self->has_clearer();
1d68af04 437 return;
b51af7f9 438 }
439
8b978dd5 440}
441
4421;
443
444__END__
445
446=pod
447
1d68af04 448=head1 NAME
8b978dd5 449
450Class::MOP::Attribute - Attribute Meta Object
451
452=head1 SYNOPSIS
1d68af04 453
2e23f7dc 454 Class::MOP::Attribute->new(
455 foo => (
456 accessor => 'foo', # dual purpose get/set accessor
457 predicate => 'has_foo', # predicate check for defined-ness
458 init_arg => '-foo', # class->new will look for a -foo key
459 default => 'BAR IS BAZ!' # if no -foo key is provided, use this
460 )
461 );
462
463 Class::MOP::Attribute->new(
464 bar => (
465 reader => 'bar', # getter
466 writer => 'set_bar', # setter
467 predicate => 'has_bar', # predicate check for defined-ness
468 init_arg => ':bar', # class->new will look for a :bar key
469 # no default value means it is undef
470 )
471 );
8b978dd5 472
473=head1 DESCRIPTION
474
2e23f7dc 475The Attribute Protocol is almost entirely an invention of
476C<Class::MOP>. Perl 5 does not have a consistent notion of
477attributes. There are so many ways in which this is done, and very few
478(if any) are easily discoverable by this module.
552e3d24 479
2e23f7dc 480With that said, this module attempts to inject some order into this
1d68af04 481chaos, by introducing a consistent API which can be used to create
fe122940 482object attributes.
552e3d24 483
484=head1 METHODS
485
486=head2 Creation
487
488=over 4
489
2e23f7dc 490=item B<< Class::MOP::Attribute->new($name, ?%options) >>
fe122940 491
1d68af04 492An attribute must (at the very least), have a C<$name>. All other
2e23f7dc 493C<%options> are added as key-value pairs.
fe122940 494
2e23f7dc 495=over 8
fe122940 496
497=item I<init_arg>
498
2e23f7dc 499This is a string value representing the expected key in an
500initialization hash. For instance, if we have an C<init_arg> value of
501C<-foo>, then the following code will Just Work.
fe122940 502
2e23f7dc 503 MyClass->meta->construct_instance( -foo => 'Hello There' );
fe122940 504
2e23f7dc 505If an init_arg is not assigned, it will automatically use the
506attribute's name. If C<init_arg> is explicitly set to C<undef>, the
507attribute cannot be specified during initialization.
7b31baf4 508
1d68af04 509=item I<builder>
510
2e23f7dc 511This provides the name of a method that will be called to initialize
512the attribute. This method will be called on the object after it is
513constructed. It is expected to return a valid value for the attribute.
fe122940 514
4c4a6c41 515=item I<default>
516
2e23f7dc 517This can be used to provide an explicit default for initializing the
518attribute. If the default you provide is a subroutine reference, then
519this reference will be called I<as a method> on the object.
4c4a6c41 520
2e23f7dc 521If the value is a simple scalar (string or number), then it can be
522just passed as is. However, if you wish to initialize it with a HASH
523or ARRAY ref, then you need to wrap that inside a subroutine
524reference:
fe122940 525
2e23f7dc 526 Class::MOP::Attribute->new(
527 'foo' => (
528 default => sub { [] },
529 )
530 );
1d68af04 531
532 # or ...
533
2e23f7dc 534 Class::MOP::Attribute->new(
535 'foo' => (
536 default => sub { {} },
537 )
538 );
539
540If you wish to initialize an attribute with a subroutine reference
541itself, then you need to wrap that in a subroutine as well:
542
543 Class::MOP::Attribute->new(
544 'foo' => (
545 default => sub {
546 sub { print "Hello World" }
547 },
548 )
549 );
550
551And lastly, if the value of your attribute is dependent upon some
552other aspect of the instance structure, then you can take advantage of
553the fact that when the C<default> value is called as a method:
554
555 Class::MOP::Attribute->new(
556 'object_identity' => (
557 default => sub { Scalar::Util::refaddr( $_[0] ) },
558 )
559 );
560
561Note that there is no guarantee that attributes are initialized in any
562particular order, so you cannot rely on the value of some other
563attribute when generating the default.
fe122940 564
0ef07b33 565=item I<initializer>
566
2e23f7dc 567This option can be either a method name or a subroutine
568reference. This method will be called when setting the attribute's
569value in the constructor. Unlike C<default> and C<builder>, the
570initializer is only called when a value is provided to the
571constructor. The initializer allows you to munge this value during
572object construction.
573
574The initializer is called as a method with three arguments. The first
575is the value that was passed to the constructor. The second is a
576subroutine reference that can be called to actually set the
577attribute's value, and the last is the associated
578C<Class::MOP::Attribute> object.
579
580This contrived example shows an initializer that sets the attribute to
581twice the given value.
582
583 Class::MOP::Attribute->new(
584 'doubled' => (
585 initializer => sub {
586 my ( $instance, $value, $set ) = @_;
587 $set->( $value * 2 );
588 },
589 )
590 );
591
592Since an initializer can be a method name, you can easily make
0ef07b33 593attribute initialization use the writer:
594
2e23f7dc 595 Class::MOP::Attribute->new(
596 'some_attr' => (
597 writer => 'some_attr',
598 initializer => 'some_attr',
599 )
600 );
0ef07b33 601
2e23f7dc 602Your writer will need to examine C<@_> and determine under which
603context it is being called.
127d39a7 604
fe122940 605=back
606
2e23f7dc 607The C<accessor>, C<reader>, C<writer>, C<predicate> and C<clearer>
608options all accept the same parameters. You can provide the name of
609the method, in which case an appropriate default method will be
610generated for you. Or instead you can also provide hash reference
611containing exactly one key (the method name) and one value. The value
612should be a subroutine reference, which will be installed as the
613method itself.
59e7697f 614
615=over 4
616
617=item I<accessor>
618
2e23f7dc 619An C<accessor> is a standard Perl-style read/write accessor. It will
620return the value of the attribute, and if a value is passed as an
621argument, it will assign that value to the attribute.
fe122940 622
2e23f7dc 623Note that C<undef> is a legitimate value, so this will work:
fe122940 624
625 $object->set_something(undef);
626
59e7697f 627=item I<reader>
628
2e23f7dc 629This is a basic read-only accessor. It returns the value of the
630attribute.
fe122940 631
59e7697f 632=item I<writer>
633
1d68af04 634This is a basic write accessor, it accepts a single argument, and
2e23f7dc 635assigns that value to the attribute.
59e7697f 636
2e23f7dc 637Note that C<undef> is a legitimate value, so this will work:
59e7697f 638
2e23f7dc 639 $object->set_something(undef);
fe122940 640
641=item I<predicate>
642
2e23f7dc 643The predicate method returns a boolean indicating whether or not the
644attribute has been explicitly set.
07dca7e3 645
2e23f7dc 646Note that the predicate returns true even if the attribute was set to
647a false value (C<0> or C<undef>).
07dca7e3 648
7d28758b 649=item I<clearer>
650
2e23f7dc 651This method will uninitialize the attribute. After an attribute is
652cleared, its C<predicate> will return false.
7d28758b 653
f8813817 654=item I<definition_context>
655
656Mostly, this exists as a hook for the benefit of Moose.
657
658This option should be a hash reference containing several keys which
659will be used when inlining the attribute's accessors. The keys should
660include C<line>, the line number where the attribute was created, and
661either C<file> or C<description>.
662
663This information will ultimately be used when eval'ing inlined
664accessor code so that error messages report a useful line and file
665name.
666
59e7697f 667=back
552e3d24 668
2e23f7dc 669=item B<< $attr->clone(%options) >>
bd4e03f9 670
2e23f7dc 671This clones the attribute. Any options you provide will override the
672settings of the original attribute. You can change the name of the new
673attribute by passing a C<name> key in C<%options>.
127d39a7 674
2e23f7dc 675=back
bd4e03f9 676
2e23f7dc 677=head2 Informational
127d39a7 678
2e23f7dc 679These are all basic read-only accessors for the values passed into
680the constructor.
552e3d24 681
2e23f7dc 682=over 4
16e960bd 683
2e23f7dc 684=item B<< $attr->name >>
2367814a 685
2e23f7dc 686=item B<< $attr->accessor >>
2367814a 687
2e23f7dc 688=item B<< $attr->reader >>
16e960bd 689
2e23f7dc 690=item B<< $attr->writer >>
16e960bd 691
2e23f7dc 692=item B<< $attr->predicate >>
16e960bd 693
2e23f7dc 694=item B<< $attr->clearer >>
c0921932 695
2e23f7dc 696The C<accessor>, C<reader>, C<writer>, C<predicate>, and C<clearer>
697methods all return exactly what was passed to the constructor, so it
698can be either a string containing a method name, or a hash refrence.
c0921932 699
2e23f7dc 700=item B<< $attr->initializer >>
16e960bd 701
2e23f7dc 702Returns the intializer as passed to the constructor, so this may be
703either a method name or a subroutine reference.
16e960bd 704
2e23f7dc 705=item B<< $attr->init_arg >>
3545c727 706
2e23f7dc 707=item B<< $attr->is_default_a_coderef >>
2367814a 708
2e23f7dc 709=item B<< $attr->default($instance) >>
3545c727 710
2e23f7dc 711The C<$instance> argument is optional. If you don't pass it, the
712return value for this method is exactly what was passed to the
713constructor, either a simple scalar or a subroutine reference.
2367814a 714
2e23f7dc 715If you I<do> pass an C<$instance> and the default is a subroutine
716reference, then the reference is called as a method on the
717C<$instance> and the generated value is returned.
16e960bd 718
2e23f7dc 719=item B<< $attr->slots >>
552e3d24 720
2e23f7dc 721Return a list of slots required by the attribute. This is usually just
722one, the name of the attribute.
fe122940 723
2e23f7dc 724A slot is the name of the hash key used to store the attribute in an
725object instance.
552e3d24 726
2e23f7dc 727=item B<< $attr->get_read_method >>
552e3d24 728
2e23f7dc 729=item B<< $attr->get_write_method >>
552e3d24 730
2e23f7dc 731Returns the name of a method suitable for reading or writing the value
732of the attribute in the associated class.
552e3d24 733
2e23f7dc 734If an attribute is read- or write-only, then these methods can return
735C<undef> as appropriate.
552e3d24 736
2e23f7dc 737=item B<< $attr->has_read_method >>
c50c603e 738
2e23f7dc 739=item B<< $attr->has_write_method >>
7d28758b 740
2e23f7dc 741This returns a boolean indicating whether the attribute has a I<named>
742read or write method.
0ab65f99 743
2e23f7dc 744=item B<< $attr->get_read_method_ref >>
552e3d24 745
2e23f7dc 746=item B<< $attr->get_write_method_ref >>
495af518 747
2e23f7dc 748Returns the subroutine reference of a method suitable for reading or
749writing the attribute's value in the associated class. These methods
750always return a subroutine reference, regardless of whether or not the
751attribute is read- or write-only.
752
753=back
fe122940 754
2e23f7dc 755=head2 Informational predicates
92d2abfa 756
2e23f7dc 757These are all basic predicate methods for the values passed into C<new>.
552e3d24 758
2e23f7dc 759=over 4
c57c8b10 760
2e23f7dc 761=item B<< $attr->has_accessor >>
c57c8b10 762
2e23f7dc 763=item B<< $attr->has_reader >>
b25109b1 764
2e23f7dc 765=item B<< $attr->has_writer >>
b25109b1 766
2e23f7dc 767=item B<< $attr->has_predicate >>
5da16d1b 768
2e23f7dc 769=item B<< $attr->has_clearer >>
5da16d1b 770
2e23f7dc 771=item B<< $attr->has_initializer >>
5da16d1b 772
2e23f7dc 773=item B<< $attr->has_init_arg >>
5da16d1b 774
2e23f7dc 775This will be I<false> if the C<init_arg> was set to C<undef>.
b25109b1 776
2e23f7dc 777=item B<< $attr->has_default >>
9e517e01 778
2e23f7dc 779This will be I<false> if the C<default> was set to C<undef>, since
780C<undef> is the default C<default> anyway.
9e517e01 781
2e23f7dc 782=item B<< $attr->has_builder >>
9e517e01 783
552e3d24 784=back
785
2e23f7dc 786=head2 Value management
552e3d24 787
2e23f7dc 788These methods are basically "backdoors" to the instance, and can be
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
2e23f7dc 915=item B<< $attr->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