1 package Class::Accessor::Grouped;
5 use Class::Inspector ();
10 our $VERSION = '0.09005';
11 $VERSION = eval $VERSION;
13 # Class::XSAccessor is segfaulting on win32, so be careful
14 # Win32 users can set $hasXS to try to use it anyway
19 if (not defined $hasXS) {
22 if ($^O ne 'MSWin32') {
24 require Class::XSAccessor;
35 Class::Accessor::Grouped - Lets you build groups of accessors
41 This class lets you build groups of accessors that will call different
46 =head2 mk_group_accessors
50 =item Arguments: $group, @fieldspec
56 Creates a set of accessors in a given group.
58 $group is the name of the accessor group for the generated accessors; they
59 will call get_$group($field) on get and set_$group($field, $value) on set.
61 If you want to mimic Class::Accessor's mk_accessors $group has to be 'simple'
62 to tell Class::Accessor::Grouped to use its own get_simple and set_simple
65 @fieldspec is a list of field/accessor names; if a fieldspec is a scalar
66 this is used as both field and accessor name, if a listref it is expected to
67 be of the form [ $accessor, $field ].
71 sub mk_group_accessors {
72 my ($self, $group, @fields) = @_;
74 $self->_mk_group_accessors('make_group_accessor', $group, @fields);
81 no warnings 'redefine';
83 sub _mk_group_accessors {
84 my($self, $maker, $group, @fields) = @_;
85 my $class = Scalar::Util::blessed $self || $self;
87 # So we don't have to do lots of lookups inside the loop.
88 $maker = $self->can($maker) unless ref $maker;
92 foreach my $field (@fields) {
93 if( $field eq 'DESTROY' ) {
94 Carp::carp("Having a data accessor named DESTROY in ".
95 "'$class' is unwise.");
100 ($name, $field) = @$field if ref $field;
102 my $alias = "_${name}_accessor";
103 my $full_name = join('::', $class, $name);
104 my $full_alias = join('::', $class, $alias);
105 if ( $hasXS && $group eq 'simple' ) {
106 require Class::XSAccessor;
107 Class::XSAccessor->import({
117 my $accessor = $self->$maker($group, $field);
118 my $alias_accessor = $self->$maker($group, $field);
120 *$full_name = Sub::Name::subname($full_name, $accessor);
121 #unless defined &{$class."\:\:$field"}
123 *$full_alias = Sub::Name::subname($full_alias, $alias_accessor);
124 #unless defined &{$class."\:\:$alias"}
130 =head2 mk_group_ro_accessors
134 =item Arguments: $group, @fieldspec
140 Creates a set of read only accessors in a given group. Identical to
141 L</mk_group_accessors> but accessors will throw an error if passed a value
142 rather than setting the value.
146 sub mk_group_ro_accessors {
147 my($self, $group, @fields) = @_;
149 $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields);
152 =head2 mk_group_wo_accessors
156 =item Arguments: $group, @fieldspec
162 Creates a set of write only accessors in a given group. Identical to
163 L</mk_group_accessors> but accessors will throw an error if not passed a
164 value rather than getting the value.
168 sub mk_group_wo_accessors {
169 my($self, $group, @fields) = @_;
171 $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
174 =head2 make_group_accessor
178 =item Arguments: $group, $field
180 Returns: $sub (\CODE)
184 Returns a single accessor in a given group; called by mk_group_accessors
185 for each entry in @fieldspec.
189 sub make_group_accessor {
190 my ($class, $group, $field) = @_;
192 my $set = "set_$group";
193 my $get = "get_$group";
197 # eval for faster fastiness
198 my $code = eval "sub {
200 return shift->$set('$field', \@_);
203 return shift->$get('$field');
206 Carp::croak $@ if $@;
211 =head2 make_group_ro_accessor
215 =item Arguments: $group, $field
217 Returns: $sub (\CODE)
221 Returns a single read-only accessor in a given group; called by
222 mk_group_ro_accessors for each entry in @fieldspec.
226 sub make_group_ro_accessor {
227 my($class, $group, $field) = @_;
229 my $get = "get_$group";
233 my $code = eval "sub {
235 my \$caller = caller;
236 Carp::croak(\"'\$caller' cannot alter the value of '$field' on \".
237 \"objects of class '$class'\");
240 return shift->$get('$field');
243 Carp::croak $@ if $@;
248 =head2 make_group_wo_accessor
252 =item Arguments: $group, $field
254 Returns: $sub (\CODE)
258 Returns a single write-only accessor in a given group; called by
259 mk_group_wo_accessors for each entry in @fieldspec.
263 sub make_group_wo_accessor {
264 my($class, $group, $field) = @_;
266 my $set = "set_$group";
270 my $code = eval "sub {
272 my \$caller = caller;
273 Carp::croak(\"'\$caller' cannot access the value of '$field' on \".
274 \"objects of class '$class'\");
277 return shift->$set('$field', \@_);
280 Carp::croak $@ if $@;
289 =item Arguments: $field
295 Simple getter for hash-based objects which returns the value for the field
296 name passed as an argument.
301 return $_[0]->{$_[1]};
308 =item Arguments: $field, $new_value
314 Simple setter for hash-based objects which sets and then returns the value
315 for the field name passed as an argument.
320 return $_[0]->{$_[1]} = $_[2];
328 =item Arguments: $field
334 Simple getter for Classes and hash-based objects which returns the value for
335 the field name passed as an argument. This behaves much like
336 L<Class::Data::Accessor> where the field can be set in a base class,
337 inherited and changed in subclasses, and inherited and changed for object
345 if ( ($class = ref $_[0]) && Scalar::Util::blessed $_[0]) {
346 if (Scalar::Util::reftype $_[0] eq 'HASH') {
347 return $_[0]->{$_[1]} if exists $_[0]->{$_[1]};
350 Carp::croak('Cannot get inherited value on an object instance that is not hash-based');
358 no warnings qw/uninitialized/;
360 my $cag_slot = '::__cag_'. $_[1];
361 return ${$class.$cag_slot} if defined(${$class.$cag_slot});
363 # we need to be smarter about recalculation, as @ISA (thus supers) can very well change in-flight
364 my $cur_gen = mro::get_pkg_gen ($class);
365 if ( $cur_gen != ${$class.'::__cag_pkg_gen__'} ) {
366 @{$class.'::__cag_supers__'} = $_[0]->get_super_paths;
367 ${$class.'::__cag_pkg_gen__'} = $cur_gen;
370 for (@{$class.'::__cag_supers__'}) {
371 return ${$_.$cag_slot} if defined(${$_.$cag_slot});
381 =item Arguments: $field, $new_value
387 Simple setter for Classes and hash-based objects which sets and then returns
388 the value for the field name passed as an argument. When called on a hash-based
389 object it will set the appropriate hash key value. When called on a class, it
390 will set a class level variable.
392 B<Note:>: This method will die if you try to set an object variable on a non
398 if (Scalar::Util::blessed $_[0]) {
399 if (Scalar::Util::reftype $_[0] eq 'HASH') {
400 return $_[0]->{$_[1]} = $_[2];
402 Carp::croak('Cannot set inherited value on an object instance that is not hash-based');
407 return ${$_[0].'::__cag_'.$_[1]} = $_[2];
411 =head2 get_component_class
415 =item Arguments: $field
421 Gets the value of the specified component class.
423 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
425 $self->result_class->method();
428 $self->get_component_class('result_class')->method();
432 sub get_component_class {
433 return $_[0]->get_inherited($_[1]);
436 =head2 set_component_class
440 =item Arguments: $field, $class
446 Inherited accessor that automatically loads the specified class before setting
447 it. This method will die if the specified class could not be loaded.
449 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
450 __PACKAGE__->result_class('MyClass');
452 $self->result_class->method();
456 sub set_component_class {
459 if (Class::Inspector->installed($_[2]) && !Class::Inspector->loaded($_[2])) {
462 Carp::croak("Could not load $_[1] '$_[2]': ", $@) if $@;
466 return $_[0]->set_inherited($_[1], $_[2]);
469 =head2 get_super_paths
471 Returns a list of 'parent' or 'super' class names that the current class inherited from.
475 sub get_super_paths {
476 return @{mro::get_linear_isa( ref($_[0]) || $_[0] )};
483 You can speed up accessors of type 'simple' by installing L<Class::XSAccessor>.
487 Matt S. Trout <mst@shadowcatsystems.co.uk>
488 Christopher H. Laco <claco@chrislaco.com>
492 groditi: Guillermo Roditi <groditi@cpan.org>
493 ribasushi: Peter Rabbitson <ribasushi@cpan.org>
494 Jason Plum <jason.plum@bmmsi.com>
496 =head1 COPYRIGHT & LICENSE
498 Copyright (c) 2006-2010 Matt S. Trout <mst@shadowcatsystems.co.uk>
500 This program is free software; you can redistribute it and/or modify
501 it under the same terms as perl itself.