1 package Class::Accessor::Grouped;
9 our $VERSION = '0.09005';
10 $VERSION = eval $VERSION;
12 # Class::XSAccessor is segfaulting on win32, so be careful
13 # Win32 users can set $hasXS to try to use it anyway
18 if (not defined $hasXS) {
21 if ($^O ne 'MSWin32') {
23 require Class::XSAccessor;
34 Class::Accessor::Grouped - Lets you build groups of accessors
40 This class lets you build groups of accessors that will call different
45 =head2 mk_group_accessors
49 =item Arguments: $group, @fieldspec
55 Creates a set of accessors in a given group.
57 $group is the name of the accessor group for the generated accessors; they
58 will call get_$group($field) on get and set_$group($field, $value) on set.
60 If you want to mimic Class::Accessor's mk_accessors $group has to be 'simple'
61 to tell Class::Accessor::Grouped to use its own get_simple and set_simple
64 @fieldspec is a list of field/accessor names; if a fieldspec is a scalar
65 this is used as both field and accessor name, if a listref it is expected to
66 be of the form [ $accessor, $field ].
70 sub mk_group_accessors {
71 my ($self, $group, @fields) = @_;
73 $self->_mk_group_accessors('make_group_accessor', $group, @fields);
80 no warnings 'redefine';
82 sub _mk_group_accessors {
83 my($self, $maker, $group, @fields) = @_;
84 my $class = Scalar::Util::blessed $self || $self;
86 # So we don't have to do lots of lookups inside the loop.
87 $maker = $self->can($maker) unless ref $maker;
91 foreach my $field (@fields) {
92 if( $field eq 'DESTROY' ) {
93 Carp::carp("Having a data accessor named DESTROY in ".
94 "'$class' is unwise.");
99 ($name, $field) = @$field if ref $field;
101 my $alias = "_${name}_accessor";
102 my $full_name = join('::', $class, $name);
103 my $full_alias = join('::', $class, $alias);
104 if ( $hasXS && $group eq 'simple' ) {
105 require Class::XSAccessor;
106 Class::XSAccessor->import({
116 my $accessor = $self->$maker($group, $field);
117 my $alias_accessor = $self->$maker($group, $field);
119 *$full_name = Sub::Name::subname($full_name, $accessor);
120 #unless defined &{$class."\:\:$field"}
122 *$full_alias = Sub::Name::subname($full_alias, $alias_accessor);
123 #unless defined &{$class."\:\:$alias"}
129 =head2 mk_group_ro_accessors
133 =item Arguments: $group, @fieldspec
139 Creates a set of read only accessors in a given group. Identical to
140 L</mk_group_accessors> but accessors will throw an error if passed a value
141 rather than setting the value.
145 sub mk_group_ro_accessors {
146 my($self, $group, @fields) = @_;
148 $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields);
151 =head2 mk_group_wo_accessors
155 =item Arguments: $group, @fieldspec
161 Creates a set of write only accessors in a given group. Identical to
162 L</mk_group_accessors> but accessors will throw an error if not passed a
163 value rather than getting the value.
167 sub mk_group_wo_accessors {
168 my($self, $group, @fields) = @_;
170 $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
173 =head2 make_group_accessor
177 =item Arguments: $group, $field
179 Returns: $sub (\CODE)
183 Returns a single accessor in a given group; called by mk_group_accessors
184 for each entry in @fieldspec.
188 sub make_group_accessor {
189 my ($class, $group, $field) = @_;
191 my $set = "set_$group";
192 my $get = "get_$group";
196 # eval for faster fastiness
197 my $code = eval "sub {
199 return shift->$set('$field', \@_);
202 return shift->$get('$field');
205 Carp::croak $@ if $@;
210 =head2 make_group_ro_accessor
214 =item Arguments: $group, $field
216 Returns: $sub (\CODE)
220 Returns a single read-only accessor in a given group; called by
221 mk_group_ro_accessors for each entry in @fieldspec.
225 sub make_group_ro_accessor {
226 my($class, $group, $field) = @_;
228 my $get = "get_$group";
232 my $code = eval "sub {
234 my \$caller = caller;
235 Carp::croak(\"'\$caller' cannot alter the value of '$field' on \".
236 \"objects of class '$class'\");
239 return shift->$get('$field');
242 Carp::croak $@ if $@;
247 =head2 make_group_wo_accessor
251 =item Arguments: $group, $field
253 Returns: $sub (\CODE)
257 Returns a single write-only accessor in a given group; called by
258 mk_group_wo_accessors for each entry in @fieldspec.
262 sub make_group_wo_accessor {
263 my($class, $group, $field) = @_;
265 my $set = "set_$group";
269 my $code = eval "sub {
271 my \$caller = caller;
272 Carp::croak(\"'\$caller' cannot access the value of '$field' on \".
273 \"objects of class '$class'\");
276 return shift->$set('$field', \@_);
279 Carp::croak $@ if $@;
288 =item Arguments: $field
294 Simple getter for hash-based objects which returns the value for the field
295 name passed as an argument.
300 return $_[0]->{$_[1]};
307 =item Arguments: $field, $new_value
313 Simple setter for hash-based objects which sets and then returns the value
314 for the field name passed as an argument.
319 return $_[0]->{$_[1]} = $_[2];
327 =item Arguments: $field
333 Simple getter for Classes and hash-based objects which returns the value for
334 the field name passed as an argument. This behaves much like
335 L<Class::Data::Accessor> where the field can be set in a base class,
336 inherited and changed in subclasses, and inherited and changed for object
344 if ( ($class = ref $_[0]) && Scalar::Util::blessed $_[0]) {
345 if (Scalar::Util::reftype $_[0] eq 'HASH') {
346 return $_[0]->{$_[1]} if exists $_[0]->{$_[1]};
349 Carp::croak('Cannot get inherited value on an object instance that is not hash-based');
357 no warnings qw/uninitialized/;
359 my $cag_slot = '::__cag_'. $_[1];
360 return ${$class.$cag_slot} if defined(${$class.$cag_slot});
362 # we need to be smarter about recalculation, as @ISA (thus supers) can very well change in-flight
363 my $cur_gen = mro::get_pkg_gen ($class);
364 if ( $cur_gen != ${$class.'::__cag_pkg_gen__'} ) {
365 @{$class.'::__cag_supers__'} = $_[0]->get_super_paths;
366 ${$class.'::__cag_pkg_gen__'} = $cur_gen;
369 for (@{$class.'::__cag_supers__'}) {
370 return ${$_.$cag_slot} if defined(${$_.$cag_slot});
380 =item Arguments: $field, $new_value
386 Simple setter for Classes and hash-based objects which sets and then returns
387 the value for the field name passed as an argument. When called on a hash-based
388 object it will set the appropriate hash key value. When called on a class, it
389 will set a class level variable.
391 B<Note:>: This method will die if you try to set an object variable on a non
397 if (Scalar::Util::blessed $_[0]) {
398 if (Scalar::Util::reftype $_[0] eq 'HASH') {
399 return $_[0]->{$_[1]} = $_[2];
401 Carp::croak('Cannot set inherited value on an object instance that is not hash-based');
406 return ${$_[0].'::__cag_'.$_[1]} = $_[2];
410 =head2 get_component_class
414 =item Arguments: $field
420 Gets the value of the specified component class.
422 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
424 $self->result_class->method();
427 $self->get_component_class('result_class')->method();
431 sub get_component_class {
432 return $_[0]->get_inherited($_[1]);
435 =head2 set_component_class
439 =item Arguments: $field, $class
445 Inherited accessor that automatically loads the specified class before setting
446 it. This method will die if the specified class could not be loaded.
448 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
449 __PACKAGE__->result_class('MyClass');
451 $self->result_class->method();
455 sub set_component_class {
458 require Class::Inspector;
459 if (Class::Inspector->installed($_[2]) && !Class::Inspector->loaded($_[2])) {
462 Carp::croak("Could not load $_[1] '$_[2]': ", $@) if $@;
466 return $_[0]->set_inherited($_[1], $_[2]);
469 =head2 get_super_paths
471 Returns a list of 'parent' or 'super' class names that the current class inherited from.
475 sub get_super_paths {
476 return @{mro::get_linear_isa( ref($_[0]) || $_[0] )};
483 You can speed up accessors of type 'simple' by installing L<Class::XSAccessor>.
487 Matt S. Trout <mst@shadowcatsystems.co.uk>
489 Christopher H. Laco <claco@chrislaco.com>
493 Caelum: Rafael Kitover <rkitover@cpan.org>
495 groditi: Guillermo Roditi <groditi@cpan.org>
497 Jason Plum <jason.plum@bmmsi.com>
499 ribasushi: Peter Rabbitson <ribasushi@cpan.org>
502 =head1 COPYRIGHT & LICENSE
504 Copyright (c) 2006-2010 Matt S. Trout <mst@shadowcatsystems.co.uk>
506 This program is free software; you can redistribute it and/or modify
507 it under the same terms as perl itself.