1 package Class::Accessor::Grouped;
6 use Scalar::Util qw/blessed reftype/;
13 Class::Accessor::Grouped - Lets you build groups of accessors
19 This class lets you build groups of accessors that will call different
24 =head2 mk_group_accessors
28 =item Arguments: $group, @fieldspec
34 Creates a set of accessors in a given group.
36 $group is the name of the accessor group for the generated accessors; they
37 will call get_$group($field) on get and set_$group($field, $value) on set.
39 @fieldspec is a list of field/accessor names; if a fieldspec is a scalar
40 this is used as both field and accessor name, if a listref it is expected to
41 be of the form [ $accessor, $field ].
45 sub mk_group_accessors {
46 my ($self, $group, @fields) = @_;
48 $self->_mk_group_accessors('make_group_accessor', $group, @fields);
55 no warnings 'redefine';
57 sub _mk_group_accessors {
58 my($self, $maker, $group, @fields) = @_;
59 my $class = ref $self || $self;
61 # So we don't have to do lots of lookups inside the loop.
62 $maker = $self->can($maker) unless ref $maker;
64 foreach my $field (@fields) {
65 if( $field eq 'DESTROY' ) {
66 carp("Having a data accessor named DESTROY in ".
67 "'$class' is unwise.");
72 ($name, $field) = @$field if ref $field;
74 my $accessor = $self->$maker($group, $field);
75 my $alias = "_${name}_accessor";
77 #warn "$class $group $field $alias";
79 *{$class."\:\:$name"} = $accessor;
80 #unless defined &{$class."\:\:$field"}
82 *{$class."\:\:$alias"} = $accessor;
83 #unless defined &{$class."\:\:$alias"}
88 =head2 mk_group_ro_accessors
92 =item Arguments: $group, @fieldspec
98 Creates a set of read only accessors in a given group. Identical to
99 <L:/mk_group_accessors> but accessors will throw an error if passed a value
100 rather than setting the value.
104 sub mk_group_ro_accessors {
105 my($self, $group, @fields) = @_;
107 $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields);
110 =head2 mk_group_wo_accessors
114 =item Arguments: $group, @fieldspec
120 Creates a set of write only accessors in a given group. Identical to
121 <L:/mk_group_accessors> but accessors will throw an error if not passed a
122 value rather than getting the value.
126 sub mk_group_wo_accessors {
127 my($self, $group, @fields) = @_;
129 $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
132 =head2 make_group_accessor
136 =item Arguments: $group, $field
138 Returns: $sub (\CODE)
142 Returns a single accessor in a given group; called by mk_group_accessors
143 for each entry in @fieldspec.
147 sub make_group_accessor {
148 my ($class, $group, $field) = @_;
150 my $set = "set_$group";
151 my $get = "get_$group";
153 # Build a closure around $field.
158 return $self->$set($field, @_);
161 return $self->$get($field);
166 =head2 make_group_ro_accessor
170 =item Arguments: $group, $field
172 Returns: $sub (\CODE)
176 Returns a single read-only accessor in a given group; called by
177 mk_group_ro_accessors for each entry in @fieldspec.
181 sub make_group_ro_accessor {
182 my($class, $group, $field) = @_;
184 my $get = "get_$group";
191 croak("'$caller' cannot alter the value of '$field' on ".
192 "objects of class '$class'");
195 return $self->$get($field);
200 =head2 make_group_wo_accessor
204 =item Arguments: $group, $field
206 Returns: $sub (\CODE)
210 Returns a single write-only accessor in a given group; called by
211 mk_group_wo_accessors for each entry in @fieldspec.
215 sub make_group_wo_accessor {
216 my($class, $group, $field) = @_;
218 my $set = "set_$group";
225 croak("'$caller' cannot access the value of '$field' on ".
226 "objects of class '$class'");
229 return $self->$set($field, @_);
238 =item Arguments: $field
244 Simple getter for hash-based objects which returns the value for the field
245 name passed as an argument.
250 my ($self, $get) = @_;
251 return $self->{$get};
258 =item Arguments: $field, $new_value
264 Simple setter for hash-based objects which sets and then returns the value
265 for the field name passed as an argument.
270 my ($self, $set, $val) = @_;
271 return $self->{$set} = $val;
279 =item Arguments: $field
285 Simple getter for Classes and hash-based objects which returns the value for the field name passed as
286 an argument. This behaves much like L<Class::Data::Accessor> where the field can be set in a
287 base class, inherited and changed in subclasses, and inherited and changed for object instances.
292 my ($self, $get) = @_;
295 if (reftype($self) eq 'HASH' && exists $self->{$get}) {
296 return $self->{$get};
297 } elsif (reftype($self) ne 'HASH') {
298 croak('Cannot get inherited value on an object instance that is not hash-based');
304 my @supers = Class::ISA::self_and_super_path(ref $self || $self);
306 return ${$_.'::_'.$get} if defined(${$_.'::_'.$get});
316 =item Arguments: $field, $new_value
322 Simple setter for Classes and hash-based objects which sets and then returns the value
323 for the field name passed as an argument. When called on a hash-based object it will set the appropriate
324 hash key value. When called on a class, it will set a class level variable.
326 B<Note:>: This method will die if you try to set an object variable on a non hash-based object.
331 my ($self, $set, $val) = @_;
334 if (reftype($self) eq 'HASH') {
335 return $self->{$set} = $val;
337 croak('Cannot set inherited value on an object instance that is not hash-based');
342 return ${$self.'::_'.$set} = $val;
350 Matt S. Trout <mst@shadowcatsystems.co.uk>
354 You may distribute this code under the same terms as Perl itself.