Merge 'trunk' into 'DBIx-Class-current'
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / AccessorGroup.pm
CommitLineData
fe5d862b 1package DBIx::Class::AccessorGroup;
2
12bbb339 3use strict;
4use warnings;
5
701da8c4 6use Carp::Clan qw/^DBIx::Class/;
7
34d52be2 8=head1 NAME
9
10DBIx::Class::AccessorGroup - Lets you build groups of accessors
11
12=head1 SYNOPSIS
13
14=head1 DESCRIPTION
15
16This class lets you build groups of accessors that will call different
17getters and setters.
18
19=head1 METHODS
20
34d52be2 21=cut
22
fe5d862b 23sub mk_group_accessors {
24 my($self, $group, @fields) = @_;
25
26 $self->_mk_group_accessors('make_group_accessor', $group, @fields);
27}
28
29
30{
31 no strict 'refs';
12bbb339 32 no warnings 'redefine';
fe5d862b 33
34 sub _mk_group_accessors {
35 my($self, $maker, $group, @fields) = @_;
36 my $class = ref $self || $self;
37
38 # So we don't have to do lots of lookups inside the loop.
39 $maker = $self->can($maker) unless ref $maker;
40
41 foreach my $field (@fields) {
42 if( $field eq 'DESTROY' ) {
701da8c4 43 carp("Having a data accessor named DESTROY in ".
fe5d862b 44 "'$class' is unwise.");
45 }
46
b8e1e21f 47 my $name = $field;
48
49 ($name, $field) = @$field if ref $field;
50
fe5d862b 51 my $accessor = $self->$maker($group, $field);
b8e1e21f 52 my $alias = "_${name}_accessor";
fe5d862b 53
12bbb339 54 #warn "$class $group $field $alias";
fe5d862b 55
b8e1e21f 56 *{$class."\:\:$name"} = $accessor;
12bbb339 57 #unless defined &{$class."\:\:$field"}
58
59 *{$class."\:\:$alias"} = $accessor;
60 #unless defined &{$class."\:\:$alias"}
fe5d862b 61 }
62 }
63}
64
65sub mk_group_ro_accessors {
66 my($self, $group, @fields) = @_;
67
68 $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields);
69}
70
71sub mk_group_wo_accessors {
72 my($self, $group, @fields) = @_;
73
74 $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
75}
76
77sub make_group_accessor {
78 my ($class, $group, $field) = @_;
79
80 my $set = "set_$group";
81 my $get = "get_$group";
82
83 # Build a closure around $field.
84 return sub {
85 my $self = shift;
86
87 if(@_) {
12bbb339 88 return $self->$set($field, @_);
fe5d862b 89 }
90 else {
12bbb339 91 return $self->$get($field);
fe5d862b 92 }
93 };
94}
95
96sub make_group_ro_accessor {
97 my($class, $group, $field) = @_;
98
99 my $get = "get_$group";
100
101 return sub {
102 my $self = shift;
103
104 if(@_) {
105 my $caller = caller;
701da8c4 106 croak("'$caller' cannot alter the value of '$field' on ".
fe5d862b 107 "objects of class '$class'");
108 }
109 else {
12bbb339 110 return $self->$get($field);
fe5d862b 111 }
112 };
113}
114
115sub make_group_wo_accessor {
116 my($class, $group, $field) = @_;
117
118 my $set = "set_$group";
119
120 return sub {
121 my $self = shift;
122
123 unless (@_) {
124 my $caller = caller;
701da8c4 125 croak("'$caller' cannot access the value of '$field' on ".
fe5d862b 126 "objects of class '$class'");
127 }
128 else {
12bbb339 129 return $self->$set($field, @_);
fe5d862b 130 }
131 };
132}
133
484c9dda 134sub get_simple {
135 my ($self, $get) = @_;
136 return $self->{$get};
137}
138
139sub set_simple {
140 my ($self, $set, $val) = @_;
141 return $self->{$set} = $val;
142}
143
fc969005 144sub get_component_class {
145 my ($self, $get) = @_;
146 if (ref $self) {
147 return $self->{$get};
148 } else {
149 $get = "_$get";
150 return $self->can($get) ? $self->$get : undef;
151 }
152}
153
154sub set_component_class {
155 my ($self, $set, $val) = @_;
156 eval "require $val";
157 if (ref $self) {
158 return $self->{$set} = $val;
159 } else {
160 $set = "_$set";
161 return $self->can($set) ? $self->$set($val) : $self->mk_classdata($set => $val);
162 }
163}
164
fe5d862b 1651;
34d52be2 166
34d52be2 167=head1 AUTHORS
168
daec44b8 169Matt S. Trout <mst@shadowcatsystems.co.uk>
34d52be2 170
171=head1 LICENSE
172
173You may distribute this code under the same terms as Perl itself.
174
175=cut
176