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