1 package Class::Accessor::Grouped;
5 use Class::Inspector ();
10 our $VERSION = '0.09000';
16 return $hasXS if defined $hasXS;
20 require Class::XSAccessor;
30 Class::Accessor::Grouped - Lets you build groups of accessors
36 This class lets you build groups of accessors that will call different
41 =head2 mk_group_accessors
45 =item Arguments: $group, @fieldspec
51 Creates a set of accessors in a given group.
53 $group is the name of the accessor group for the generated accessors; they
54 will call get_$group($field) on get and set_$group($field, $value) on set.
56 If you want to mimic Class::Accessor's mk_accessors $group has to be 'simple'
57 to tell Class::Accessor::Grouped to use its own get_simple and set_simple
60 @fieldspec is a list of field/accessor names; if a fieldspec is a scalar
61 this is used as both field and accessor name, if a listref it is expected to
62 be of the form [ $accessor, $field ].
66 sub mk_group_accessors {
67 my ($self, $group, @fields) = @_;
69 $self->_mk_group_accessors('make_group_accessor', $group, @fields);
76 no warnings 'redefine';
78 sub _mk_group_accessors {
79 my($self, $maker, $group, @fields) = @_;
80 my $class = Scalar::Util::blessed $self || $self;
82 # So we don't have to do lots of lookups inside the loop.
83 $maker = $self->can($maker) unless ref $maker;
87 foreach my $field (@fields) {
88 if( $field eq 'DESTROY' ) {
89 Carp::carp("Having a data accessor named DESTROY in ".
90 "'$class' is unwise.");
95 ($name, $field) = @$field if ref $field;
97 my $alias = "_${name}_accessor";
98 my $full_name = join('::', $class, $name);
99 my $full_alias = join('::', $class, $alias);
101 if ( $hasXS && $group eq 'simple' ) {
102 Class::XSAccessor::newxs_accessor("${class}::${name}", $field, 0);
103 Class::XSAccessor::newxs_accessor("${class}::${alias}", $field, 0);
105 # XXX: is the alias accessor really necessary?
108 my $accessor = $self->$maker($group, $field);
109 my $alias_accessor = $self->$maker($group, $field);
111 *$full_name = Sub::Name::subname($full_name, $accessor);
112 #unless defined &{$class."\:\:$field"}
114 *$full_alias = Sub::Name::subname($full_alias, $alias_accessor);
115 #unless defined &{$class."\:\:$alias"}
121 =head2 mk_group_ro_accessors
125 =item Arguments: $group, @fieldspec
131 Creates a set of read only accessors in a given group. Identical to
132 <L:/mk_group_accessors> but accessors will throw an error if passed a value
133 rather than setting the value.
137 sub mk_group_ro_accessors {
138 my($self, $group, @fields) = @_;
140 $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields);
143 =head2 mk_group_wo_accessors
147 =item Arguments: $group, @fieldspec
153 Creates a set of write only accessors in a given group. Identical to
154 <L:/mk_group_accessors> but accessors will throw an error if not passed a
155 value rather than getting the value.
159 sub mk_group_wo_accessors {
160 my($self, $group, @fields) = @_;
162 $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
165 =head2 make_group_accessor
169 =item Arguments: $group, $field
171 Returns: $sub (\CODE)
175 Returns a single accessor in a given group; called by mk_group_accessors
176 for each entry in @fieldspec.
180 sub make_group_accessor {
181 my ($class, $group, $field) = @_;
183 my $set = "set_$group";
184 my $get = "get_$group";
186 # eval for faster fastiness
189 return shift->$set('$field', \@_);
192 return shift->$get('$field');
197 =head2 make_group_ro_accessor
201 =item Arguments: $group, $field
203 Returns: $sub (\CODE)
207 Returns a single read-only accessor in a given group; called by
208 mk_group_ro_accessors for each entry in @fieldspec.
212 sub make_group_ro_accessor {
213 my($class, $group, $field) = @_;
215 my $get = "get_$group";
219 my \$caller = caller;
220 Carp::croak(\"'\$caller' cannot alter the value of '$field' on \".
221 \"objects of class '$class'\");
224 return shift->$get('$field');
229 =head2 make_group_wo_accessor
233 =item Arguments: $group, $field
235 Returns: $sub (\CODE)
239 Returns a single write-only accessor in a given group; called by
240 mk_group_wo_accessors for each entry in @fieldspec.
244 sub make_group_wo_accessor {
245 my($class, $group, $field) = @_;
247 my $set = "set_$group";
251 my \$caller = caller;
252 Carp::croak(\"'\$caller' cannot access the value of '$field' on \".
253 \"objects of class '$class'\");
256 return shift->$set('$field', \@_);
265 =item Arguments: $field
271 Simple getter for hash-based objects which returns the value for the field
272 name passed as an argument.
277 return $_[0]->{$_[1]};
284 =item Arguments: $field, $new_value
290 Simple setter for hash-based objects which sets and then returns the value
291 for the field name passed as an argument.
296 return $_[0]->{$_[1]} = $_[2];
304 =item Arguments: $field
310 Simple getter for Classes and hash-based objects which returns the value for
311 the field name passed as an argument. This behaves much like
312 L<Class::Data::Accessor> where the field can be set in a base class,
313 inherited and changed in subclasses, and inherited and changed for object
321 if (Scalar::Util::blessed $_[0]) {
322 my $reftype = Scalar::Util::reftype $_[0];
325 if ($reftype eq 'HASH' && exists $_[0]->{$_[1]}) {
326 return $_[0]->{$_[1]};
327 } elsif ($reftype ne 'HASH') {
328 Carp::croak('Cannot get inherited value on an object instance that is not hash-based');
335 no warnings qw/uninitialized/;
336 return ${$class.'::__cag_'.$_[1]} if defined(${$class.'::__cag_'.$_[1]});
338 # we need to be smarter about recalculation, as @ISA (thus supers) can very well change in-flight
339 my $pkg_gen = mro::get_pkg_gen ($class);
340 if ( ${$class.'::__cag_pkg_gen'} != $pkg_gen ) {
341 @{$class.'::__cag_supers'} = $_[0]->get_super_paths;
342 ${$class.'::__cag_pkg_gen'} = $pkg_gen;
345 foreach (@{$class.'::__cag_supers'}) {
346 return ${$_.'::__cag_'.$_[1]} if defined(${$_.'::__cag_'.$_[1]});
356 =item Arguments: $field, $new_value
362 Simple setter for Classes and hash-based objects which sets and then returns
363 the value for the field name passed as an argument. When called on a hash-based
364 object it will set the appropriate hash key value. When called on a class, it
365 will set a class level variable.
367 B<Note:>: This method will die if you try to set an object variable on a non
373 if (Scalar::Util::blessed $_[0]) {
374 if (Scalar::Util::reftype $_[0] eq 'HASH') {
375 return $_[0]->{$_[1]} = $_[2];
377 Carp::croak('Cannot set inherited value on an object instance that is not hash-based');
382 return ${$_[0].'::__cag_'.$_[1]} = $_[2];
386 =head2 get_component_class
390 =item Arguments: $field
396 Gets the value of the specified component class.
398 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
400 $self->result_class->method();
403 $self->get_component_class('result_class')->method();
407 sub get_component_class {
408 return $_[0]->get_inherited($_[1]);
411 =head2 set_component_class
415 =item Arguments: $field, $class
421 Inherited accessor that automatically loads the specified class before setting
422 it. This method will die if the specified class could not be loaded.
424 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
425 __PACKAGE__->result_class('MyClass');
427 $self->result_class->method();
431 sub set_component_class {
434 if (Class::Inspector->installed($_[2]) && !Class::Inspector->loaded($_[2])) {
437 Carp::croak("Could not load $_[1] '$_[2]': ", $@) if $@;
441 return $_[0]->set_inherited($_[1], $_[2]);
444 =head2 get_super_paths
446 Returns a list of 'parent' or 'super' class names that the current class inherited from.
450 sub get_super_paths {
451 my $class = Scalar::Util::blessed $_[0] || $_[0];
453 return @{mro::get_linear_isa($class)};
460 Matt S. Trout <mst@shadowcatsystems.co.uk>
461 Christopher H. Laco <claco@chrislaco.com>
463 With contributions from:
465 Guillermo Roditi <groditi@cpan.org>
469 You may distribute this code under the same terms as Perl itself.