1 package Class::Accessor::Grouped;
5 use Class::Inspector ();
10 our $VERSION = '0.09003';
11 $VERSION = eval $VERSION;
16 if (not 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);
100 if ( $hasXS && $group eq 'simple' ) {
101 require Class::XSAccessor;
102 Class::XSAccessor->import({
112 my $accessor = $self->$maker($group, $field);
113 my $alias_accessor = $self->$maker($group, $field);
115 *$full_name = Sub::Name::subname($full_name, $accessor);
116 #unless defined &{$class."\:\:$field"}
118 *$full_alias = Sub::Name::subname($full_alias, $alias_accessor);
119 #unless defined &{$class."\:\:$alias"}
125 =head2 mk_group_ro_accessors
129 =item Arguments: $group, @fieldspec
135 Creates a set of read only accessors in a given group. Identical to
136 L</mk_group_accessors> but accessors will throw an error if passed a value
137 rather than setting the value.
141 sub mk_group_ro_accessors {
142 my($self, $group, @fields) = @_;
144 $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields);
147 =head2 mk_group_wo_accessors
151 =item Arguments: $group, @fieldspec
157 Creates a set of write only accessors in a given group. Identical to
158 L</mk_group_accessors> but accessors will throw an error if not passed a
159 value rather than getting the value.
163 sub mk_group_wo_accessors {
164 my($self, $group, @fields) = @_;
166 $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
169 =head2 make_group_accessor
173 =item Arguments: $group, $field
175 Returns: $sub (\CODE)
179 Returns a single accessor in a given group; called by mk_group_accessors
180 for each entry in @fieldspec.
184 sub make_group_accessor {
185 my ($class, $group, $field) = @_;
187 my $set = "set_$group";
188 my $get = "get_$group";
190 # eval for faster fastiness
191 my $code = eval "sub {
193 return shift->$set('$field', \@_);
196 return shift->$get('$field');
199 Carp::croak $@ if $@;
204 =head2 make_group_ro_accessor
208 =item Arguments: $group, $field
210 Returns: $sub (\CODE)
214 Returns a single read-only accessor in a given group; called by
215 mk_group_ro_accessors for each entry in @fieldspec.
219 sub make_group_ro_accessor {
220 my($class, $group, $field) = @_;
222 my $get = "get_$group";
224 my $code = eval "sub {
226 my \$caller = caller;
227 Carp::croak(\"'\$caller' cannot alter the value of '$field' on \".
228 \"objects of class '$class'\");
231 return shift->$get('$field');
234 Carp::croak $@ if $@;
239 =head2 make_group_wo_accessor
243 =item Arguments: $group, $field
245 Returns: $sub (\CODE)
249 Returns a single write-only accessor in a given group; called by
250 mk_group_wo_accessors for each entry in @fieldspec.
254 sub make_group_wo_accessor {
255 my($class, $group, $field) = @_;
257 my $set = "set_$group";
259 my $code = eval "sub {
261 my \$caller = caller;
262 Carp::croak(\"'\$caller' cannot access the value of '$field' on \".
263 \"objects of class '$class'\");
266 return shift->$set('$field', \@_);
269 Carp::croak $@ if $@;
278 =item Arguments: $field
284 Simple getter for hash-based objects which returns the value for the field
285 name passed as an argument.
290 return $_[0]->{$_[1]};
297 =item Arguments: $field, $new_value
303 Simple setter for hash-based objects which sets and then returns the value
304 for the field name passed as an argument.
309 return $_[0]->{$_[1]} = $_[2];
317 =item Arguments: $field
323 Simple getter for Classes and hash-based objects which returns the value for
324 the field name passed as an argument. This behaves much like
325 L<Class::Data::Accessor> where the field can be set in a base class,
326 inherited and changed in subclasses, and inherited and changed for object
334 if ( ($class = ref $_[0]) && Scalar::Util::blessed $_[0]) {
335 if (Scalar::Util::reftype $_[0] eq 'HASH') {
336 return $_[0]->{$_[1]} if exists $_[0]->{$_[1]};
339 Carp::croak('Cannot get inherited value on an object instance that is not hash-based');
347 no warnings qw/uninitialized/;
349 my $cag_slot = '::__cag_'. $_[1];
350 return ${$class.$cag_slot} if defined(${$class.$cag_slot});
352 # we need to be smarter about recalculation, as @ISA (thus supers) can very well change in-flight
353 my $cur_gen = mro::get_pkg_gen ($class);
354 if ( $cur_gen != ${$class.'::__cag_pkg_gen__'} ) {
355 @{$class.'::__cag_supers__'} = $_[0]->get_super_paths;
356 ${$class.'::__cag_pkg_gen__'} = $cur_gen;
359 for (@{$class.'::__cag_supers__'}) {
360 return ${$_.$cag_slot} if defined(${$_.$cag_slot});
370 =item Arguments: $field, $new_value
376 Simple setter for Classes and hash-based objects which sets and then returns
377 the value for the field name passed as an argument. When called on a hash-based
378 object it will set the appropriate hash key value. When called on a class, it
379 will set a class level variable.
381 B<Note:>: This method will die if you try to set an object variable on a non
387 if (Scalar::Util::blessed $_[0]) {
388 if (Scalar::Util::reftype $_[0] eq 'HASH') {
389 return $_[0]->{$_[1]} = $_[2];
391 Carp::croak('Cannot set inherited value on an object instance that is not hash-based');
396 return ${$_[0].'::__cag_'.$_[1]} = $_[2];
400 =head2 get_component_class
404 =item Arguments: $field
410 Gets the value of the specified component class.
412 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
414 $self->result_class->method();
417 $self->get_component_class('result_class')->method();
421 sub get_component_class {
422 return $_[0]->get_inherited($_[1]);
425 =head2 set_component_class
429 =item Arguments: $field, $class
435 Inherited accessor that automatically loads the specified class before setting
436 it. This method will die if the specified class could not be loaded.
438 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
439 __PACKAGE__->result_class('MyClass');
441 $self->result_class->method();
445 sub set_component_class {
448 if (Class::Inspector->installed($_[2]) && !Class::Inspector->loaded($_[2])) {
451 Carp::croak("Could not load $_[1] '$_[2]': ", $@) if $@;
455 return $_[0]->set_inherited($_[1], $_[2]);
458 =head2 get_super_paths
460 Returns a list of 'parent' or 'super' class names that the current class inherited from.
464 sub get_super_paths {
465 return @{mro::get_linear_isa( ref($_[0]) || $_[0] )};
472 You can speed up accessors of type 'simple' by installing L<Class::XSAccessor>.
476 Matt S. Trout <mst@shadowcatsystems.co.uk>
477 Christopher H. Laco <claco@chrislaco.com>
481 groditi: Guillermo Roditi <groditi@cpan.org>
482 ribasushi: Peter Rabbitson <ribasushi@cpan.org>
484 =head1 COPYRIGHT & LICENSE
486 Copyright (c) 2006-2010 Matt S. Trout <mst@shadowcatsystems.co.uk>
488 This program is free software; you can redistribute it and/or modify
489 it under the same terms as perl itself.