inflate refactor
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / InflateColumn.pm
CommitLineData
0e5c2582 1package DBIx::Class::InflateColumn;
2
3use strict;
4use warnings;
aa562407 5
aa562407 6
75a23b3e 7use base qw/DBIx::Class::Row/;
0e5c2582 8
75d07914 9=head1 NAME
bcae85db 10
11DBIx::Class::InflateColumn - Automatically create objects from column data
12
13=head1 SYNOPSIS
14
15 # In your table classes
16 __PACKAGE__->inflate_column('column_name', {
17 inflate => sub { ... },
18 deflate => sub { ... },
19 });
20
21=head1 DESCRIPTION
22
23This component translates column data into objects, i.e. "inflating"
24the column data. It also "deflates" objects into an appropriate format
25for the database.
26
27It can be used, for example, to automatically convert to and from
75d07914 28L<DateTime> objects for your date and time fields.
bcae85db 29
30=head1 METHODS
31
32=head2 inflate_column
33
75d07914 34Instruct L<DBIx::Class> to inflate the given column.
bcae85db 35
36In addition to the column name, you must provide C<inflate> and
37C<deflate> methods. The C<inflate> method is called when you access
38the field, while the C<deflate> method is called when the field needs
39to used by the database.
40
41For example, if you have a table C<events> with a timestamp field
42named C<insert_time>, you could inflate the column in the
43corresponding table class using something like:
44
45 __PACKAGE__->inflate_column('insert_time', {
46 inflate => sub { DateTime::Format::Pg->parse_datetime(shift); },
47 deflate => sub { DateTime::Format::Pg->format_datetime(shift); },
48 });
49
50(Replace L<DateTime::Format::Pg> with the appropriate module for your
51database, or consider L<DateTime::Format::DBI>.)
52
06fc5fc9 53The coderefs you set for inflate and deflate are called with two parameters,
54the first is the value of the column to be inflated/deflated, the second is the
55row object itself. Thus you can call C<< ->result_source->schema->storage->dbh >> on
56it, to feed to L<DateTime::Format::DBI>.
57
bcae85db 58In this example, calls to an event's C<insert_time> accessor return a
59L<DateTime> object. This L<DateTime> object is later "deflated" when
60used in the database layer.
61
62=cut
63
0e5c2582 64sub inflate_column {
65 my ($self, $col, $attrs) = @_;
bc0c9800 66 $self->throw_exception("No such column $col to inflate")
67 unless $self->has_column($col);
68 $self->throw_exception("inflate_column needs attr hashref")
69 unless ref $attrs eq 'HASH';
103647d5 70 $self->column_info($col)->{_inflate_info} = $attrs;
0e5c2582 71 $self->mk_group_accessors('inflated_column' => $col);
72 return 1;
73}
74
4a07648a 75sub _inflated_column {
0e5c2582 76 my ($self, $col, $value) = @_;
9f300b1b 77 return $value unless defined $value; # NULL is NULL is NULL
bc0c9800 78 my $info = $self->column_info($col)
79 or $self->throw_exception("No column info for $col");
103647d5 80 return $value unless exists $info->{_inflate_info};
81 my $inflate = $info->{_inflate_info}{inflate};
701da8c4 82 $self->throw_exception("No inflator for $col") unless defined $inflate;
0e5c2582 83 return $inflate->($value, $self);
84}
85
180c7679 86sub _deflate_column {
87 my ($self, $col) = @_;
88 return if exists $self->{_column_data}{$col};
89 my $value = $self->{_inflated_column}{$col};
90 if (ref $value) {
91 my $info = $self->column_info($col) or
92 $self->throw_exception("No column info for $col");
93 if (exists $info->{_inflate_info}) {
94 my $deflate = $info->{_inflate_info}{deflate};
95 $self->throw_exception("No deflator for $col") unless defined $deflate;
96 $value = $deflate->($value, $self);
97 }
98 }
99 $self->store_column($col, $value);
0e5c2582 100}
101
7eb4ecc8 102=head2 get_inflated_column
103
104 my $val = $obj->get_inflated_column($col);
105
106Fetch a column value in its inflated state. This is directly
107analogous to L<DBIx::Class::Row/get_column> in that it only fetches a
108column already retreived from the database, and then inflates it.
109Throws an exception if the column requested is not an inflated column.
110
111=cut
112
0e5c2582 113sub get_inflated_column {
114 my ($self, $col) = @_;
bc0c9800 115 $self->throw_exception("$col is not an inflated column")
116 unless exists $self->column_info($col)->{_inflate_info};
4a07648a 117
0e5c2582 118 return $self->{_inflated_column}{$col}
119 if exists $self->{_inflated_column}{$col};
0e5c2582 120 return $self->{_inflated_column}{$col} =
4a07648a 121 $self->_inflated_column($col, $self->get_column($col));
0e5c2582 122}
123
7eb4ecc8 124=head2 set_inflated_column
125
126 my $copy = $obj->set_inflated_column($col => $val);
127
128Sets a column value from an inflated value. This is directly
129analogous to L<DBIx::Class::Row/set_column>.
130
131=cut
132
0e5c2582 133sub set_inflated_column {
180c7679 134 my ($self, $col, $obj) = @_;
135 my $old = $self->get_inflated_column($col);
136 my $ret = $self->store_inflated_column($col, $obj);
137 $self->{_dirty_columns}{$col} = 1
138 if (defined $old ^ defined $ret) || (defined $old && $old ne $ret);
0e5c2582 139 return $ret;
140}
141
7eb4ecc8 142=head2 store_inflated_column
143
144 my $copy = $obj->store_inflated_column($col => $val);
145
146Sets a column value from an inflated value without marking the column
147as dirty. This is directly analogous to
148L<DBIx::Class::Row/store_column>.
149
150=cut
151
0e5c2582 152sub store_inflated_column {
180c7679 153 my ($self, $col, $obj) = @_;
0e5c2582 154 unless (ref $obj) {
155 delete $self->{_inflated_column}{$col};
180c7679 156 return $self->store_column($col, $obj);
0e5c2582 157 }
180c7679 158 delete $self->{_column_data}{$col};
159 return $self->{_inflated_column}{$col} = $obj;
160}
4a07648a 161
180c7679 162sub get_column {
163 my ($self, $col) = @_;
164 $self->_deflate_column($col);
165 return $self->next::method($col);
166}
4a07648a 167
180c7679 168sub get_columns {
169 my $self = shift;
170 if (exists $self->{_inflated_column}) {
171 $self->_deflate_column($_) for keys %{$self->{_inflated_column}};
172 }
173 return $self->next::method;
174}
175
176sub has_column_loaded {
177 my ($self, $col) = @_;
178 return 1 if exists $self->{_inflated_column}{$col};
179 return $self->next::method($col);
0e5c2582 180}
181
7eb4ecc8 182=head2 update
183
184Updates a row in the same way as L<DBIx::Class::Row/update>, handling
185inflation and deflation of columns appropriately.
186
187=cut
188
9fcda149 189sub update {
190 my ($class, $attrs, @rest) = @_;
180c7679 191 foreach my $key (keys %{$attrs||{}}) {
9fcda149 192 if (ref $attrs->{$key}
193 && exists $class->column_info($key)->{_inflate_info}) {
180c7679 194 $class->set_inflated_column($key, delete $attrs->{$key});
9fcda149 195 }
196 }
197 return $class->next::method($attrs, @rest);
198}
199
7eb4ecc8 200=head2 new
201
202Creates a row in the same way as L<DBIx::Class::Row/new>, handling
203inflation and deflation of columns appropriately.
204
205=cut
206
0e5c2582 207sub new {
208 my ($class, $attrs, @rest) = @_;
180c7679 209 my $inflated;
210 foreach my $key (keys %{$attrs||{}}) {
211 $inflated->{$key} = delete $attrs->{$key}
212 if ref $attrs->{$key} && exists $class->column_info($key)->{_inflate_info};
0e5c2582 213 }
180c7679 214 my $obj = $class->next::method($attrs, @rest);
215 $obj->{_inflated_column} = $inflated if $inflated;
216 return $obj;
0e5c2582 217}
218
bcae85db 219=head1 SEE ALSO
220
221=over 4
222
223=item L<DBIx::Class::Core> - This component is loaded as part of the
224 "core" L<DBIx::Class> components; generally there is no need to
225 load it directly
226
227=back
228
229=head1 AUTHOR
230
231Matt S. Trout <mst@shadowcatsystems.co.uk>
232
233=head1 CONTRIBUTORS
234
235Daniel Westermann-Clark <danieltwc@cpan.org> (documentation)
236
237=head1 LICENSE
238
239You may distribute this code under the same terms as Perl itself.
240
241=cut
242
0e5c2582 2431;