1 package DBIx::Class::InflateColumn;
5 use base qw/DBIx::Class::Row/;
9 DBIx::Class::InflateColumn - Automatically create objects from column data
13 # In your table classes
14 __PACKAGE__->inflate_column('column_name', {
15 inflate => sub { ... },
16 deflate => sub { ... },
21 This component translates column data into objects, i.e. "inflating"
22 the column data. It also "deflates" objects into an appropriate format
25 It can be used, for example, to automatically convert to and from
26 L<DateTime> objects for your date and time fields.
32 Instruct L<DBIx::Class> to inflate the given column.
34 In addition to the column name, you must provide C<inflate> and
35 C<deflate> methods. The C<inflate> method is called when you access
36 the field, while the C<deflate> method is called when the field needs
37 to used by the database.
39 For example, if you have a table C<events> with a timestamp field
40 named C<insert_time>, you could inflate the column in the
41 corresponding table class using something like:
43 __PACKAGE__->inflate_column('insert_time', {
44 inflate => sub { DateTime::Format::Pg->parse_datetime(shift); },
45 deflate => sub { DateTime::Format::Pg->format_datetime(shift); },
48 (Replace L<DateTime::Format::Pg> with the appropriate module for your
49 database, or consider L<DateTime::Format::DBI>.)
51 In this example, calls to an event's C<insert_time> accessor return a
52 L<DateTime> object. This L<DateTime> object is later "deflated" when
53 used in the database layer.
58 my ($self, $col, $attrs) = @_;
59 die "No such column $col to inflate" unless $self->has_column($col);
60 die "inflate_column needs attr hashref" unless ref $attrs eq 'HASH';
61 $self->column_info($col)->{_inflate_info} = $attrs;
62 $self->mk_group_accessors('inflated_column' => $col);
66 sub _inflated_column {
67 my ($self, $col, $value) = @_;
68 return $value unless defined $value; # NULL is NULL is NULL
69 my $info = $self->column_info($col) || die "No column info for $col";
70 return $value unless exists $info->{_inflate_info};
71 my $inflate = $info->{_inflate_info}{inflate};
72 die "No inflator for $col" unless defined $inflate;
73 return $inflate->($value, $self);
76 sub _deflated_column {
77 my ($self, $col, $value) = @_;
78 return $value unless ref $value; # If it's not an object, don't touch it
79 my $info = $self->column_info($col) || die "No column info for $col";
80 return $value unless exists $info->{_inflate_info};
81 my $deflate = $info->{_inflate_info}{deflate};
82 die "No deflator for $col" unless defined $deflate;
83 return $deflate->($value, $self);
86 sub get_inflated_column {
87 my ($self, $col) = @_;
88 $self->throw("$col is not an inflated column") unless
89 exists $self->column_info($col)->{_inflate_info};
91 return $self->{_inflated_column}{$col}
92 if exists $self->{_inflated_column}{$col};
93 return $self->{_inflated_column}{$col} =
94 $self->_inflated_column($col, $self->get_column($col));
97 sub set_inflated_column {
98 my ($self, $col, @rest) = @_;
99 my $ret = $self->_inflated_column_op('set', $col, @rest);
103 sub store_inflated_column {
104 my ($self, $col, @rest) = @_;
105 my $ret = $self->_inflated_column_op('store', $col, @rest);
109 sub _inflated_column_op {
110 my ($self, $op, $col, $obj) = @_;
111 my $meth = "${op}_column";
113 delete $self->{_inflated_column}{$col};
114 return $self->$meth($col, $obj);
117 my $deflated = $self->_deflated_column($col, $obj);
118 # Do this now so we don't store if it's invalid
120 $self->{_inflated_column}{$col} = $obj;
121 $self->$meth($col, $deflated);
126 my ($class, $attrs, @rest) = @_;
128 foreach my $key (keys %$attrs) {
129 if (ref $attrs->{$key}
130 && exists $class->column_info($key)->{_inflate_info}) {
131 $attrs->{$key} = $class->_deflated_column($key, $attrs->{$key});
134 return $class->next::method($attrs, @rest);
141 =item L<DBIx::Class::Core> - This component is loaded as part of the
142 "core" L<DBIx::Class> components; generally there is no need to
149 Matt S. Trout <mst@shadowcatsystems.co.uk>
153 Daniel Westermann-Clark <danieltwc@cpan.org> (documentation)
157 You may distribute this code under the same terms as Perl itself.