various fixes + weakening support for the Class::MOP instance layout
[gitmo/Class-MOP.git] / lib / Class / MOP / Attribute.pm
CommitLineData
8b978dd5 1
2package Class::MOP::Attribute;
3
4use strict;
5use warnings;
6
2eb717d5 7use Carp 'confess';
9ec169fe 8use Scalar::Util 'blessed', 'reftype', 'weaken';
2eb717d5 9
2bab2be6 10our $VERSION = '0.08';
8b978dd5 11
727919c5 12sub meta {
13 require Class::MOP::Class;
aa448b16 14 Class::MOP::Class->initialize(blessed($_[0]) || $_[0]);
727919c5 15}
2eb717d5 16
727919c5 17# NOTE: (meta-circularity)
18# This method will be replaces in the
19# boostrap section of Class::MOP, by
20# a new version which uses the
21# &Class::MOP::Class::construct_instance
22# method to build an attribute meta-object
23# which itself is described with attribute
24# meta-objects.
25# - Ain't meta-circularity grand? :)
8b978dd5 26sub new {
27 my $class = shift;
28 my $name = shift;
29 my %options = @_;
30
cbd9f942 31 (defined $name && $name)
8b978dd5 32 || confess "You must provide a name for the attribute";
5659d76e 33 $options{init_arg} = $name
34 if not exists $options{init_arg};
2eb717d5 35
8b978dd5 36 bless {
c50c603e 37 name => $name,
38 accessor => $options{accessor},
39 reader => $options{reader},
40 writer => $options{writer},
41 predicate => $options{predicate},
42 init_arg => $options{init_arg},
9ec169fe 43 default => $options{default},
44 # keep a weakened link to the
45 # class we are associated with
46 associated_class => undef,
8b978dd5 47 } => $class;
48}
49
7b31baf4 50# NOTE:
5659d76e 51# this is a primative (and kludgy) clone operation
52# for now, it will be repleace in the Class::MOP
53# bootstrap with a proper one, however we know
54# that this one will work fine for now.
55sub clone {
56 my $self = shift;
57 my %options = @_;
58 (blessed($self))
59 || confess "Can only clone an instance";
60 return bless { %{$self}, %options } => blessed($self);
61}
62
bd4e03f9 63sub initialize_instance_slot {
2d711cc8 64 my ($self, $instance, $params) = @_;
291073fc 65 my $init_arg = $self->{init_arg};
bd4e03f9 66 # try to fetch the init arg from the %params ...
67 my $val;
68 $val = $params->{$init_arg} if exists $params->{$init_arg};
69 # if nothing was in the %params, we can use the
70 # attribute's default value (if it has one)
bb8dacfa 71 if (!defined $val && defined $self->{default}) {
2d711cc8 72 $val = $self->default($instance);
73 }
74
75 my $meta_instance = $self->associated_class->get_meta_instance;
76 $meta_instance->set_slot_value_with_init( $instance, $self->slot_name, $val );
bd4e03f9 77}
78
5659d76e 79# NOTE:
7b31baf4 80# the next bunch of methods will get bootstrapped
81# away in the Class::MOP bootstrapping section
82
c50c603e 83sub name { $_[0]->{name} }
84
7b31baf4 85sub associated_class { $_[0]->{associated_class} }
86
727919c5 87sub has_accessor { defined($_[0]->{accessor}) ? 1 : 0 }
88sub has_reader { defined($_[0]->{reader}) ? 1 : 0 }
89sub has_writer { defined($_[0]->{writer}) ? 1 : 0 }
c50c603e 90sub has_predicate { defined($_[0]->{predicate}) ? 1 : 0 }
727919c5 91sub has_init_arg { defined($_[0]->{init_arg}) ? 1 : 0 }
92sub has_default { defined($_[0]->{default}) ? 1 : 0 }
c50c603e 93
94sub accessor { $_[0]->{accessor} }
95sub reader { $_[0]->{reader} }
96sub writer { $_[0]->{writer} }
97sub predicate { $_[0]->{predicate} }
98sub init_arg { $_[0]->{init_arg} }
99
7b31baf4 100# end bootstrapped away method section.
101# (all methods below here are kept intact)
102
c50c603e 103sub default {
104 my $self = shift;
105 if (reftype($self->{default}) && reftype($self->{default}) eq 'CODE') {
727919c5 106 # if the default is a CODE ref, then
107 # we pass in the instance and default
108 # can return a value based on that
109 # instance. Somewhat crude, but works.
c50c603e 110 return $self->{default}->(shift);
111 }
112 $self->{default};
113}
8b978dd5 114
9ec169fe 115# class association
727919c5 116
9ec169fe 117sub attach_to_class {
118 my ($self, $class) = @_;
119 (blessed($class) && $class->isa('Class::MOP::Class'))
120 || confess "You must pass a Class::MOP::Class instance (or a subclass)";
121 weaken($self->{associated_class} = $class);
122}
123
124sub detach_from_class {
125 my $self = shift;
126 $self->{associated_class} = undef;
127}
128
2d711cc8 129# slot management
130
131sub slot_name { # when attr <-> slot mapping is 1:1
132 my $self = shift;
133 $self->name;
134}
135
136# slot alocation
137
138sub allocate_slots {
139 my $self = shift;
140 my $meta_instance = $self->associated_class->get_meta_instance;
141 $meta_instance->add_slot( $self->slot_name );
142}
143
144sub deallocate_slots {
145 my $self = shift;
146 my $meta_instance = $self->associated_class->get_meta_instance;
147 $meta_instance->remove_slot( $self->slot_name );
148}
149
9ec169fe 150## Method generation helpers
151
152sub generate_accessor_method {
08388f17 153 my $self = shift;
2d711cc8 154 my $meta_instance = $self->associated_class->get_meta_instance;
155 my $slot_name = $self->slot_name;
156
013b1897 157 sub {
2d711cc8 158 $meta_instance->set_slot_value($_[0], $slot_name, $_[1]) if scalar(@_) == 2;
159 $meta_instance->get_slot_value($_[0], $slot_name);
013b1897 160 };
9ec169fe 161}
162
163sub generate_reader_method {
08388f17 164 my $self = shift;
2d711cc8 165 my $meta_instance = $self->associated_class->get_meta_instance;
166 my $slot_name = $self->slot_name;
b9dfbf78 167 sub {
168 confess "Cannot assign a value to a read-only accessor" if @_ > 1;
2d711cc8 169 $meta_instance->get_slot_value($_[0], $slot_name);
b9dfbf78 170 };
9ec169fe 171}
172
173sub generate_writer_method {
08388f17 174 my $self = shift;
2d711cc8 175 my $meta_instance = $self->associated_class->get_meta_instance;
176 my $slot_name = $self->slot_name;
2bab2be6 177 sub {
2d711cc8 178 $meta_instance->set_slot_value($_[0], $slot_name, $_[1]);
2bab2be6 179 };
9ec169fe 180}
181
182sub generate_predicate_method {
08388f17 183 my $self = shift;
2d711cc8 184 my $meta_instance = $self->associated_class->get_meta_instance;
185 my $slot_name = $self->slot_name;
2bab2be6 186 sub {
2d711cc8 187 defined $meta_instance->get_slot_value($_[0], $slot_name);
2bab2be6 188 };
9ec169fe 189}
190
191sub process_accessors {
192 my ($self, $type, $accessor) = @_;
013b1897 193 if (reftype($accessor)) {
194 (reftype($accessor) eq 'HASH')
195 || confess "bad accessor/reader/writer/predicate format, must be a HASH ref";
9ec169fe 196 my ($name, $method) = each %{$accessor};
a4258ffd 197 return ($name, Class::MOP::Attribute::Accessor->wrap($method));
2eb717d5 198 }
9ec169fe 199 else {
200 my $generator = $self->can('generate_' . $type . '_method');
201 ($generator)
202 || confess "There is no method generator for the type='$type'";
203 if (my $method = $self->$generator($self->name)) {
a4258ffd 204 return ($accessor => Class::MOP::Attribute::Accessor->wrap($method));
9ec169fe 205 }
343203ee 206 confess "Could not create the '$type' method for " . $self->name . " because : $@";
9ec169fe 207 }
208}
209
210sub install_accessors {
211 my $self = shift;
212 my $class = $self->associated_class;
c50c603e 213
9ec169fe 214 $class->add_method(
215 $self->process_accessors('accessor' => $self->accessor())
216 ) if $self->has_accessor();
217
218 $class->add_method(
219 $self->process_accessors('reader' => $self->reader())
220 ) if $self->has_reader();
221
222 $class->add_method(
223 $self->process_accessors('writer' => $self->writer())
224 ) if $self->has_writer();
225
226 $class->add_method(
227 $self->process_accessors('predicate' => $self->predicate())
228 ) if $self->has_predicate();
229 return;
2eb717d5 230}
231
b51af7f9 232{
233 my $_remove_accessor = sub {
234 my ($accessor, $class) = @_;
c50c603e 235 if (reftype($accessor) && reftype($accessor) eq 'HASH') {
236 ($accessor) = keys %{$accessor};
237 }
b51af7f9 238 my $method = $class->get_method($accessor);
239 $class->remove_method($accessor)
2eb717d5 240 if (blessed($method) && $method->isa('Class::MOP::Attribute::Accessor'));
b51af7f9 241 };
c50c603e 242
b51af7f9 243 sub remove_accessors {
9ec169fe 244 my $self = shift;
245 $_remove_accessor->($self->accessor(), $self->associated_class()) if $self->has_accessor();
246 $_remove_accessor->($self->reader(), $self->associated_class()) if $self->has_reader();
247 $_remove_accessor->($self->writer(), $self->associated_class()) if $self->has_writer();
248 $_remove_accessor->($self->predicate(), $self->associated_class()) if $self->has_predicate();
b51af7f9 249 return;
250 }
251
8b978dd5 252}
253
2eb717d5 254package Class::MOP::Attribute::Accessor;
255
256use strict;
257use warnings;
258
727919c5 259use Class::MOP::Method;
260
2eb717d5 261our $VERSION = '0.01';
262
263our @ISA = ('Class::MOP::Method');
264
8b978dd5 2651;
266
267__END__
268
269=pod
270
271=head1 NAME
272
273Class::MOP::Attribute - Attribute Meta Object
274
275=head1 SYNOPSIS
276
277 Class::MOP::Attribute->new('$foo' => (
fe122940 278 accessor => 'foo', # dual purpose get/set accessor
279 predicate => 'has_foo' # predicate check for defined-ness
280 init_arg => '-foo', # class->new will look for a -foo key
281 default => 'BAR IS BAZ!' # if no -foo key is provided, use this
8b978dd5 282 ));
283
284 Class::MOP::Attribute->new('$.bar' => (
fe122940 285 reader => 'bar', # getter
286 writer => 'set_bar', # setter
287 predicate => 'has_bar' # predicate check for defined-ness
288 init_arg => ':bar', # class->new will look for a :bar key
8b978dd5 289 # no default value means it is undef
290 ));
291
292=head1 DESCRIPTION
293
fe122940 294The Attribute Protocol is almost entirely an invention of this module,
295and is completely optional to this MOP. This is because Perl 5 does not
296have consistent notion of what is an attribute of a class. There are
297so many ways in which this is done, and very few (if any) are
298easily discoverable by this module.
552e3d24 299
300So, all that said, this module attempts to inject some order into this
fe122940 301chaos, by introducing a consistent API which can be used to create
302object attributes.
552e3d24 303
304=head1 METHODS
305
306=head2 Creation
307
308=over 4
309
fe122940 310=item B<new ($name, ?%options)>
311
312An attribute must (at the very least), have a C<$name>. All other
a2e85e6c 313C<%options> are contained added as key-value pairs. Acceptable keys
fe122940 314are as follows:
315
316=over 4
317
318=item I<init_arg>
319
320This should be a string value representing the expected key in
321an initialization hash. For instance, if we have an I<init_arg>
322value of C<-foo>, then the following code will Just Work.
323
324 MyClass->meta->construct_instance(-foo => "Hello There");
325
7b31baf4 326In an init_arg is not assigned, it will automatically use the
327value of C<$name>.
328
fe122940 329=item I<default>
330
331The value of this key is the default value which
332C<Class::MOP::Class::construct_instance> will initialize the
333attribute to.
334
335B<NOTE:>
336If the value is a simple scalar (string or number), then it can
337be just passed as is. However, if you wish to initialize it with
338a HASH or ARRAY ref, then you need to wrap that inside a CODE
339reference, like so:
340
341 Class::MOP::Attribute->new('@foo' => (
342 default => sub { [] },
343 ));
344
345 # or ...
346
347 Class::MOP::Attribute->new('%foo' => (
348 default => sub { {} },
349 ));
350
351If you wish to initialize an attribute with a CODE reference
352itself, then you need to wrap that in a subroutine as well, like
353so:
354
355 Class::MOP::Attribute->new('&foo' => (
356 default => sub { sub { print "Hello World" } },
357 ));
358
359And lastly, if the value of your attribute is dependent upon
360some other aspect of the instance structure, then you can take
361advantage of the fact that when the I<default> value is a CODE
362reference, it is passed the raw (unblessed) instance structure
363as it's only argument. So you can do things like this:
364
365 Class::MOP::Attribute->new('$object_identity' => (
366 default => sub { Scalar::Util::refaddr($_[0]) },
367 ));
368
369This last feature is fairly limited as there is no gurantee of
370the order of attribute initializations, so you cannot perform
371any kind of dependent initializations. However, if this is
372something you need, you could subclass B<Class::MOP::Class> and
373this class to acheive it. However, this is currently left as
374an exercise to the reader :).
375
376=back
377
a2e85e6c 378The I<accessor>, I<reader>, I<writer> and I<predicate> keys can
fe122940 379contain either; the name of the method and an appropriate default
380one will be generated for you, B<or> a HASH ref containing exactly one
381key (which will be used as the name of the method) and one value,
382which should contain a CODE reference which will be installed as
383the method itself.
59e7697f 384
385=over 4
386
387=item I<accessor>
388
fe122940 389The I<accessor> is a standard perl-style read/write accessor. It will
390return the value of the attribute, and if a value is passed as an argument,
391it will assign that value to the attribute.
392
393B<NOTE:>
394This method will properly handle the following code, by assigning an
395C<undef> value to the attribute.
396
397 $object->set_something(undef);
398
59e7697f 399=item I<reader>
400
fe122940 401This is a basic read-only accessor, it will just return the value of
402the attribute.
403
59e7697f 404=item I<writer>
405
fe122940 406This is a basic write accessor, it accepts a single argument, and
407assigns that value to the attribute. This method does not intentially
408return a value, however perl will return the result of the last
409expression in the subroutine, which returns in this returning the
410same value that it was passed.
59e7697f 411
fe122940 412B<NOTE:>
413This method will properly handle the following code, by assigning an
414C<undef> value to the attribute.
59e7697f 415
fe122940 416 $object->set_something();
417
418=item I<predicate>
419
420This is a basic test to see if the value of the attribute is not
421C<undef>. It will return true (C<1>) if the attribute's value is
422defined, and false (C<0>) otherwise.
59e7697f 423
424=back
552e3d24 425
bd4e03f9 426=item B<clone (%options)>
427
428=item B<initialize_instance_slot ($instance, $params)>
429
552e3d24 430=back
431
432=head2 Informational
433
fe122940 434These are all basic read-only value accessors for the values
435passed into C<new>. I think they are pretty much self-explanitory.
436
552e3d24 437=over 4
438
439=item B<name>
440
441=item B<accessor>
442
443=item B<reader>
444
445=item B<writer>
446
c50c603e 447=item B<predicate>
448
552e3d24 449=item B<init_arg>
450
fe122940 451=item B<default (?$instance)>
452
453As noted in the documentation for C<new> above, if the I<default>
454value is a CODE reference, this accessor will pass a single additional
455argument C<$instance> into it and return the value.
552e3d24 456
457=back
458
459=head2 Informational predicates
460
a2e85e6c 461These are all basic predicate methods for the values passed into C<new>.
fe122940 462
552e3d24 463=over 4
464
465=item B<has_accessor>
466
552e3d24 467=item B<has_reader>
468
552e3d24 469=item B<has_writer>
470
c50c603e 471=item B<has_predicate>
472
552e3d24 473=item B<has_init_arg>
474
552e3d24 475=item B<has_default>
476
552e3d24 477=back
478
9ec169fe 479=head2 Class association
480
481=over 4
482
483=item B<associated_class>
484
485=item B<attach_to_class ($class)>
486
487=item B<detach_from_class>
488
2d711cc8 489=item B<slot_name>
490
491=item B<allocate_slots>
492
493=item B<deallocate_slots>
494
9ec169fe 495=back
496
552e3d24 497=head2 Attribute Accessor generation
498
499=over 4
500
9ec169fe 501=item B<install_accessors>
2eb717d5 502
503This allows the attribute to generate and install code for it's own
a2e85e6c 504I<accessor/reader/writer/predicate> methods. This is called by
fe122940 505C<Class::MOP::Class::add_attribute>.
2eb717d5 506
9ec169fe 507This method will call C<process_accessors> for each of the possible
508method types (accessor, reader, writer & predicate).
509
510=item B<process_accessors ($type, $value)>
511
512This takes a C<$type> (accessor, reader, writer or predicate), and
513a C<$value> (the value passed into the constructor for each of the
514different types). It will then either generate the method itself
515(using the C<generate_*_method> methods listed below) or it will
516use the custom method passed through the constructor.
517
518=over 4
519
08388f17 520=item B<generate_accessor_method>
9ec169fe 521
08388f17 522=item B<generate_predicate_method>
9ec169fe 523
08388f17 524=item B<generate_reader_method>
9ec169fe 525
08388f17 526=item B<generate_writer_method>
9ec169fe 527
528=back
529
530=item B<remove_accessors>
2eb717d5 531
532This allows the attribute to remove the method for it's own
a2e85e6c 533I<accessor/reader/writer/predicate>. This is called by
fe122940 534C<Class::MOP::Class::remove_attribute>.
2eb717d5 535
536=back
537
538=head2 Introspection
539
540=over 4
552e3d24 541
2eb717d5 542=item B<meta>
552e3d24 543
fe122940 544This will return a B<Class::MOP::Class> instance which is related
545to this class.
546
547It should also be noted that B<Class::MOP> will actually bootstrap
548this module by installing a number of attribute meta-objects into
549it's metaclass. This will allow this class to reap all the benifits
550of the MOP when subclassing it.
551
552e3d24 552=back
553
8b978dd5 554=head1 AUTHOR
555
a2e85e6c 556Stevan Little E<lt>stevan@iinteractive.comE<gt>
8b978dd5 557
558=head1 COPYRIGHT AND LICENSE
559
560Copyright 2006 by Infinity Interactive, Inc.
561
562L<http://www.iinteractive.com>
563
564This library is free software; you can redistribute it and/or modify
565it under the same terms as Perl itself.
566
9ec169fe 567=cut