1 package Class::Accessor::Grouped;
5 use Class::Inspector ();
7 use Scalar::Util qw/reftype blessed/;
15 Class::Accessor::Grouped - Lets you build groups of accessors
21 This class lets you build groups of accessors that will call different
26 =head2 mk_group_accessors
30 =item Arguments: $group, @fieldspec
36 Creates a set of accessors in a given group.
38 $group is the name of the accessor group for the generated accessors; they
39 will call get_$group($field) on get and set_$group($field, $value) on set.
41 @fieldspec is a list of field/accessor names; if a fieldspec is a scalar
42 this is used as both field and accessor name, if a listref it is expected to
43 be of the form [ $accessor, $field ].
47 sub mk_group_accessors {
48 my ($self, $group, @fields) = @_;
50 $self->_mk_group_accessors('make_group_accessor', $group, @fields);
57 no warnings 'redefine';
59 sub _mk_group_accessors {
60 my($self, $maker, $group, @fields) = @_;
61 my $class = blessed $self || $self;
63 # So we don't have to do lots of lookups inside the loop.
64 $maker = $self->can($maker) unless ref $maker;
66 foreach my $field (@fields) {
67 if( $field eq 'DESTROY' ) {
68 carp("Having a data accessor named DESTROY in ".
69 "'$class' is unwise.");
74 ($name, $field) = @$field if ref $field;
76 my $accessor = $self->$maker($group, $field);
77 my $alias = "_${name}_accessor";
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
286 the field name passed as an argument. This behaves much like
287 L<Class::Data::Accessor> where the field can be set in a base class,
288 inherited and changed in subclasses, and inherited and changed for object
294 my ($self, $get) = @_;
298 my $reftype = reftype $self;
301 if ($reftype eq 'HASH' && exists $self->{$get}) {
302 return $self->{$get};
303 } elsif ($reftype ne 'HASH') {
304 croak('Cannot get inherited value on an object instance that is not hash-based');
311 return ${$class.'::__cag_'.$get} if defined(${$class.'::__cag_'.$get});
313 if (!@{$class.'::__cag_supers'}) {
314 @{$class.'::__cag_supers'} = $self->get_super_paths;
317 foreach (@{$class.'::__cag_supers'}) {
318 return ${$_.'::__cag_'.$get} if defined(${$_.'::__cag_'.$get});
328 =item Arguments: $field, $new_value
334 Simple setter for Classes and hash-based objects which sets and then returns
335 the value for the field name passed as an argument. When called on a hash-based
336 object it will set the appropriate hash key value. When called on a class, it
337 will set a class level variable.
339 B<Note:>: This method will die if you try to set an object variable on a non
345 my ($self, $set, $val) = @_;
348 if (reftype $self eq 'HASH') {
349 return $self->{$set} = $val;
351 croak('Cannot set inherited value on an object instance that is not hash-based');
356 return ${$self.'::__cag_'.$set} = $val;
360 =head2 get_component_class
364 =item Arguments: $field
370 Gets the value of the specified component class.
372 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
374 $self->result_class->method();
377 $self->get_component_class('result_class')->method();
381 sub get_component_class {
382 my ($self, $field) = @_;
384 return $self->get_inherited($field);
387 =head2 set_component_class
391 =item Arguments: $field, $class
397 Inherited accessor that automatically loads the specified class before setting
398 it. This method will die if the specified class could not be loaded.
400 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
401 __PACKAGE__->result_class('MyClass');
403 $self->result_class->method();
407 sub set_component_class {
408 my ($self, $field, $value) = @_;
412 if (Class::Inspector->installed($value) && !Class::Inspector->loaded($value)) {
415 croak("Could not load $field '$value': ", $@) if $@;
419 return $self->set_inherited($field, $value);
422 =head2 get_super_paths
424 Returns a list of 'parent' or 'super' class names that the current class inherited from.
428 sub get_super_paths {
429 my $class = blessed $_[0] || $_[0];
431 return Class::ISA::super_path($class);
438 Matt S. Trout <mst@shadowcatsystems.co.uk>
439 Christopher H. Laco <claco@chrislaco.com>
443 You may distribute this code under the same terms as Perl itself.