Commit | Line | Data |
51bec050 |
1 | package DBIx::Class::FilterColumn; |
51bec050 |
2 | use strict; |
3 | use warnings; |
4 | |
5 | use base qw/DBIx::Class::Row/; |
6 | |
7 | sub filter_column { |
8 | my ($self, $col, $attrs) = @_; |
9 | |
c227b295 |
10 | $self->throw_exception("FilterColumn does not work with InflateColumn") |
11 | if $self->isa('DBIx::Class::InflateColumn') && |
12 | defined $self->column_info($col)->{_inflate_info}; |
13 | |
51bec050 |
14 | $self->throw_exception("No such column $col to filter") |
15 | unless $self->has_column($col); |
16 | |
17 | $self->throw_exception("filter_column needs attr hashref") |
18 | unless ref $attrs eq 'HASH'; |
19 | |
20 | $self->column_info($col)->{_filter_info} = $attrs; |
21 | my $acc = $self->column_info($col)->{accessor}; |
d7d38bef |
22 | $self->mk_group_accessors(filtered_column => [ (defined $acc ? $acc : $col), $col]); |
51bec050 |
23 | return 1; |
24 | } |
25 | |
4c0c3038 |
26 | sub _column_from_storage { |
9b2c0de6 |
27 | my ($self, $col, $value) = @_; |
51bec050 |
28 | |
29 | return $value unless defined $value; |
30 | |
31 | my $info = $self->column_info($col) |
32 | or $self->throw_exception("No column info for $col"); |
33 | |
34 | return $value unless exists $info->{_filter_info}; |
35 | |
d7d38bef |
36 | my $filter = $info->{_filter_info}{filter_from_storage}; |
c227b295 |
37 | $self->throw_exception("No filter for $col") unless defined $filter; |
51bec050 |
38 | |
9b2c0de6 |
39 | return $self->$filter($value); |
51bec050 |
40 | } |
41 | |
4c0c3038 |
42 | sub _column_to_storage { |
9b2c0de6 |
43 | my ($self, $col, $value) = @_; |
51bec050 |
44 | |
45 | my $info = $self->column_info($col) or |
46 | $self->throw_exception("No column info for $col"); |
47 | |
48 | return $value unless exists $info->{_filter_info}; |
49 | |
d7d38bef |
50 | my $unfilter = $info->{_filter_info}{filter_to_storage}; |
51bec050 |
51 | $self->throw_exception("No unfilter for $col") unless defined $unfilter; |
9b2c0de6 |
52 | return $self->$unfilter($value); |
51bec050 |
53 | } |
54 | |
d7d38bef |
55 | sub get_filtered_column { |
51bec050 |
56 | my ($self, $col) = @_; |
57 | |
58 | $self->throw_exception("$col is not a filtered column") |
59 | unless exists $self->column_info($col)->{_filter_info}; |
60 | |
61 | return $self->{_filtered_column}{$col} |
62 | if exists $self->{_filtered_column}{$col}; |
63 | |
64 | my $val = $self->get_column($col); |
65 | |
9b2c0de6 |
66 | return $self->{_filtered_column}{$col} = $self->_column_from_storage($col, $val); |
51bec050 |
67 | } |
68 | |
491c8ff9 |
69 | sub get_column { |
70 | my ($self, $col) = @_; |
71 | if (exists $self->{_filtered_column}{$col}) { |
72 | return $self->{_column_data}{$col} ||= $self->_column_to_storage ($col, $self->{_filtered_column}{$col}); |
73 | } |
74 | |
75 | return $self->next::method ($col); |
76 | } |
77 | |
78 | # sadly a separate codepath in Row.pm ( used by insert() ) |
79 | sub get_columns { |
80 | my $self = shift; |
81 | |
82 | foreach my $col (keys %{$self->{_filtered_column}||{}}) { |
83 | $self->{_column_data}{$col} ||= $self->_column_to_storage ($col, $self->{_filtered_column}{$col}) |
84 | if exists $self->{_filtered_column}{$col}; |
85 | } |
86 | |
87 | $self->next::method (@_); |
88 | } |
89 | |
90 | sub store_column { |
85439e0c |
91 | my ($self, $col) = (shift, @_); |
92 | |
93 | # blow cache |
94 | delete $self->{_filtered_column}{$col}; |
95 | |
96 | $self->next::method(@_); |
97 | } |
98 | |
d7d38bef |
99 | sub set_filtered_column { |
51bec050 |
100 | my ($self, $col, $filtered) = @_; |
101 | |
cde96798 |
102 | # do not blow up the cache via set_column unless necessary |
103 | # (filtering may be expensive!) |
104 | if (exists $self->{_filtered_column}{$col}) { |
105 | return $filtered |
106 | if ($self->_eq_column_values ($col, $filtered, $self->{_filtered_column}{$col} ) ); |
51bec050 |
107 | |
cde96798 |
108 | $self->make_column_dirty ($col); # so the comparison won't run again |
109 | } |
110 | |
111 | $self->set_column($col, $self->_column_to_storage($col, $filtered)); |
51bec050 |
112 | |
491c8ff9 |
113 | return $self->{_filtered_column}{$col} = $filtered; |
51bec050 |
114 | } |
115 | |
7b461f8a |
116 | sub update { |
117 | my ($self, $attrs, @rest) = @_; |
491c8ff9 |
118 | |
7b461f8a |
119 | foreach my $key (keys %{$attrs||{}}) { |
491c8ff9 |
120 | if ( |
121 | $self->has_column($key) |
122 | && |
123 | exists $self->column_info($key)->{_filter_info} |
124 | ) { |
125 | $self->set_filtered_column($key, delete $attrs->{$key}); |
7c6fa77f |
126 | |
127 | # FIXME update() reaches directly into the object-hash |
128 | # and we may *not* have a filtered value there - thus |
129 | # the void-ctx filter-trigger |
130 | $self->get_column($key) unless exists $self->{_column_data}{$key}; |
7b461f8a |
131 | } |
132 | } |
491c8ff9 |
133 | |
7b461f8a |
134 | return $self->next::method($attrs, @rest); |
135 | } |
136 | |
7b461f8a |
137 | sub new { |
138 | my ($class, $attrs, @rest) = @_; |
9b2c0de6 |
139 | my $source = $attrs->{-result_source} |
d9ea6d6d |
140 | or $class->throw_exception('Sourceless rows are not supported with DBIx::Class::FilterColumn'); |
141 | |
9b2c0de6 |
142 | my $obj = $class->next::method($attrs, @rest); |
7b461f8a |
143 | foreach my $key (keys %{$attrs||{}}) { |
9b2c0de6 |
144 | if ($obj->has_column($key) && |
145 | exists $obj->column_info($key)->{_filter_info} ) { |
491c8ff9 |
146 | $obj->set_filtered_column($key, $attrs->{$key}); |
7b461f8a |
147 | } |
148 | } |
491c8ff9 |
149 | |
7b461f8a |
150 | return $obj; |
151 | } |
152 | |
51bec050 |
153 | 1; |
22d9e05a |
154 | |
9b2c0de6 |
155 | =head1 NAME |
22d9e05a |
156 | |
9b2c0de6 |
157 | DBIx::Class::FilterColumn - Automatically convert column data |
158 | |
159 | =head1 SYNOPSIS |
160 | |
f10a2e9a |
161 | In your Schema or DB class add "FilterColumn" to the top of the component list. |
162 | |
163 | __PACKAGE__->load_components(qw( FilterColumn ... )); |
164 | |
165 | Set up filters for the columns you want to convert. |
166 | |
9b2c0de6 |
167 | __PACKAGE__->filter_column( money => { |
168 | filter_to_storage => 'to_pennies', |
169 | filter_from_storage => 'from_pennies', |
170 | }); |
22d9e05a |
171 | |
172 | sub to_pennies { $_[1] * 100 } |
9b2c0de6 |
173 | |
22d9e05a |
174 | sub from_pennies { $_[1] / 100 } |
175 | |
176 | 1; |
177 | |
f10a2e9a |
178 | |
9b2c0de6 |
179 | =head1 DESCRIPTION |
22d9e05a |
180 | |
9b2c0de6 |
181 | This component is meant to be a more powerful, but less DWIM-y, |
182 | L<DBIx::Class::InflateColumn>. One of the major issues with said component is |
183 | that it B<only> works with references. Generally speaking anything that can |
184 | be done with L<DBIx::Class::InflateColumn> can be done with this component. |
22d9e05a |
185 | |
9b2c0de6 |
186 | =head1 METHODS |
22d9e05a |
187 | |
9b2c0de6 |
188 | =head2 filter_column |
22d9e05a |
189 | |
9b2c0de6 |
190 | __PACKAGE__->filter_column( colname => { |
191 | filter_from_storage => 'method', |
192 | filter_to_storage => 'method', |
193 | }) |
22d9e05a |
194 | |
9b2c0de6 |
195 | This is the method that you need to call to set up a filtered column. It takes |
196 | exactly two arguments; the first being the column name the second being a |
197 | C<HashRef> with C<filter_from_storage> and C<filter_to_storage> having |
198 | something that can be called as a method. The method will be called with |
199 | the value of the column as the first non-C<$self> argument. |
22d9e05a |
200 | |
9b2c0de6 |
201 | =head2 get_filtered_column |
22d9e05a |
202 | |
9b2c0de6 |
203 | $obj->get_filtered_column('colname') |
204 | |
205 | Returns the filtered value of the column |
206 | |
207 | =head2 set_filtered_column |
208 | |
209 | $obj->set_filtered_column(colname => 'new_value') |
210 | |
211 | Sets the filtered value of the column |