1 package DBIx::Class::AccessorGroup;
10 DBIx::Class::AccessorGroup - Lets you build groups of accessors
16 This class lets you build groups of accessors that will call different
25 sub mk_group_accessors {
26 my($self, $group, @fields) = @_;
28 $self->_mk_group_accessors('make_group_accessor', $group, @fields);
34 no warnings 'redefine';
36 sub _mk_group_accessors {
37 my($self, $maker, $group, @fields) = @_;
38 my $class = ref $self || $self;
40 # So we don't have to do lots of lookups inside the loop.
41 $maker = $self->can($maker) unless ref $maker;
43 foreach my $field (@fields) {
44 if( $field eq 'DESTROY' ) {
46 &Carp::carp("Having a data accessor named DESTROY in ".
47 "'$class' is unwise.");
52 ($name, $field) = @$field if ref $field;
54 my $accessor = $self->$maker($group, $field);
55 my $alias = "_${name}_accessor";
57 #warn "$class $group $field $alias";
59 *{$class."\:\:$name"} = $accessor;
60 #unless defined &{$class."\:\:$field"}
62 *{$class."\:\:$alias"} = $accessor;
63 #unless defined &{$class."\:\:$alias"}
68 sub mk_group_ro_accessors {
69 my($self, $group, @fields) = @_;
71 $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields);
74 sub mk_group_wo_accessors {
75 my($self, $group, @fields) = @_;
77 $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
80 sub make_group_accessor {
81 my ($class, $group, $field) = @_;
83 my $set = "set_$group";
84 my $get = "get_$group";
86 # Build a closure around $field.
91 return $self->$set($field, @_);
94 return $self->$get($field);
99 sub make_group_ro_accessor {
100 my($class, $group, $field) = @_;
102 my $get = "get_$group";
110 Carp::croak("'$caller' cannot alter the value of '$field' on ".
111 "objects of class '$class'");
114 return $self->$get($field);
119 sub make_group_wo_accessor {
120 my($class, $group, $field) = @_;
122 my $set = "set_$group";
130 Carp::croak("'$caller' cannot access the value of '$field' on ".
131 "objects of class '$class'");
134 return $self->$set($field, @_);
145 Matt S. Trout <perl-stuff@trout.me.uk>
149 You may distribute this code under the same terms as Perl itself.