bump!
[p5sagit/Class-Accessor-Grouped.git] / lib / Class / Accessor / Grouped.pm
CommitLineData
963a69a5 1package Class::Accessor::Grouped;
2use strict;
3use warnings;
a0bce8bc 4use Carp ();
331e820d 5use Class::Inspector ();
a0bce8bc 6use Scalar::Util ();
8787799c 7use MRO::Compat;
1ee74bdd 8use Sub::Name ();
331e820d 9
44060ff1 10our $VERSION = '0.08004';
963a69a5 11
12=head1 NAME
13
1ad8d8c6 14Class::Accessor::Grouped - Lets you build groups of accessors
963a69a5 15
16=head1 SYNOPSIS
17
18=head1 DESCRIPTION
19
20This class lets you build groups of accessors that will call different
21getters and setters.
22
23=head1 METHODS
24
25=head2 mk_group_accessors
26
27=over 4
28
29=item Arguments: $group, @fieldspec
30
31Returns: none
32
33=back
34
35Creates a set of accessors in a given group.
36
37$group is the name of the accessor group for the generated accessors; they
38will call get_$group($field) on get and set_$group($field, $value) on set.
39
22fa6720 40If you want to mimic Class::Accessor's mk_accessors $group has to be 'simple'
41to tell Class::Accessor::Grouped to use its own get_simple and set_simple
42methods.
43
963a69a5 44@fieldspec is a list of field/accessor names; if a fieldspec is a scalar
45this is used as both field and accessor name, if a listref it is expected to
46be of the form [ $accessor, $field ].
47
48=cut
49
50sub mk_group_accessors {
51 my ($self, $group, @fields) = @_;
52
53 $self->_mk_group_accessors('make_group_accessor', $group, @fields);
54 return;
55}
56
57
58{
59 no strict 'refs';
60 no warnings 'redefine';
61
62 sub _mk_group_accessors {
63 my($self, $maker, $group, @fields) = @_;
a0bce8bc 64 my $class = Scalar::Util::blessed $self || $self;
963a69a5 65
66 # So we don't have to do lots of lookups inside the loop.
67 $maker = $self->can($maker) unless ref $maker;
68
69 foreach my $field (@fields) {
70 if( $field eq 'DESTROY' ) {
a0bce8bc 71 Carp::carp("Having a data accessor named DESTROY in ".
963a69a5 72 "'$class' is unwise.");
73 }
74
75 my $name = $field;
76
77 ($name, $field) = @$field if ref $field;
78
79 my $accessor = $self->$maker($group, $field);
1ee74bdd 80 my $alias_accessor = $self->$maker($group, $field);
81
963a69a5 82 my $alias = "_${name}_accessor";
1ee74bdd 83 my $full_name = join('::', $class, $name);
84 my $full_alias = join('::', $class, $alias);
963a69a5 85
1ee74bdd 86 *$full_name = Sub::Name::subname($full_name, $accessor);
963a69a5 87 #unless defined &{$class."\:\:$field"}
1ee74bdd 88 *$full_alias = Sub::Name::subname($full_alias, $alias_accessor);
963a69a5 89 #unless defined &{$class."\:\:$alias"}
90 }
91 }
92}
93
94=head2 mk_group_ro_accessors
95
96=over 4
97
98=item Arguments: $group, @fieldspec
99
100Returns: none
101
102=back
103
104Creates a set of read only accessors in a given group. Identical to
105<L:/mk_group_accessors> but accessors will throw an error if passed a value
106rather than setting the value.
107
108=cut
109
110sub mk_group_ro_accessors {
111 my($self, $group, @fields) = @_;
112
113 $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields);
114}
115
116=head2 mk_group_wo_accessors
117
118=over 4
119
120=item Arguments: $group, @fieldspec
121
122Returns: none
123
124=back
125
126Creates a set of write only accessors in a given group. Identical to
127<L:/mk_group_accessors> but accessors will throw an error if not passed a
128value rather than getting the value.
129
130=cut
131
132sub mk_group_wo_accessors {
133 my($self, $group, @fields) = @_;
134
135 $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
136}
137
138=head2 make_group_accessor
139
140=over 4
141
142=item Arguments: $group, $field
143
144Returns: $sub (\CODE)
145
146=back
147
148Returns a single accessor in a given group; called by mk_group_accessors
149for each entry in @fieldspec.
150
151=cut
152
153sub make_group_accessor {
154 my ($class, $group, $field) = @_;
155
156 my $set = "set_$group";
157 my $get = "get_$group";
158
a0bce8bc 159 # eval for faster fastiness
160 return eval "sub {
161 if(\@_ > 1) {
162 return shift->$set('$field', \@_);
963a69a5 163 }
164 else {
a0bce8bc 165 return shift->$get('$field');
963a69a5 166 }
a0bce8bc 167 };"
963a69a5 168}
169
170=head2 make_group_ro_accessor
171
172=over 4
173
174=item Arguments: $group, $field
175
176Returns: $sub (\CODE)
177
178=back
179
180Returns a single read-only accessor in a given group; called by
181mk_group_ro_accessors for each entry in @fieldspec.
182
183=cut
184
185sub make_group_ro_accessor {
186 my($class, $group, $field) = @_;
187
188 my $get = "get_$group";
189
a0bce8bc 190 return eval "sub {
191 if(\@_ > 1) {
192 my \$caller = caller;
193 Carp::croak(\"'\$caller' cannot alter the value of '$field' on \".
194 \"objects of class '$class'\");
963a69a5 195 }
196 else {
a0bce8bc 197 return shift->$get('$field');
963a69a5 198 }
a0bce8bc 199 };"
963a69a5 200}
201
202=head2 make_group_wo_accessor
203
204=over 4
205
206=item Arguments: $group, $field
207
208Returns: $sub (\CODE)
209
210=back
211
212Returns a single write-only accessor in a given group; called by
213mk_group_wo_accessors for each entry in @fieldspec.
214
215=cut
216
217sub make_group_wo_accessor {
218 my($class, $group, $field) = @_;
219
220 my $set = "set_$group";
221
a0bce8bc 222 return eval "sub {
223 unless (\@_ > 1) {
224 my \$caller = caller;
225 Carp::croak(\"'\$caller' cannot access the value of '$field' on \".
226 \"objects of class '$class'\");
963a69a5 227 }
228 else {
a0bce8bc 229 return shift->$set('$field', \@_);
963a69a5 230 }
a0bce8bc 231 };"
963a69a5 232}
233
234=head2 get_simple
235
236=over 4
237
238=item Arguments: $field
239
240Returns: $value
241
242=back
243
244Simple getter for hash-based objects which returns the value for the field
245name passed as an argument.
246
247=cut
248
249sub get_simple {
a0bce8bc 250 return $_[0]->{$_[1]};
963a69a5 251}
252
253=head2 set_simple
254
255=over 4
256
257=item Arguments: $field, $new_value
258
259Returns: $new_value
260
261=back
262
263Simple setter for hash-based objects which sets and then returns the value
264for the field name passed as an argument.
265
266=cut
267
268sub set_simple {
a0bce8bc 269 return $_[0]->{$_[1]} = $_[2];
963a69a5 270}
271
e6f2a0fd 272
273=head2 get_inherited
274
275=over 4
276
277=item Arguments: $field
278
279Returns: $value
280
281=back
282
331e820d 283Simple getter for Classes and hash-based objects which returns the value for
284the field name passed as an argument. This behaves much like
285L<Class::Data::Accessor> where the field can be set in a base class,
286inherited and changed in subclasses, and inherited and changed for object
287instances.
e6f2a0fd 288
289=cut
290
291sub get_inherited {
a49c32d9 292 my $class;
e6f2a0fd 293
a0bce8bc 294 if (Scalar::Util::blessed $_[0]) {
295 my $reftype = Scalar::Util::reftype $_[0];
296 $class = ref $_[0];
a49c32d9 297
a0bce8bc 298 if ($reftype eq 'HASH' && exists $_[0]->{$_[1]}) {
299 return $_[0]->{$_[1]};
a49c32d9 300 } elsif ($reftype ne 'HASH') {
a0bce8bc 301 Carp::croak('Cannot get inherited value on an object instance that is not hash-based');
e6f2a0fd 302 };
a49c32d9 303 } else {
a0bce8bc 304 $class = $_[0];
e6f2a0fd 305 };
306
307 no strict 'refs';
fe63d727 308 no warnings qw/uninitialized/;
a0bce8bc 309 return ${$class.'::__cag_'.$_[1]} if defined(${$class.'::__cag_'.$_[1]});
e6f2a0fd 310
4f8ce9da 311 # we need to be smarter about recalculation, as @ISA (thus supers) can very well change in-flight
312 my $pkg_gen = mro::get_pkg_gen ($class);
fe63d727 313 if ( ${$class.'::__cag_pkg_gen'} != $pkg_gen ) {
a0bce8bc 314 @{$class.'::__cag_supers'} = $_[0]->get_super_paths;
4f8ce9da 315 ${$class.'::__cag_pkg_gen'} = $pkg_gen;
a49c32d9 316 };
317
318 foreach (@{$class.'::__cag_supers'}) {
a0bce8bc 319 return ${$_.'::__cag_'.$_[1]} if defined(${$_.'::__cag_'.$_[1]});
e6f2a0fd 320 };
c46050d3 321
9c3d5179 322 return undef;
e6f2a0fd 323}
324
325=head2 set_inherited
326
327=over 4
328
329=item Arguments: $field, $new_value
330
331Returns: $new_value
332
333=back
334
331e820d 335Simple setter for Classes and hash-based objects which sets and then returns
336the value for the field name passed as an argument. When called on a hash-based
337object it will set the appropriate hash key value. When called on a class, it
338will set a class level variable.
e6f2a0fd 339
331e820d 340B<Note:>: This method will die if you try to set an object variable on a non
341hash-based object.
e6f2a0fd 342
343=cut
344
345sub set_inherited {
a0bce8bc 346 if (Scalar::Util::blessed $_[0]) {
347 if (Scalar::Util::reftype $_[0] eq 'HASH') {
348 return $_[0]->{$_[1]} = $_[2];
e6f2a0fd 349 } else {
a0bce8bc 350 Carp::croak('Cannot set inherited value on an object instance that is not hash-based');
e6f2a0fd 351 };
352 } else {
353 no strict 'refs';
354
a0bce8bc 355 return ${$_[0].'::__cag_'.$_[1]} = $_[2];
e6f2a0fd 356 };
357}
358
331e820d 359=head2 get_component_class
360
361=over 4
362
363=item Arguments: $field
364
365Returns: $value
366
367=back
368
369Gets the value of the specified component class.
370
371 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
97d76fb4 372
331e820d 373 $self->result_class->method();
97d76fb4 374
331e820d 375 ## same as
376 $self->get_component_class('result_class')->method();
377
378=cut
379
380sub get_component_class {
a0bce8bc 381 return $_[0]->get_inherited($_[1]);
331e820d 382};
383
384=head2 set_component_class
385
386=over 4
387
388=item Arguments: $field, $class
389
390Returns: $new_value
391
392=back
393
394Inherited accessor that automatically loads the specified class before setting
395it. This method will die if the specified class could not be loaded.
396
397 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
398 __PACKAGE__->result_class('MyClass');
97d76fb4 399
331e820d 400 $self->result_class->method();
401
402=cut
403
404sub set_component_class {
a0bce8bc 405 if ($_[2]) {
bce7bdf8 406 local $^W = 0;
a0bce8bc 407 if (Class::Inspector->installed($_[2]) && !Class::Inspector->loaded($_[2])) {
408 eval "use $_[2]";
331e820d 409
a0bce8bc 410 Carp::croak("Could not load $_[1] '$_[2]': ", $@) if $@;
331e820d 411 };
412 };
413
a0bce8bc 414 return $_[0]->set_inherited($_[1], $_[2]);
331e820d 415};
416
a49c32d9 417=head2 get_super_paths
418
419Returns a list of 'parent' or 'super' class names that the current class inherited from.
420
421=cut
422
423sub get_super_paths {
a0bce8bc 424 my $class = Scalar::Util::blessed $_[0] || $_[0];
a49c32d9 425
8787799c 426 return @{mro::get_linear_isa($class)};
a49c32d9 427};
428
963a69a5 4291;
430
431=head1 AUTHORS
432
433Matt S. Trout <mst@shadowcatsystems.co.uk>
97972dcb 434Christopher H. Laco <claco@chrislaco.com>
963a69a5 435
dfb86526 436With contributions from:
437
438Guillermo Roditi <groditi@cpan.org>
439
963a69a5 440=head1 LICENSE
441
442You may distribute this code under the same terms as Perl itself.
443
444=cut
445