1 package DBIx::Class::FilterColumn;
5 use base qw/DBIx::Class::Row/;
8 my ($self, $col, $attrs) = @_;
10 $self->throw_exception("No such column $col to filter")
11 unless $self->has_column($col);
13 $self->throw_exception("filter_column needs attr hashref")
14 unless ref $attrs eq 'HASH';
16 $self->column_info($col)->{_filter_info} = $attrs;
17 my $acc = $self->column_info($col)->{accessor};
18 $self->mk_group_accessors(filtered_column => [ (defined $acc ? $acc : $col), $col]);
22 sub _column_from_storage {
23 my ($self, $col, $value) = @_;
25 return $value unless defined $value;
27 my $info = $self->column_info($col)
28 or $self->throw_exception("No column info for $col");
30 return $value unless exists $info->{_filter_info};
32 my $filter = $info->{_filter_info}{filter_from_storage};
33 $self->throw_exception("No inflator for $col") unless defined $filter;
35 return $self->$filter($value);
38 sub _column_to_storage {
39 my ($self, $col, $value) = @_;
41 my $info = $self->column_info($col) or
42 $self->throw_exception("No column info for $col");
44 return $value unless exists $info->{_filter_info};
46 my $unfilter = $info->{_filter_info}{filter_to_storage};
47 $self->throw_exception("No unfilter for $col") unless defined $unfilter;
48 return $self->$unfilter($value);
51 sub get_filtered_column {
52 my ($self, $col) = @_;
54 $self->throw_exception("$col is not a filtered column")
55 unless exists $self->column_info($col)->{_filter_info};
57 return $self->{_filtered_column}{$col}
58 if exists $self->{_filtered_column}{$col};
60 my $val = $self->get_column($col);
62 return $self->{_filtered_column}{$col} = $self->_column_from_storage($col, $val);
66 my ($self, $col) = (shift, @_);
69 delete $self->{_filtered_column}{$col};
71 $self->next::method(@_);
74 sub set_filtered_column {
75 my ($self, $col, $filtered) = @_;
77 # do not blow up the cache via set_column unless necessary
78 # (filtering may be expensive!)
79 if (exists $self->{_filtered_column}{$col}) {
81 if ($self->_eq_column_values ($col, $filtered, $self->{_filtered_column}{$col} ) );
83 $self->make_column_dirty ($col); # so the comparison won't run again
86 $self->set_column($col, $self->_column_to_storage($col, $filtered));
92 my ($self, $attrs, @rest) = @_;
93 foreach my $key (keys %{$attrs||{}}) {
94 if ($self->has_column($key) &&
95 exists $self->column_info($key)->{_filter_info}) {
96 my $val = delete $attrs->{$key};
97 $self->set_filtered_column($key, $val);
98 $attrs->{$key} = $self->_column_to_storage($key, $val)
101 return $self->next::method($attrs, @rest);
105 my ($class, $attrs, @rest) = @_;
106 my $source = $attrs->{-result_source}
107 or $class->throw_exception('Sourceless rows are not supported with DBIx::Class::FilterColumn');
109 my $obj = $class->next::method($attrs, @rest);
110 foreach my $key (keys %{$attrs||{}}) {
111 if ($obj->has_column($key) &&
112 exists $obj->column_info($key)->{_filter_info} ) {
113 my $val = delete $attrs->{$key};
114 $obj->set_filtered_column($key, $val);
124 DBIx::Class::FilterColumn - Automatically convert column data
128 # In your result classes
129 __PACKAGE__->filter_column( money => {
130 filter_to_storage => 'to_pennies',
131 filter_from_storage => 'from_pennies',
134 sub to_pennies { $_[1] * 100 }
136 sub from_pennies { $_[1] / 100 }
142 This component is meant to be a more powerful, but less DWIM-y,
143 L<DBIx::Class::InflateColumn>. One of the major issues with said component is
144 that it B<only> works with references. Generally speaking anything that can
145 be done with L<DBIx::Class::InflateColumn> can be done with this component.
151 __PACKAGE__->filter_column( colname => {
152 filter_from_storage => 'method',
153 filter_to_storage => 'method',
156 This is the method that you need to call to set up a filtered column. It takes
157 exactly two arguments; the first being the column name the second being a
158 C<HashRef> with C<filter_from_storage> and C<filter_to_storage> having
159 something that can be called as a method. The method will be called with
160 the value of the column as the first non-C<$self> argument.
162 =head2 get_filtered_column
164 $obj->get_filtered_column('colname')
166 Returns the filtered value of the column
168 =head2 set_filtered_column
170 $obj->set_filtered_column(colname => 'new_value')
172 Sets the filtered value of the column