1 package DBIx::Class::AccessorGroup;
8 DBIx::Class::AccessorGroup - Lets you build groups of accessors
14 This class lets you build groups of accessors that will call different
21 sub mk_group_accessors {
22 my($self, $group, @fields) = @_;
24 $self->_mk_group_accessors('make_group_accessor', $group, @fields);
30 no warnings 'redefine';
32 sub _mk_group_accessors {
33 my($self, $maker, $group, @fields) = @_;
34 my $class = ref $self || $self;
36 # So we don't have to do lots of lookups inside the loop.
37 $maker = $self->can($maker) unless ref $maker;
39 foreach my $field (@fields) {
40 if( $field eq 'DESTROY' ) {
42 &Carp::carp("Having a data accessor named DESTROY in ".
43 "'$class' is unwise.");
48 ($name, $field) = @$field if ref $field;
50 my $accessor = $self->$maker($group, $field);
51 my $alias = "_${name}_accessor";
53 #warn "$class $group $field $alias";
55 *{$class."\:\:$name"} = $accessor;
56 #unless defined &{$class."\:\:$field"}
58 *{$class."\:\:$alias"} = $accessor;
59 #unless defined &{$class."\:\:$alias"}
64 sub mk_group_ro_accessors {
65 my($self, $group, @fields) = @_;
67 $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields);
70 sub mk_group_wo_accessors {
71 my($self, $group, @fields) = @_;
73 $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
76 sub make_group_accessor {
77 my ($class, $group, $field) = @_;
79 my $set = "set_$group";
80 my $get = "get_$group";
82 # Build a closure around $field.
87 return $self->$set($field, @_);
90 return $self->$get($field);
95 sub make_group_ro_accessor {
96 my($class, $group, $field) = @_;
98 my $get = "get_$group";
106 Carp::croak("'$caller' cannot alter the value of '$field' on ".
107 "objects of class '$class'");
110 return $self->$get($field);
115 sub make_group_wo_accessor {
116 my($class, $group, $field) = @_;
118 my $set = "set_$group";
126 Carp::croak("'$caller' cannot access the value of '$field' on ".
127 "objects of class '$class'");
130 return $self->$set($field, @_);
136 my ($self, $get) = @_;
137 return $self->{$get};
141 my ($self, $set, $val) = @_;
142 return $self->{$set} = $val;
149 Matt S. Trout <mst@shadowcatsystems.co.uk>
153 You may distribute this code under the same terms as Perl itself.