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