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