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 | |
85439e0c |
69 | sub set_column { |
70 | my ($self, $col) = (shift, @_); |
71 | |
72 | # blow cache |
73 | delete $self->{_filtered_column}{$col}; |
74 | |
75 | $self->next::method(@_); |
76 | } |
77 | |
d7d38bef |
78 | sub set_filtered_column { |
51bec050 |
79 | my ($self, $col, $filtered) = @_; |
80 | |
cde96798 |
81 | # do not blow up the cache via set_column unless necessary |
82 | # (filtering may be expensive!) |
83 | if (exists $self->{_filtered_column}{$col}) { |
84 | return $filtered |
85 | if ($self->_eq_column_values ($col, $filtered, $self->{_filtered_column}{$col} ) ); |
51bec050 |
86 | |
cde96798 |
87 | $self->make_column_dirty ($col); # so the comparison won't run again |
88 | } |
89 | |
90 | $self->set_column($col, $self->_column_to_storage($col, $filtered)); |
51bec050 |
91 | |
92 | return $filtered; |
93 | } |
94 | |
7b461f8a |
95 | sub update { |
96 | my ($self, $attrs, @rest) = @_; |
97 | foreach my $key (keys %{$attrs||{}}) { |
98 | if ($self->has_column($key) && |
99 | exists $self->column_info($key)->{_filter_info}) { |
100 | my $val = delete $attrs->{$key}; |
d7d38bef |
101 | $self->set_filtered_column($key, $val); |
9b2c0de6 |
102 | $attrs->{$key} = $self->_column_to_storage($key, $val) |
7b461f8a |
103 | } |
104 | } |
105 | return $self->next::method($attrs, @rest); |
106 | } |
107 | |
7b461f8a |
108 | sub new { |
109 | my ($class, $attrs, @rest) = @_; |
9b2c0de6 |
110 | my $source = $attrs->{-result_source} |
d9ea6d6d |
111 | or $class->throw_exception('Sourceless rows are not supported with DBIx::Class::FilterColumn'); |
112 | |
9b2c0de6 |
113 | my $obj = $class->next::method($attrs, @rest); |
7b461f8a |
114 | foreach my $key (keys %{$attrs||{}}) { |
9b2c0de6 |
115 | if ($obj->has_column($key) && |
116 | exists $obj->column_info($key)->{_filter_info} ) { |
117 | my $val = delete $attrs->{$key}; |
118 | $obj->set_filtered_column($key, $val); |
7b461f8a |
119 | } |
120 | } |
7b461f8a |
121 | return $obj; |
122 | } |
123 | |
51bec050 |
124 | 1; |
22d9e05a |
125 | |
9b2c0de6 |
126 | =head1 NAME |
22d9e05a |
127 | |
9b2c0de6 |
128 | DBIx::Class::FilterColumn - Automatically convert column data |
129 | |
130 | =head1 SYNOPSIS |
131 | |
132 | # In your result classes |
133 | __PACKAGE__->filter_column( money => { |
134 | filter_to_storage => 'to_pennies', |
135 | filter_from_storage => 'from_pennies', |
136 | }); |
22d9e05a |
137 | |
138 | sub to_pennies { $_[1] * 100 } |
9b2c0de6 |
139 | |
22d9e05a |
140 | sub from_pennies { $_[1] / 100 } |
141 | |
142 | 1; |
143 | |
9b2c0de6 |
144 | =head1 DESCRIPTION |
22d9e05a |
145 | |
9b2c0de6 |
146 | This component is meant to be a more powerful, but less DWIM-y, |
147 | L<DBIx::Class::InflateColumn>. One of the major issues with said component is |
148 | that it B<only> works with references. Generally speaking anything that can |
149 | be done with L<DBIx::Class::InflateColumn> can be done with this component. |
22d9e05a |
150 | |
9b2c0de6 |
151 | =head1 METHODS |
22d9e05a |
152 | |
9b2c0de6 |
153 | =head2 filter_column |
22d9e05a |
154 | |
9b2c0de6 |
155 | __PACKAGE__->filter_column( colname => { |
156 | filter_from_storage => 'method', |
157 | filter_to_storage => 'method', |
158 | }) |
22d9e05a |
159 | |
9b2c0de6 |
160 | This is the method that you need to call to set up a filtered column. It takes |
161 | exactly two arguments; the first being the column name the second being a |
162 | C<HashRef> with C<filter_from_storage> and C<filter_to_storage> having |
163 | something that can be called as a method. The method will be called with |
164 | the value of the column as the first non-C<$self> argument. |
22d9e05a |
165 | |
9b2c0de6 |
166 | =head2 get_filtered_column |
22d9e05a |
167 | |
9b2c0de6 |
168 | $obj->get_filtered_column('colname') |
169 | |
170 | Returns the filtered value of the column |
171 | |
172 | =head2 set_filtered_column |
173 | |
174 | $obj->set_filtered_column(colname => 'new_value') |
175 | |
176 | Sets the filtered value of the column |