1 package DBIx::Class::FilterColumn;
6 use base qw/DBIx::Class::Row/;
9 my ($self, $col, $attrs) = @_;
11 $self->throw_exception("No such column $col to filter")
12 unless $self->has_column($col);
14 $self->throw_exception("filter_column needs attr hashref")
15 unless ref $attrs eq 'HASH';
17 $self->column_info($col)->{_filter_info} = $attrs;
18 my $acc = $self->column_info($col)->{accessor};
19 $self->mk_group_accessors(filtered_column => [ (defined $acc ? $acc : $col), $col]);
23 sub _column_from_storage {
24 my ($self, $source, $col, $value) = @_;
26 return $value unless defined $value;
28 my $info = $self->column_info($col)
29 or $self->throw_exception("No column info for $col");
31 return $value unless exists $info->{_filter_info};
33 my $filter = $info->{_filter_info}{filter_from_storage};
34 $self->throw_exception("No inflator for $col") unless defined $filter;
36 return $source->$filter($value);
39 sub _column_to_storage {
40 my ($self, $source, $col, $value) = @_;
42 my $info = $self->column_info($col) or
43 $self->throw_exception("No column info for $col");
45 return $value unless exists $info->{_filter_info};
47 my $unfilter = $info->{_filter_info}{filter_to_storage};
48 $self->throw_exception("No unfilter for $col") unless defined $unfilter;
49 return $source->$unfilter($value);
52 sub get_filtered_column {
53 my ($self, $col) = @_;
55 $self->throw_exception("$col is not a filtered column")
56 unless exists $self->column_info($col)->{_filter_info};
58 return $self->{_filtered_column}{$col}
59 if exists $self->{_filtered_column}{$col};
61 my $val = $self->get_column($col);
63 return $self->{_filtered_column}{$col} = $self->_column_from_storage($self->result_source, $col, $val);
66 sub set_filtered_column {
67 my ($self, $col, $filtered) = @_;
69 $self->set_column($col, $self->_column_to_storage($self->result_source, $col, $filtered));
71 delete $self->{_filtered_column}{$col};
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_filtered_column($key, $val);
83 $attrs->{$key} = $self->_column_to_storage($self->result_source, $key, $val)
86 return $self->next::method($attrs, @rest);
91 my ($class, $attrs, @rest) = @_;
92 my $source = delete $attrs->{-result_source}
93 or $class->throw_exception('Sourceless rows are not supported with DBIx::Class::FilterColumn');
95 foreach my $key (keys %{$attrs||{}}) {
96 if ($class->has_column($key) &&
97 exists $class->column_info($key)->{_filter_info} ) {
98 $attrs->{$key} = $class->_column_to_storage($source, $key, delete $attrs->{$key})
101 my $obj = $class->next::method($attrs, @rest);
107 =head1 THE ONE TRUE WAY
109 package My::Reusable::Filter;
111 sub to_pennies { $_[1] * 100 }
112 sub from_pennies { $_[1] / 100 }
116 package My::Schema::Result::Account;
121 use base 'DBIx::Class::Core';
123 __PACKAGE->load_components('FilterColumn');
125 __PACKAGE__->add_columns(
128 is_auto_increment => 1,
135 __PACKAGE__->set_primary_key('id');
137 __PACKAGE__->filter_column(total_money => {
138 filter_to_storage => 'to_pennies',
139 filter_from_storage => 'from_pennies',