Commit | Line | Data |
0e5c2582 |
1 | package DBIx::Class::InflateColumn; |
2 | |
3 | use strict; |
4 | use warnings; |
75a23b3e |
5 | use base qw/DBIx::Class::Row/; |
0e5c2582 |
6 | |
7 | sub inflate_column { |
8 | my ($self, $col, $attrs) = @_; |
103647d5 |
9 | die "No such column $col to inflate" unless $self->has_column($col); |
0e5c2582 |
10 | die "inflate_column needs attr hashref" unless ref $attrs eq 'HASH'; |
103647d5 |
11 | $self->column_info($col)->{_inflate_info} = $attrs; |
0e5c2582 |
12 | $self->mk_group_accessors('inflated_column' => $col); |
13 | return 1; |
14 | } |
15 | |
4a07648a |
16 | sub _inflated_column { |
0e5c2582 |
17 | my ($self, $col, $value) = @_; |
9f300b1b |
18 | return $value unless defined $value; # NULL is NULL is NULL |
103647d5 |
19 | my $info = $self->column_info($col) || die "No column info for $col"; |
20 | return $value unless exists $info->{_inflate_info}; |
21 | my $inflate = $info->{_inflate_info}{inflate}; |
22 | die "No inflator for $col" unless defined $inflate; |
0e5c2582 |
23 | return $inflate->($value, $self); |
24 | } |
25 | |
4a07648a |
26 | sub _deflated_column { |
0e5c2582 |
27 | my ($self, $col, $value) = @_; |
28 | return $value unless ref $value; # If it's not an object, don't touch it |
103647d5 |
29 | my $info = $self->column_info($col) || die "No column info for $col"; |
30 | return $value unless exists $info->{_inflate_info}; |
31 | my $deflate = $info->{_inflate_info}{deflate}; |
32 | die "No deflator for $col" unless defined $deflate; |
0e5c2582 |
33 | return $deflate->($value, $self); |
34 | } |
35 | |
36 | sub get_inflated_column { |
37 | my ($self, $col) = @_; |
38 | $self->throw("$col is not an inflated column") unless |
103647d5 |
39 | exists $self->column_info($col)->{_inflate_info}; |
4a07648a |
40 | |
0e5c2582 |
41 | return $self->{_inflated_column}{$col} |
42 | if exists $self->{_inflated_column}{$col}; |
0e5c2582 |
43 | return $self->{_inflated_column}{$col} = |
4a07648a |
44 | $self->_inflated_column($col, $self->get_column($col)); |
0e5c2582 |
45 | } |
46 | |
47 | sub set_inflated_column { |
48 | my ($self, $col, @rest) = @_; |
47bd0267 |
49 | my $ret = $self->_inflated_column_op('set', $col, @rest); |
0e5c2582 |
50 | return $ret; |
51 | } |
52 | |
53 | sub store_inflated_column { |
47bd0267 |
54 | my ($self, $col, @rest) = @_; |
55 | my $ret = $self->_inflated_column_op('store', $col, @rest); |
56 | return $ret; |
57 | } |
58 | |
59 | sub _inflated_column_op { |
60 | my ($self, $op, $col, $obj) = @_; |
61 | my $meth = "${op}_column"; |
0e5c2582 |
62 | unless (ref $obj) { |
63 | delete $self->{_inflated_column}{$col}; |
47bd0267 |
64 | return $self->$meth($col, $obj); |
0e5c2582 |
65 | } |
4a07648a |
66 | |
67 | my $deflated = $self->_deflated_column($col, $obj); |
9f300b1b |
68 | # Do this now so we don't store if it's invalid |
4a07648a |
69 | |
0e5c2582 |
70 | $self->{_inflated_column}{$col} = $obj; |
47bd0267 |
71 | $self->$meth($col, $deflated); |
0e5c2582 |
72 | return $obj; |
73 | } |
74 | |
75 | sub new { |
76 | my ($class, $attrs, @rest) = @_; |
77 | $attrs ||= {}; |
0e5c2582 |
78 | foreach my $key (keys %$attrs) { |
103647d5 |
79 | if (ref $attrs->{$key} |
80 | && exists $class->column_info($key)->{_inflate_info}) { |
484c9dda |
81 | $attrs->{$key} = $class->_deflated_column($key, $attrs->{$key}); |
0e5c2582 |
82 | } |
83 | } |
147dd158 |
84 | return $class->next::method($attrs, @rest); |
0e5c2582 |
85 | } |
86 | |
0e5c2582 |
87 | 1; |