1 package Class::Accessor::Grouped;
5 use Class::Inspector ();
10 our $VERSION = '0.08003';
14 Class::Accessor::Grouped - Lets you build groups of accessors
20 This class lets you build groups of accessors that will call different
25 =head2 mk_group_accessors
29 =item Arguments: $group, @fieldspec
35 Creates a set of accessors in a given group.
37 $group is the name of the accessor group for the generated accessors; they
38 will call get_$group($field) on get and set_$group($field, $value) on set.
40 If you want to mimic Class::Accessor's mk_accessors $group has to be 'simple'
41 to tell Class::Accessor::Grouped to use its own get_simple and set_simple
44 @fieldspec is a list of field/accessor names; if a fieldspec is a scalar
45 this is used as both field and accessor name, if a listref it is expected to
46 be of the form [ $accessor, $field ].
50 sub mk_group_accessors {
51 my ($self, $group, @fields) = @_;
53 $self->_mk_group_accessors('make_group_accessor', $group, @fields);
60 no warnings 'redefine';
62 sub _mk_group_accessors {
63 my($self, $maker, $group, @fields) = @_;
64 my $class = Scalar::Util::blessed $self || $self;
66 # So we don't have to do lots of lookups inside the loop.
67 $maker = $self->can($maker) unless ref $maker;
69 foreach my $field (@fields) {
70 if( $field eq 'DESTROY' ) {
71 Carp::carp("Having a data accessor named DESTROY in ".
72 "'$class' is unwise.");
77 ($name, $field) = @$field if ref $field;
79 my $accessor = $self->$maker($group, $field);
80 my $alias_accessor = $self->$maker($group, $field);
82 my $alias = "_${name}_accessor";
83 my $full_name = join('::', $class, $name);
84 my $full_alias = join('::', $class, $alias);
86 *$full_name = Sub::Name::subname($full_name, $accessor);
87 #unless defined &{$class."\:\:$field"}
88 *$full_alias = Sub::Name::subname($full_alias, $alias_accessor);
89 #unless defined &{$class."\:\:$alias"}
94 =head2 mk_group_ro_accessors
98 =item Arguments: $group, @fieldspec
104 Creates a set of read only accessors in a given group. Identical to
105 <L:/mk_group_accessors> but accessors will throw an error if passed a value
106 rather than setting the value.
110 sub mk_group_ro_accessors {
111 my($self, $group, @fields) = @_;
113 $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields);
116 =head2 mk_group_wo_accessors
120 =item Arguments: $group, @fieldspec
126 Creates a set of write only accessors in a given group. Identical to
127 <L:/mk_group_accessors> but accessors will throw an error if not passed a
128 value rather than getting the value.
132 sub mk_group_wo_accessors {
133 my($self, $group, @fields) = @_;
135 $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
138 =head2 make_group_accessor
142 =item Arguments: $group, $field
144 Returns: $sub (\CODE)
148 Returns a single accessor in a given group; called by mk_group_accessors
149 for each entry in @fieldspec.
153 sub make_group_accessor {
154 my ($class, $group, $field) = @_;
156 my $set = "set_$group";
157 my $get = "get_$group";
159 # eval for faster fastiness
162 return shift->$set('$field', \@_);
165 return shift->$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";
192 my \$caller = caller;
193 Carp::croak(\"'\$caller' cannot alter the value of '$field' on \".
194 \"objects of class '$class'\");
197 return shift->$get('$field');
202 =head2 make_group_wo_accessor
206 =item Arguments: $group, $field
208 Returns: $sub (\CODE)
212 Returns a single write-only accessor in a given group; called by
213 mk_group_wo_accessors for each entry in @fieldspec.
217 sub make_group_wo_accessor {
218 my($class, $group, $field) = @_;
220 my $set = "set_$group";
224 my \$caller = caller;
225 Carp::croak(\"'\$caller' cannot access the value of '$field' on \".
226 \"objects of class '$class'\");
229 return shift->$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 return $_[0]->{$_[1]};
257 =item Arguments: $field, $new_value
263 Simple setter for hash-based objects which sets and then returns the value
264 for the field name passed as an argument.
269 return $_[0]->{$_[1]} = $_[2];
277 =item Arguments: $field
283 Simple getter for Classes and hash-based objects which returns the value for
284 the field name passed as an argument. This behaves much like
285 L<Class::Data::Accessor> where the field can be set in a base class,
286 inherited and changed in subclasses, and inherited and changed for object
294 if (Scalar::Util::blessed $_[0]) {
295 my $reftype = Scalar::Util::reftype $_[0];
298 if ($reftype eq 'HASH' && exists $_[0]->{$_[1]}) {
299 return $_[0]->{$_[1]};
300 } elsif ($reftype ne 'HASH') {
301 Carp::croak('Cannot get inherited value on an object instance that is not hash-based');
308 no warnings qw/uninitialized/;
309 return ${$class.'::__cag_'.$_[1]} if defined(${$class.'::__cag_'.$_[1]});
311 # we need to be smarter about recalculation, as @ISA (thus supers) can very well change in-flight
312 my $pkg_gen = mro::get_pkg_gen ($class);
313 if ( ${$class.'::__cag_pkg_gen'} != $pkg_gen ) {
314 @{$class.'::__cag_supers'} = $_[0]->get_super_paths;
315 ${$class.'::__cag_pkg_gen'} = $pkg_gen;
318 foreach (@{$class.'::__cag_supers'}) {
319 return ${$_.'::__cag_'.$_[1]} if defined(${$_.'::__cag_'.$_[1]});
329 =item Arguments: $field, $new_value
335 Simple setter for Classes and hash-based objects which sets and then returns
336 the value for the field name passed as an argument. When called on a hash-based
337 object it will set the appropriate hash key value. When called on a class, it
338 will set a class level variable.
340 B<Note:>: This method will die if you try to set an object variable on a non
346 if (Scalar::Util::blessed $_[0]) {
347 if (Scalar::Util::reftype $_[0] eq 'HASH') {
348 return $_[0]->{$_[1]} = $_[2];
350 Carp::croak('Cannot set inherited value on an object instance that is not hash-based');
355 return ${$_[0].'::__cag_'.$_[1]} = $_[2];
359 =head2 get_component_class
363 =item Arguments: $field
369 Gets the value of the specified component class.
371 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
373 $self->result_class->method();
376 $self->get_component_class('result_class')->method();
380 sub get_component_class {
381 return $_[0]->get_inherited($_[1]);
384 =head2 set_component_class
388 =item Arguments: $field, $class
394 Inherited accessor that automatically loads the specified class before setting
395 it. This method will die if the specified class could not be loaded.
397 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
398 __PACKAGE__->result_class('MyClass');
400 $self->result_class->method();
404 sub set_component_class {
407 if (Class::Inspector->installed($_[2]) && !Class::Inspector->loaded($_[2])) {
410 Carp::croak("Could not load $_[1] '$_[2]': ", $@) if $@;
414 return $_[0]->set_inherited($_[1], $_[2]);
417 =head2 get_super_paths
419 Returns a list of 'parent' or 'super' class names that the current class inherited from.
423 sub get_super_paths {
424 my $class = Scalar::Util::blessed $_[0] || $_[0];
426 return @{mro::get_linear_isa($class)};
433 Matt S. Trout <mst@shadowcatsystems.co.uk>
434 Christopher H. Laco <claco@chrislaco.com>
436 With contributions from:
438 Guillermo Roditi <groditi@cpan.org>
442 You may distribute this code under the same terms as Perl itself.