better namiology
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / FilterColumn.pm
CommitLineData
51bec050 1package DBIx::Class::FilterColumn;
2
3use strict;
4use warnings;
5
6use base qw/DBIx::Class::Row/;
7
8sub filter_column {
9 my ($self, $col, $attrs) = @_;
10
11 $self->throw_exception("No such column $col to filter")
12 unless $self->has_column($col);
13
14 $self->throw_exception("filter_column needs attr hashref")
15 unless ref $attrs eq 'HASH';
16
17 $self->column_info($col)->{_filter_info} = $attrs;
18 my $acc = $self->column_info($col)->{accessor};
cc2d2ead 19 $self->mk_group_accessors('value' => [ (defined $acc ? $acc : $col), $col]);
51bec050 20 return 1;
21}
22
4c0c3038 23sub _column_from_storage {
51bec050 24 my ($self, $col, $value) = @_;
25
26 return $value unless defined $value;
27
28 my $info = $self->column_info($col)
29 or $self->throw_exception("No column info for $col");
30
31 return $value unless exists $info->{_filter_info};
32
4c0c3038 33 my $filter = $info->{_filter_info}{from_storage};
51bec050 34 $self->throw_exception("No inflator for $col") unless defined $filter;
35
36 return $self->$filter($value);
37}
38
4c0c3038 39sub _column_to_storage {
51bec050 40 my ($self, $col, $value) = @_;
41
42 my $info = $self->column_info($col) or
43 $self->throw_exception("No column info for $col");
44
45 return $value unless exists $info->{_filter_info};
46
4c0c3038 47 my $unfilter = $info->{_filter_info}{to_storage};
51bec050 48 $self->throw_exception("No unfilter for $col") unless defined $unfilter;
49 return $self->$unfilter($value);
50}
51
956f4141 52sub get_value {
51bec050 53 my ($self, $col) = @_;
54
55 $self->throw_exception("$col is not a filtered column")
56 unless exists $self->column_info($col)->{_filter_info};
57
58 return $self->{_filtered_column}{$col}
59 if exists $self->{_filtered_column}{$col};
60
61 my $val = $self->get_column($col);
62
4c0c3038 63 return $self->{_filtered_column}{$col} = $self->_column_from_storage($col, $val);
51bec050 64}
65
956f4141 66sub set_value {
51bec050 67 my ($self, $col, $filtered) = @_;
68
4c0c3038 69 $self->set_column($col, $self->_column_to_storage($col, $filtered));
51bec050 70
71 delete $self->{_filtered_column}{$col};
72
73 return $filtered;
74}
75
7b461f8a 76sub update {
77 my ($self, $attrs, @rest) = @_;
78 foreach my $key (keys %{$attrs||{}}) {
79 if ($self->has_column($key) &&
80 exists $self->column_info($key)->{_filter_info}) {
81 my $val = delete $attrs->{$key};
82 $self->set_value($key, $val);
4c0c3038 83 $attrs->{$key} = $self->_column_to_storage($key, $val)
7b461f8a 84 }
85 }
86 return $self->next::method($attrs, @rest);
87}
88
89
90sub new {
91 my ($class, $attrs, @rest) = @_;
7b461f8a 92 foreach my $key (keys %{$attrs||{}}) {
93 if ($class->has_column($key) &&
94 exists $class->column_info($key)->{_filter_info} ) {
4c0c3038 95 $attrs->{$key} = $class->_column_to_storage($key, delete $attrs->{$key})
7b461f8a 96 }
97 }
98 my $obj = $class->next::method($attrs, @rest);
99 return $obj;
100}
101
102
51bec050 1031;