1 package DBIx::Class::AccessorGroup;
6 use Carp::Clan qw/^DBIx::Class/;
10 DBIx::Class::AccessorGroup - Lets you build groups of accessors
16 This class lets you build groups of accessors that will call different
21 =head2 mk_group_accessors
25 =item Arguments: $group, @fieldspec
31 Creates a set of accessors in a given group.
33 $group is the name of the accessor group for the generated accessors; they
34 will call get_$group($field) on get and set_$group($field, $value) on set.
36 @fieldspec is a list of field/accessor names; if a fieldspec is a scalar
37 this is used as both field and accessor name, if a listref it is expected to
38 be of the form [ $accessor, $field ].
42 sub mk_group_accessors {
43 my ($self, $group, @fields) = @_;
45 $self->_mk_group_accessors('make_group_accessor', $group, @fields);
52 no warnings 'redefine';
54 sub _mk_group_accessors {
55 my($self, $maker, $group, @fields) = @_;
56 my $class = ref $self || $self;
58 # So we don't have to do lots of lookups inside the loop.
59 $maker = $self->can($maker) unless ref $maker;
61 foreach my $field (@fields) {
62 if( $field eq 'DESTROY' ) {
63 carp("Having a data accessor named DESTROY in ".
64 "'$class' is unwise.");
69 ($name, $field) = @$field if ref $field;
71 my $accessor = $self->$maker($group, $field);
72 my $alias = "_${name}_accessor";
74 #warn "$class $group $field $alias";
76 *{$class."\:\:$name"} = $accessor;
77 #unless defined &{$class."\:\:$field"}
79 *{$class."\:\:$alias"} = $accessor;
80 #unless defined &{$class."\:\:$alias"}
85 =head2 mk_group_ro_accessors
89 =item Arguments: $group, @fieldspec
95 Creates a set of read only accessors in a given group. Identical to
96 <L:/mk_group_accessors> but accessors will throw an error if passed a value
97 rather than setting the value.
101 sub mk_group_ro_accessors {
102 my($self, $group, @fields) = @_;
104 $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields);
107 =head2 mk_group_wo_accessors
111 =item Arguments: $group, @fieldspec
117 Creates a set of write only accessors in a given group. Identical to
118 <L:/mk_group_accessors> but accessors will throw an error if not passed a
119 value rather than getting the value.
123 sub mk_group_wo_accessors {
124 my($self, $group, @fields) = @_;
126 $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
129 =head2 make_group_accessor
133 =item Arguments: $group, $field
135 Returns: $sub (\CODE)
139 Returns a single accessor in a given group; called by mk_group_accessors
140 for each entry in @fieldspec.
144 sub make_group_accessor {
145 my ($class, $group, $field) = @_;
147 my $set = "set_$group";
148 my $get = "get_$group";
150 # Build a closure around $field.
155 return $self->$set($field, @_);
158 return $self->$get($field);
163 =head2 make_group_ro_accessor
167 =item Arguments: $group, $field
169 Returns: $sub (\CODE)
173 Returns a single read-only accessor in a given group; called by
174 mk_group_ro_accessors for each entry in @fieldspec.
178 sub make_group_ro_accessor {
179 my($class, $group, $field) = @_;
181 my $get = "get_$group";
188 croak("'$caller' cannot alter the value of '$field' on ".
189 "objects of class '$class'");
192 return $self->$get($field);
197 =head2 make_group_wo_accessor
201 =item Arguments: $group, $field
203 Returns: $sub (\CODE)
207 Returns a single write-only accessor in a given group; called by
208 mk_group_wo_accessors for each entry in @fieldspec.
212 sub make_group_wo_accessor {
213 my($class, $group, $field) = @_;
215 my $set = "set_$group";
222 croak("'$caller' cannot access the value of '$field' on ".
223 "objects of class '$class'");
226 return $self->$set($field, @_);
235 =item Arguments: $field
241 Simple getter for hash-based objects which returns the value for the field
242 name passed as an argument.
247 my ($self, $get) = @_;
248 return $self->{$get};
255 =item Arguments: $field, $new_value
261 Simple setter for hash-based objects which sets and then returns the value
262 for the field name passed as an argument.
267 my ($self, $set, $val) = @_;
268 return $self->{$set} = $val;
271 =head2 get_component_class
275 =item Arguments: $name
277 Returns: $component_class
281 Returns the class name for a component; returns an object key if called on
282 an object, or attempts to return classdata referenced by _$name if called
287 sub get_component_class {
288 my ($self, $get) = @_;
290 return $self->{$get};
293 return $self->can($get) ? $self->$get : undef;
297 =head2 set_component_class
301 =item Arguments: $name, $new_component_class
303 Returns: $new_component_class
307 Sets a component class name; attempts to require the class before setting
308 but does not error if unable to do so. Sets an object key of the given name
309 if called or an object or classdata called _$name if called on a class.
313 sub set_component_class {
314 my ($self, $set, $val) = @_;
317 return $self->{$set} = $val;
320 return $self->can($set) ?
322 $self->mk_classdata($set => $val);
330 Matt S. Trout <mst@shadowcatsystems.co.uk>
334 You may distribute this code under the same terms as Perl itself.