1 package Class::Accessor::Grouped;
5 use Class::Inspector ();
10 our $VERSION = '0.09001';
11 $VERSION = eval $VERSION;
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 = Scalar::Util::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;
72 foreach my $field (@fields) {
73 if( $field eq 'DESTROY' ) {
74 Carp::carp("Having a data accessor named DESTROY in ".
75 "'$class' is unwise.");
80 ($name, $field) = @$field if ref $field;
82 my $alias = "_${name}_accessor";
83 my $full_name = join('::', $class, $name);
84 my $full_alias = join('::', $class, $alias);
86 if ( $hasXS && $group eq 'simple' ) {
87 Class::XSAccessor::newxs_accessor("${class}::${name}", $field, 0);
88 Class::XSAccessor::newxs_accessor("${class}::${alias}", $field, 0);
90 # XXX: is the alias accessor really necessary?
93 my $accessor = $self->$maker($group, $field);
94 my $alias_accessor = $self->$maker($group, $field);
96 *$full_name = Sub::Name::subname($full_name, $accessor);
97 #unless defined &{$class."\:\:$field"}
99 *$full_alias = Sub::Name::subname($full_alias, $alias_accessor);
100 #unless defined &{$class."\:\:$alias"}
106 =head2 mk_group_ro_accessors
110 =item Arguments: $group, @fieldspec
116 Creates a set of read only accessors in a given group. Identical to
117 L</mk_group_accessors> but accessors will throw an error if passed a value
118 rather than setting the value.
122 sub mk_group_ro_accessors {
123 my($self, $group, @fields) = @_;
125 $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields);
128 =head2 mk_group_wo_accessors
132 =item Arguments: $group, @fieldspec
138 Creates a set of write only accessors in a given group. Identical to
139 L</mk_group_accessors> but accessors will throw an error if not passed a
140 value rather than getting the value.
144 sub mk_group_wo_accessors {
145 my($self, $group, @fields) = @_;
147 $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
150 =head2 make_group_accessor
154 =item Arguments: $group, $field
156 Returns: $sub (\CODE)
160 Returns a single accessor in a given group; called by mk_group_accessors
161 for each entry in @fieldspec.
165 sub make_group_accessor {
166 my ($class, $group, $field) = @_;
168 my $set = "set_$group";
169 my $get = "get_$group";
171 # eval for faster fastiness
174 return shift->$set('$field', \@_);
177 return shift->$get('$field');
182 =head2 make_group_ro_accessor
186 =item Arguments: $group, $field
188 Returns: $sub (\CODE)
192 Returns a single read-only accessor in a given group; called by
193 mk_group_ro_accessors for each entry in @fieldspec.
197 sub make_group_ro_accessor {
198 my($class, $group, $field) = @_;
200 my $get = "get_$group";
204 my \$caller = caller;
205 Carp::croak(\"'\$caller' cannot alter the value of '$field' on \".
206 \"objects of class '$class'\");
209 return shift->$get('$field');
214 =head2 make_group_wo_accessor
218 =item Arguments: $group, $field
220 Returns: $sub (\CODE)
224 Returns a single write-only accessor in a given group; called by
225 mk_group_wo_accessors for each entry in @fieldspec.
229 sub make_group_wo_accessor {
230 my($class, $group, $field) = @_;
232 my $set = "set_$group";
236 my \$caller = caller;
237 Carp::croak(\"'\$caller' cannot access the value of '$field' on \".
238 \"objects of class '$class'\");
241 return shift->$set('$field', \@_);
250 =item Arguments: $field
256 Simple getter for hash-based objects which returns the value for the field
257 name passed as an argument.
262 return $_[0]->{$_[1]};
269 =item Arguments: $field, $new_value
275 Simple setter for hash-based objects which sets and then returns the value
276 for the field name passed as an argument.
281 return $_[0]->{$_[1]} = $_[2];
289 =item Arguments: $field
295 Simple getter for Classes and hash-based objects which returns the value for
296 the field name passed as an argument. This behaves much like
297 L<Class::Data::Accessor> where the field can be set in a base class,
298 inherited and changed in subclasses, and inherited and changed for object
306 if (Scalar::Util::blessed $_[0]) {
307 my $reftype = Scalar::Util::reftype $_[0];
310 if ($reftype eq 'HASH' && exists $_[0]->{$_[1]}) {
311 return $_[0]->{$_[1]};
312 } elsif ($reftype ne 'HASH') {
313 Carp::croak('Cannot get inherited value on an object instance that is not hash-based');
320 no warnings qw/uninitialized/;
321 return ${$class.'::__cag_'.$_[1]} if defined(${$class.'::__cag_'.$_[1]});
323 # we need to be smarter about recalculation, as @ISA (thus supers) can very well change in-flight
324 my $pkg_gen = mro::get_pkg_gen ($class);
325 if ( ${$class.'::__cag_pkg_gen'} != $pkg_gen ) {
326 @{$class.'::__cag_supers'} = $_[0]->get_super_paths;
327 ${$class.'::__cag_pkg_gen'} = $pkg_gen;
330 foreach (@{$class.'::__cag_supers'}) {
331 return ${$_.'::__cag_'.$_[1]} if defined(${$_.'::__cag_'.$_[1]});
341 =item Arguments: $field, $new_value
347 Simple setter for Classes and hash-based objects which sets and then returns
348 the value for the field name passed as an argument. When called on a hash-based
349 object it will set the appropriate hash key value. When called on a class, it
350 will set a class level variable.
352 B<Note:>: This method will die if you try to set an object variable on a non
358 if (Scalar::Util::blessed $_[0]) {
359 if (Scalar::Util::reftype $_[0] eq 'HASH') {
360 return $_[0]->{$_[1]} = $_[2];
362 Carp::croak('Cannot set inherited value on an object instance that is not hash-based');
367 return ${$_[0].'::__cag_'.$_[1]} = $_[2];
371 =head2 get_component_class
375 =item Arguments: $field
381 Gets the value of the specified component class.
383 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
385 $self->result_class->method();
388 $self->get_component_class('result_class')->method();
392 sub get_component_class {
393 return $_[0]->get_inherited($_[1]);
396 =head2 set_component_class
400 =item Arguments: $field, $class
406 Inherited accessor that automatically loads the specified class before setting
407 it. This method will die if the specified class could not be loaded.
409 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
410 __PACKAGE__->result_class('MyClass');
412 $self->result_class->method();
416 sub set_component_class {
419 if (Class::Inspector->installed($_[2]) && !Class::Inspector->loaded($_[2])) {
422 Carp::croak("Could not load $_[1] '$_[2]': ", $@) if $@;
426 return $_[0]->set_inherited($_[1], $_[2]);
429 =head2 get_super_paths
431 Returns a list of 'parent' or 'super' class names that the current class inherited from.
435 sub get_super_paths {
436 my $class = Scalar::Util::blessed $_[0] || $_[0];
438 return @{mro::get_linear_isa($class)};
441 # This is now a hard, rather than optional dep. Since we dep on Sub::Name, we no
442 # longer care about not using XS modules.
447 return $hasXS if defined $hasXS;
451 require Class::XSAccessor;
463 Matt S. Trout <mst@shadowcatsystems.co.uk>
464 Christopher H. Laco <claco@chrislaco.com>
466 With contributions from:
468 Guillermo Roditi <groditi@cpan.org>
470 =head1 COPYRIGHT & LICENSE
472 Copyright (c) 2006-2009 Matt S. Trout <mst@shadowcatsystems.co.uk>
474 This program is free software; you can redistribute it and/or modify
475 it under the same terms as perl itself.