1 package Class::Accessor::Grouped;
5 use Class::Inspector ();
9 our $VERSION = '0.08003';
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 If you want to mimic Class::Accessor's mk_accessors $group has to be 'simple'
40 to tell Class::Accessor::Grouped to use its own get_simple and set_simple
43 @fieldspec is a list of field/accessor names; if a fieldspec is a scalar
44 this is used as both field and accessor name, if a listref it is expected to
45 be of the form [ $accessor, $field ].
49 sub mk_group_accessors {
50 my ($self, $group, @fields) = @_;
52 $self->_mk_group_accessors('make_group_accessor', $group, @fields);
59 no warnings 'redefine';
61 sub _mk_group_accessors {
62 my($self, $maker, $group, @fields) = @_;
63 my $class = Scalar::Util::blessed $self || $self;
65 # So we don't have to do lots of lookups inside the loop.
66 $maker = $self->can($maker) unless ref $maker;
68 foreach my $field (@fields) {
69 if( $field eq 'DESTROY' ) {
70 Carp::carp("Having a data accessor named DESTROY in ".
71 "'$class' is unwise.");
76 ($name, $field) = @$field if ref $field;
78 my $accessor = $self->$maker($group, $field);
79 my $alias = "_${name}_accessor";
81 *{$class."\:\:$name"} = $accessor;
82 #unless defined &{$class."\:\:$field"}
84 *{$class."\:\:$alias"} = $accessor;
85 #unless defined &{$class."\:\:$alias"}
90 =head2 mk_group_ro_accessors
94 =item Arguments: $group, @fieldspec
100 Creates a set of read only accessors in a given group. Identical to
101 <L:/mk_group_accessors> but accessors will throw an error if passed a value
102 rather than setting the value.
106 sub mk_group_ro_accessors {
107 my($self, $group, @fields) = @_;
109 $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields);
112 =head2 mk_group_wo_accessors
116 =item Arguments: $group, @fieldspec
122 Creates a set of write only accessors in a given group. Identical to
123 <L:/mk_group_accessors> but accessors will throw an error if not passed a
124 value rather than getting the value.
128 sub mk_group_wo_accessors {
129 my($self, $group, @fields) = @_;
131 $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
134 =head2 make_group_accessor
138 =item Arguments: $group, $field
140 Returns: $sub (\CODE)
144 Returns a single accessor in a given group; called by mk_group_accessors
145 for each entry in @fieldspec.
149 sub make_group_accessor {
150 my ($class, $group, $field) = @_;
152 my $set = "set_$group";
153 my $get = "get_$group";
155 # eval for faster fastiness
158 return shift->$set('$field', \@_);
161 return shift->$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";
188 my \$caller = caller;
189 Carp::croak(\"'\$caller' cannot alter the value of '$field' on \".
190 \"objects of class '$class'\");
193 return shift->$get('$field');
198 =head2 make_group_wo_accessor
202 =item Arguments: $group, $field
204 Returns: $sub (\CODE)
208 Returns a single write-only accessor in a given group; called by
209 mk_group_wo_accessors for each entry in @fieldspec.
213 sub make_group_wo_accessor {
214 my($class, $group, $field) = @_;
216 my $set = "set_$group";
220 my \$caller = caller;
221 Carp::croak(\"'\$caller' cannot access the value of '$field' on \".
222 \"objects of class '$class'\");
225 return shift->$set('$field', \@_);
234 =item Arguments: $field
240 Simple getter for hash-based objects which returns the value for the field
241 name passed as an argument.
246 return $_[0]->{$_[1]};
253 =item Arguments: $field, $new_value
259 Simple setter for hash-based objects which sets and then returns the value
260 for the field name passed as an argument.
265 return $_[0]->{$_[1]} = $_[2];
273 =item Arguments: $field
279 Simple getter for Classes and hash-based objects which returns the value for
280 the field name passed as an argument. This behaves much like
281 L<Class::Data::Accessor> where the field can be set in a base class,
282 inherited and changed in subclasses, and inherited and changed for object
290 if (Scalar::Util::blessed $_[0]) {
291 my $reftype = Scalar::Util::reftype $_[0];
294 if ($reftype eq 'HASH' && exists $_[0]->{$_[1]}) {
295 return $_[0]->{$_[1]};
296 } elsif ($reftype ne 'HASH') {
297 Carp::croak('Cannot get inherited value on an object instance that is not hash-based');
304 no warnings qw/uninitialized/;
305 return ${$class.'::__cag_'.$_[1]} if defined(${$class.'::__cag_'.$_[1]});
307 # we need to be smarter about recalculation, as @ISA (thus supers) can very well change in-flight
308 my $pkg_gen = mro::get_pkg_gen ($class);
309 if ( ${$class.'::__cag_pkg_gen'} != $pkg_gen ) {
310 @{$class.'::__cag_supers'} = $_[0]->get_super_paths;
311 ${$class.'::__cag_pkg_gen'} = $pkg_gen;
314 foreach (@{$class.'::__cag_supers'}) {
315 return ${$_.'::__cag_'.$_[1]} if defined(${$_.'::__cag_'.$_[1]});
325 =item Arguments: $field, $new_value
331 Simple setter for Classes and hash-based objects which sets and then returns
332 the value for the field name passed as an argument. When called on a hash-based
333 object it will set the appropriate hash key value. When called on a class, it
334 will set a class level variable.
336 B<Note:>: This method will die if you try to set an object variable on a non
342 if (Scalar::Util::blessed $_[0]) {
343 if (Scalar::Util::reftype $_[0] eq 'HASH') {
344 return $_[0]->{$_[1]} = $_[2];
346 Carp::croak('Cannot set inherited value on an object instance that is not hash-based');
351 return ${$_[0].'::__cag_'.$_[1]} = $_[2];
355 =head2 get_component_class
359 =item Arguments: $field
365 Gets the value of the specified component class.
367 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
369 $self->result_class->method();
372 $self->get_component_class('result_class')->method();
376 sub get_component_class {
377 return $_[0]->get_inherited($_[1]);
380 =head2 set_component_class
384 =item Arguments: $field, $class
390 Inherited accessor that automatically loads the specified class before setting
391 it. This method will die if the specified class could not be loaded.
393 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
394 __PACKAGE__->result_class('MyClass');
396 $self->result_class->method();
400 sub set_component_class {
403 if (Class::Inspector->installed($_[2]) && !Class::Inspector->loaded($_[2])) {
406 Carp::croak("Could not load $_[1] '$_[2]': ", $@) if $@;
410 return $_[0]->set_inherited($_[1], $_[2]);
413 =head2 get_super_paths
415 Returns a list of 'parent' or 'super' class names that the current class inherited from.
419 sub get_super_paths {
420 my $class = Scalar::Util::blessed $_[0] || $_[0];
422 return @{mro::get_linear_isa($class)};
429 Matt S. Trout <mst@shadowcatsystems.co.uk>
430 Christopher H. Laco <claco@chrislaco.com>
434 You may distribute this code under the same terms as Perl itself.