doc, removal of source stuff, and Changes
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / FilterColumn.pm
CommitLineData
51bec050 1package DBIx::Class::FilterColumn;
51bec050 2use strict;
3use warnings;
4
5use base qw/DBIx::Class::Row/;
6
7sub filter_column {
8 my ($self, $col, $attrs) = @_;
9
10 $self->throw_exception("No such column $col to filter")
11 unless $self->has_column($col);
12
13 $self->throw_exception("filter_column needs attr hashref")
14 unless ref $attrs eq 'HASH';
15
16 $self->column_info($col)->{_filter_info} = $attrs;
17 my $acc = $self->column_info($col)->{accessor};
d7d38bef 18 $self->mk_group_accessors(filtered_column => [ (defined $acc ? $acc : $col), $col]);
51bec050 19 return 1;
20}
21
4c0c3038 22sub _column_from_storage {
9b2c0de6 23 my ($self, $col, $value) = @_;
51bec050 24
25 return $value unless defined $value;
26
27 my $info = $self->column_info($col)
28 or $self->throw_exception("No column info for $col");
29
30 return $value unless exists $info->{_filter_info};
31
d7d38bef 32 my $filter = $info->{_filter_info}{filter_from_storage};
51bec050 33 $self->throw_exception("No inflator for $col") unless defined $filter;
34
9b2c0de6 35 return $self->$filter($value);
51bec050 36}
37
4c0c3038 38sub _column_to_storage {
9b2c0de6 39 my ($self, $col, $value) = @_;
51bec050 40
41 my $info = $self->column_info($col) or
42 $self->throw_exception("No column info for $col");
43
44 return $value unless exists $info->{_filter_info};
45
d7d38bef 46 my $unfilter = $info->{_filter_info}{filter_to_storage};
51bec050 47 $self->throw_exception("No unfilter for $col") unless defined $unfilter;
9b2c0de6 48 return $self->$unfilter($value);
51bec050 49}
50
d7d38bef 51sub get_filtered_column {
51bec050 52 my ($self, $col) = @_;
53
54 $self->throw_exception("$col is not a filtered column")
55 unless exists $self->column_info($col)->{_filter_info};
56
57 return $self->{_filtered_column}{$col}
58 if exists $self->{_filtered_column}{$col};
59
60 my $val = $self->get_column($col);
61
9b2c0de6 62 return $self->{_filtered_column}{$col} = $self->_column_from_storage($col, $val);
51bec050 63}
64
d7d38bef 65sub set_filtered_column {
51bec050 66 my ($self, $col, $filtered) = @_;
67
9b2c0de6 68 $self->set_column($col, $self->_column_to_storage($col, $filtered));
51bec050 69
70 delete $self->{_filtered_column}{$col};
71
72 return $filtered;
73}
74
7b461f8a 75sub update {
76 my ($self, $attrs, @rest) = @_;
77 foreach my $key (keys %{$attrs||{}}) {
78 if ($self->has_column($key) &&
79 exists $self->column_info($key)->{_filter_info}) {
80 my $val = delete $attrs->{$key};
d7d38bef 81 $self->set_filtered_column($key, $val);
9b2c0de6 82 $attrs->{$key} = $self->_column_to_storage($key, $val)
7b461f8a 83 }
84 }
85 return $self->next::method($attrs, @rest);
86}
87
7b461f8a 88sub new {
89 my ($class, $attrs, @rest) = @_;
9b2c0de6 90 my $source = $attrs->{-result_source}
d9ea6d6d 91 or $class->throw_exception('Sourceless rows are not supported with DBIx::Class::FilterColumn');
92
9b2c0de6 93 my $obj = $class->next::method($attrs, @rest);
7b461f8a 94 foreach my $key (keys %{$attrs||{}}) {
9b2c0de6 95 if ($obj->has_column($key) &&
96 exists $obj->column_info($key)->{_filter_info} ) {
97 my $val = delete $attrs->{$key};
98 $obj->set_filtered_column($key, $val);
7b461f8a 99 }
100 }
7b461f8a 101 return $obj;
102}
103
51bec050 1041;
22d9e05a 105
9b2c0de6 106=head1 NAME
22d9e05a 107
9b2c0de6 108DBIx::Class::FilterColumn - Automatically convert column data
109
110=head1 SYNOPSIS
111
112 # In your result classes
113 __PACKAGE__->filter_column( money => {
114 filter_to_storage => 'to_pennies',
115 filter_from_storage => 'from_pennies',
116 });
22d9e05a 117
118 sub to_pennies { $_[1] * 100 }
9b2c0de6 119
22d9e05a 120 sub from_pennies { $_[1] / 100 }
121
122 1;
123
9b2c0de6 124=head1 DESCRIPTION
22d9e05a 125
9b2c0de6 126This component is meant to be a more powerful, but less DWIM-y,
127L<DBIx::Class::InflateColumn>. One of the major issues with said component is
128that it B<only> works with references. Generally speaking anything that can
129be done with L<DBIx::Class::InflateColumn> can be done with this component.
22d9e05a 130
9b2c0de6 131=head1 METHODS
22d9e05a 132
9b2c0de6 133=head2 filter_column
22d9e05a 134
9b2c0de6 135 __PACKAGE__->filter_column( colname => {
136 filter_from_storage => 'method',
137 filter_to_storage => 'method',
138 })
22d9e05a 139
9b2c0de6 140This is the method that you need to call to set up a filtered column. It takes
141exactly two arguments; the first being the column name the second being a
142C<HashRef> with C<filter_from_storage> and C<filter_to_storage> having
143something that can be called as a method. The method will be called with
144the value of the column as the first non-C<$self> argument.
22d9e05a 145
9b2c0de6 146=head2 get_filtered_column
22d9e05a 147
9b2c0de6 148 $obj->get_filtered_column('colname')
149
150Returns the filtered value of the column
151
152=head2 set_filtered_column
153
154 $obj->set_filtered_column(colname => 'new_value')
155
156Sets the filtered value of the column
157
158=head2 update
159
160Just overridden to filter changed columns through this component
161
162=head2 new
22d9e05a 163
9b2c0de6 164Just overridden to filter columns through this component