1 package Class::Accessor::Grouped;
5 use Class::Inspector ();
6 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 If you want to mimic Class::Accessor's mk_accessors $group has to be 'simple'
42 to tell Class::Accessor::Grouped to use its own get_simple and set_simple
45 @fieldspec is a list of field/accessor names; if a fieldspec is a scalar
46 this is used as both field and accessor name, if a listref it is expected to
47 be of the form [ $accessor, $field ].
51 sub mk_group_accessors {
52 my ($self, $group, @fields) = @_;
54 $self->_mk_group_accessors('make_group_accessor', $group, @fields);
61 no warnings 'redefine';
63 sub _mk_group_accessors {
64 my($self, $maker, $group, @fields) = @_;
65 my $class = blessed $self || $self;
67 # So we don't have to do lots of lookups inside the loop.
68 $maker = $self->can($maker) unless ref $maker;
70 foreach my $field (@fields) {
71 if( $field eq 'DESTROY' ) {
72 carp("Having a data accessor named DESTROY in ".
73 "'$class' is unwise.");
78 ($name, $field) = @$field if ref $field;
80 my $accessor = $self->$maker($group, $field);
81 my $alias = "_${name}_accessor";
83 *{$class."\:\:$name"} = $accessor;
84 #unless defined &{$class."\:\:$field"}
86 *{$class."\:\:$alias"} = $accessor;
87 #unless defined &{$class."\:\:$alias"}
92 =head2 mk_group_ro_accessors
96 =item Arguments: $group, @fieldspec
102 Creates a set of read only accessors in a given group. Identical to
103 <L:/mk_group_accessors> but accessors will throw an error if passed a value
104 rather than setting the value.
108 sub mk_group_ro_accessors {
109 my($self, $group, @fields) = @_;
111 $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields);
114 =head2 mk_group_wo_accessors
118 =item Arguments: $group, @fieldspec
124 Creates a set of write only accessors in a given group. Identical to
125 <L:/mk_group_accessors> but accessors will throw an error if not passed a
126 value rather than getting the value.
130 sub mk_group_wo_accessors {
131 my($self, $group, @fields) = @_;
133 $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
136 =head2 make_group_accessor
140 =item Arguments: $group, $field
142 Returns: $sub (\CODE)
146 Returns a single accessor in a given group; called by mk_group_accessors
147 for each entry in @fieldspec.
151 sub make_group_accessor {
152 my ($class, $group, $field) = @_;
154 my $set = "set_$group";
155 my $get = "get_$group";
157 # Build a closure around $field.
162 return $self->$set($field, @_);
165 return $self->$get($field);
170 =head2 make_group_ro_accessor
174 =item Arguments: $group, $field
176 Returns: $sub (\CODE)
180 Returns a single read-only accessor in a given group; called by
181 mk_group_ro_accessors for each entry in @fieldspec.
185 sub make_group_ro_accessor {
186 my($class, $group, $field) = @_;
188 my $get = "get_$group";
195 croak("'$caller' cannot alter the value of '$field' on ".
196 "objects of class '$class'");
199 return $self->$get($field);
204 =head2 make_group_wo_accessor
208 =item Arguments: $group, $field
210 Returns: $sub (\CODE)
214 Returns a single write-only accessor in a given group; called by
215 mk_group_wo_accessors for each entry in @fieldspec.
219 sub make_group_wo_accessor {
220 my($class, $group, $field) = @_;
222 my $set = "set_$group";
229 croak("'$caller' cannot access the value of '$field' on ".
230 "objects of class '$class'");
233 return $self->$set($field, @_);
242 =item Arguments: $field
248 Simple getter for hash-based objects which returns the value for the field
249 name passed as an argument.
254 my ($self, $get) = @_;
255 return $self->{$get};
262 =item Arguments: $field, $new_value
268 Simple setter for hash-based objects which sets and then returns the value
269 for the field name passed as an argument.
274 my ($self, $set, $val) = @_;
275 return $self->{$set} = $val;
283 =item Arguments: $field
289 Simple getter for Classes and hash-based objects which returns the value for
290 the field name passed as an argument. This behaves much like
291 L<Class::Data::Accessor> where the field can be set in a base class,
292 inherited and changed in subclasses, and inherited and changed for object
298 my ($self, $get) = @_;
302 my $reftype = reftype $self;
305 if ($reftype eq 'HASH' && exists $self->{$get}) {
306 return $self->{$get};
307 } elsif ($reftype ne 'HASH') {
308 croak('Cannot get inherited value on an object instance that is not hash-based');
315 return ${$class.'::__cag_'.$get} if defined(${$class.'::__cag_'.$get});
317 if (!@{$class.'::__cag_supers'}) {
318 @{$class.'::__cag_supers'} = $self->get_super_paths;
321 foreach (@{$class.'::__cag_supers'}) {
322 return ${$_.'::__cag_'.$get} if defined(${$_.'::__cag_'.$get});
332 =item Arguments: $field, $new_value
338 Simple setter for Classes and hash-based objects which sets and then returns
339 the value for the field name passed as an argument. When called on a hash-based
340 object it will set the appropriate hash key value. When called on a class, it
341 will set a class level variable.
343 B<Note:>: This method will die if you try to set an object variable on a non
349 my ($self, $set, $val) = @_;
352 if (reftype $self eq 'HASH') {
353 return $self->{$set} = $val;
355 croak('Cannot set inherited value on an object instance that is not hash-based');
360 return ${$self.'::__cag_'.$set} = $val;
364 =head2 get_component_class
368 =item Arguments: $field
374 Gets the value of the specified component class.
376 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
378 $self->result_class->method();
381 $self->get_component_class('result_class')->method();
385 sub get_component_class {
386 my ($self, $field) = @_;
388 return $self->get_inherited($field);
391 =head2 set_component_class
395 =item Arguments: $field, $class
401 Inherited accessor that automatically loads the specified class before setting
402 it. This method will die if the specified class could not be loaded.
404 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
405 __PACKAGE__->result_class('MyClass');
407 $self->result_class->method();
411 sub set_component_class {
412 my ($self, $field, $value) = @_;
416 if (Class::Inspector->installed($value) && !Class::Inspector->loaded($value)) {
419 croak("Could not load $field '$value': ", $@) if $@;
423 return $self->set_inherited($field, $value);
426 =head2 get_super_paths
428 Returns a list of 'parent' or 'super' class names that the current class inherited from.
432 sub get_super_paths {
433 my $class = blessed $_[0] || $_[0];
435 return @{mro::get_linear_isa($class)};
442 Matt S. Trout <mst@shadowcatsystems.co.uk>
443 Christopher H. Laco <claco@chrislaco.com>
447 You may distribute this code under the same terms as Perl itself.