1
package Class::Meta::AccessorBuilder;
3
# $Id: AccessorBuilder.pm 2405 2005-12-17 03:41:09Z theory $
7
Class::Meta::AccessorBuilder - Perl style accessor generation
11
package MyApp::TypeDef;
14
use Class::Meta::Type;
17
my $type = Class::Meta::Type->add( key => 'io_socket',
19
desc => 'IO::Socket object',
20
name => 'IO::Socket Object' );
24
This module provides the default accessor builder for Class::Meta. It builds
25
standard Perl-style accessors. For example, an attribute named "io_socket"
26
would have a single accessor method, C<io_socket>.
30
Class::Meta::AccessorBuilder create three different types of accessors:
31
read-only, write-only, and read/write. The type of accessor created depends on
32
the value of the C<authz> attribute of the Class::Meta::Attribute for which
33
the accessor is being created.
35
For example, if the C<authz> is Class::Meta::RDWR, then the method will be
36
able to both read and write the attribute.
38
my $value = $obj->io_socket;
39
$obj->io_socket($value);
41
If the value of C<authz> is Class::Meta::READ, then the method will not
42
be able to change the value of the attribute:
44
my $value = $obj->io_socket;
45
$obj->io_socket($value); # Has no effect.
47
And finally, if the value of C<authz> is Class::Meta::WRITE, then the method
48
will not return the value of the attribute (why anyone would want this is
49
beyond me, but I provide for the sake of completeness):
51
$obj->io_socket($value);
52
my $value = $obj->io_socket; # Always returns undef.
54
=head2 Data Type Validation
56
Class::Meta::AccessorBuilder uses all of the validation checks passed to it to
57
validate new values before assigning them to an attribute. It also checks to
58
see if the attribute is required, and if so, adds a check to ensure that its
59
value is never undefined. It does not currently check to ensure that private
60
and protected methods are used only in their appropriate contexts, but may do
61
so in a future release.
63
=head2 Class Attributes
65
If the C<context> attribute of the attribute object for which accessors are to
66
be built is C<Class::Meta::CLASS>, Class::Meta::AccessorBuilder will build
67
accessors for a class attribute instead of an object attribute. Of course,
68
this means that if you change the value of the class attribute in any
69
context--whether via a an object, the class name, or an an inherited class
70
name or object, the value will be changed everywhere.
72
For example, for a class attribute "count", you can expect the following to
75
MyApp::Custom->count(10);
76
my $count = MyApp::Custom->count; # Returns 10.
77
my $obj = MyApp::Custom->new;
78
$count = $obj->count; # Returns 10.
81
$count = $obj->count; # Returns 22.
82
my $count = MyApp::Custom->count; # Returns 22.
84
MyApp::Custom->count(35);
85
$count = $obj->count; # Returns 35.
86
my $count = MyApp::Custom->count; # Returns 35.
88
Currently, class attribute accessors are not designed to be inheritable in the
89
way designed by Class::Data::Inheritable, although this might be changed in a
90
future release. For now, I expect that the current simple approach will cover
91
the vast majority of circumstances.
93
B<Note:> Class attribute accessors will not work accurately in multiprocess
94
environments such as mod_perl. If you change a class attribute's value in one
95
process, it will not be changed in any of the others. Furthermore, class
96
attributes are not currently shared across threads. So if you're using
97
Class::Meta class attributes in a multi-threaded environment (such as iThreads
98
in Perl 5.8.0 and later) the changes to a class attribute in one thread will
99
not be reflected in other threads.
101
=head1 Private and Protected Attributes
103
Any attributes that have their C<view> attribute set to Class::Meta::Private
104
or Class::Meta::Protected get additional validation installed to ensure that
105
they're truly private or protected. This includes when they are set via
106
parameters to constructors generated by Class::Meta. The validation is
107
performed by checking the caller of the accessors, and throwing an exception
108
when the caller isn't the class that owns the attribute (for private
109
attributes) or when it doesn't inherit from the class that owns the attribute
110
(for protected attributes).
112
As an implementation note, this validation is performed for parameters passed
113
to constructors created by Class::Meta by ignoring looking for the first
114
caller that isn't Class::Meta::Constructor:
117
# Circumvent generated constructors.
118
for (my $i = 1; $caller eq 'Class::Meta::Constructor'; $i++) {
119
$caller = caller($i);
122
This works because Class::Meta::Constructor installs the closures that become
123
constructors, and thus, when those closures call accessors to set new values
124
for attributes, the caller is Class::Meta::Constructor. By going up the stack
125
until we find another package, we correctly check to see what context is
126
setting attribute values via a constructor, rather than the constructor method
127
itself being the context.
129
This is a bit of a hack, but since Perl uses call stacks for checking security
130
in this way, it's the best I could come up with. Other suggestions welcome. Or
131
see L<Class::Meta::Type|Class::Meta::Type/"Custom Accessor Building"> to
132
create your own accessor generation code
136
The following functions must be implemented by any Class::Meta accessor
141
=head3 build_attr_get
143
my $code = Class::Meta::AccessorBuilder::build_attr_get();
145
This function is called by C<Class::Meta::Type::make_attr_get()> and returns a
146
code reference that can be used by the C<get()> method of
147
Class::Meta::Attribute to return the value stored for that attribute for the
148
object passed to the code reference.
150
=head3 build_attr_set
152
my $code = Class::Meta::AccessorBuilder::build_attr_set();
154
This function is called by C<Class::Meta::Type::make_attr_set()> and returns a
155
code reference that can be used by the C<set()> method of
156
Class::Meta::Attribute to set the value stored for that attribute for the
157
object passed to the code reference.
161
Class::Meta::AccessorBuilder::build($pkg, $attribute, $create, @checks);
163
This method is called by the C<build()> method of Class::Meta::Type, and does
164
the work of actually generating the accessors for an attribute object. The
165
arguments passed to it are:
171
The name of the class to which the accessors will be added.
175
The Class::Meta::Attribute object that specifies the attribute for which the
176
accessors will be created.
180
The value of the C<create> attribute of the Class::Meta::Attribute object,
181
which determines what accessors, if any, are to be created.
185
A list of code references that validate the value of an attribute. These will
186
be used in the set acccessor (mutator) to validate new attribute values.
194
our $VERSION = "0.52";
197
UNIVERSAL::can($_[0]->package, $_[0]->name);
200
sub build_attr_set { &build_attr_get }
203
$_[2]->class->handle_error("Attribute ", $_[2]->name, " must be defined")
204
unless defined $_[0];
208
$_[2]->class->handle_error("Attribute ", $_[2]->name,
209
" can only be set once")
210
if defined $_[1]->{$_[2]->name};
214
my ($pkg, $attr, $create, @checks) = @_;
215
my $name = $attr->name;
217
# Add the required check, if needed.
218
unshift @checks, $req_chk if $attr->required;
220
# Add a once check, if needed.
221
unshift @checks, $once_chk if $attr->once;
224
if ($attr->context == Class::Meta::CLASS) {
225
# Create class attribute accessors by creating a closure that
226
# references this variable.
227
my $data = $attr->default;
229
if ($create == Class::Meta::GET) {
230
# Create GET accessor.
231
$sub = sub { $data };
233
} elsif ($create == Class::Meta::SET) {
234
# Create SET accessor.
237
# Check the value passed in.
238
$_->($_[1], { $name => $data,
239
__pkg => ref $_[0] || $_[0] },
253
} elsif ($create == Class::Meta::GETSET) {
254
# Create GETSET accessor(s).
258
return $data unless @_;
259
# Check the value passed in.
260
$_->($_[1], { $name => $data,
261
__pkg => ref $self || $self },
264
return $data = $_[0];
269
return $data unless @_;
271
return $data = shift;
275
# Well, nothing I guess.
278
# Create object attribute accessors.
279
if ($create == Class::Meta::GET) {
280
# Create GET accessor.
281
$sub = sub { $_[0]->{$name} };
283
} elsif ($create == Class::Meta::SET) {
284
# Create SET accessor.
287
# Check the value passed in.
288
$_->($_[1], $_[0], $attr) for @checks;
290
$_[0]->{$name} = $_[1];
296
$_[0]->{$name} = $_[1];
301
} elsif ($create == Class::Meta::GETSET) {
302
# Create GETSET accessor(s).
306
return $self->{$name} unless @_;
307
# Check the value passed in.
308
$_->($_[0], $self, $attr) for @checks;
310
return $self->{$name} = $_[0];
315
return $self->{$name} unless @_;
317
return $self->{$name} = shift;
321
# Well, nothing I guess.
325
# Add public and private checks, if required.
326
if ($attr->view == Class::Meta::PROTECTED) {
330
# Circumvent generated constructors.
331
for (my $i = 1; $caller eq 'Class::Meta::Constructor'; $i++) {
332
$caller = caller($i);
335
$attr->class->handle_error("$name is a protected attribute "
337
unless UNIVERSAL::isa($caller, $pkg);
340
} elsif ($attr->view == Class::Meta::PRIVATE) {
344
# Circumvent generated constructors.
345
for (my $i = 1; $caller eq 'Class::Meta::Constructor'; $i++) {
346
$caller = caller($i);
349
$attr->class->handle_error("$name is a private attribute of $pkg")
350
unless $caller eq $pkg;
353
} elsif ($attr->view == Class::Meta::TRUSTED) {
355
# XXX Should we have an accessor for this?
356
my $trusted = $attr->class->{trusted};
359
# Circumvent generated constructors.
360
for (my $i = 1; $caller eq 'Class::Meta::Constructor'; $i++) {
361
$caller = caller($i);
364
goto &$real_sub if $caller eq $pkg;
365
for my $pack (@{$trusted}) {
366
goto &$real_sub if UNIVERSAL::isa($caller, $pack);
368
$attr->class->handle_error("$name is a trusted attribute of $pkg");
372
# Install the accessor.
374
*{"${pkg}::$name"} = $sub;
382
Please send bug reports to <bug-class-meta@rt.cpan.org> or report them via the
383
CPAN Request Tracker at L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Class-Meta>.
387
David Wheeler <david@kineticode.com>
393
=item L<Class::Meta|Class::Meta>
395
This class contains most of the documentation you need to get started with
398
=item L<Class::Meta::AccessorBuilder::Affordance|Class::Meta::AccessorBuilder::Affordance>
400
This module generates affordance style accessors (e.g., C<get_foo()> and
403
=item L<Class::Meta::AccessorBuilder::SemiAffordance|Class::Meta::AccessorBuilder::SemiAffordance>
405
This module generates semi-affordance style accessors (e.g., C<foo()> and
408
=item L<Class::Meta::Type|Class::Meta::Type>
410
This class manages the creation of data types.
412
=item L<Class::Meta::Attribute|Class::Meta::Attribute>
414
This class manages Class::Meta class attributes, most of which will have
419
=head1 COPYRIGHT AND LICENSE
421
Copyright (c) 2002-2005, David Wheeler. All Rights Reserved.
423
This module is free software; you can redistribute it and/or modify it under
424
the same terms as Perl itself.