2f9b4f113b80a1f54d1d712461ab7e7842b242d3
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / FilterColumn.pm
1 package DBIx::Class::FilterColumn;
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
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};
18   $self->mk_group_accessors(filtered_column => [ (defined $acc ? $acc : $col), $col]);
19   return 1;
20 }
21
22 sub _column_from_storage {
23   my ($self, $col, $value) = @_;
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
32   my $filter = $info->{_filter_info}{filter_from_storage};
33   $self->throw_exception("No inflator for $col") unless defined $filter;
34
35   return $self->$filter($value);
36 }
37
38 sub _column_to_storage {
39   my ($self, $col, $value) = @_;
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
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);
49 }
50
51 sub get_filtered_column {
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
62   return $self->{_filtered_column}{$col} = $self->_column_from_storage($col, $val);
63 }
64
65 sub set_column {
66   my ($self, $col) = (shift, @_);
67
68   # blow cache
69   delete $self->{_filtered_column}{$col};
70
71   $self->next::method(@_);
72 }
73
74 sub set_filtered_column {
75   my ($self, $col, $filtered) = @_;
76
77   $self->set_column($col, $self->_column_to_storage($col, $filtered));
78
79   delete $self->{_filtered_column}{$col};
80
81   return $filtered;
82 }
83
84 sub update {
85   my ($self, $attrs, @rest) = @_;
86   foreach my $key (keys %{$attrs||{}}) {
87     if ($self->has_column($key) &&
88           exists $self->column_info($key)->{_filter_info}) {
89       my $val = delete $attrs->{$key};
90       $self->set_filtered_column($key, $val);
91       $attrs->{$key} = $self->_column_to_storage($key, $val)
92     }
93   }
94   return $self->next::method($attrs, @rest);
95 }
96
97 sub new {
98   my ($class, $attrs, @rest) = @_;
99   my $source = $attrs->{-result_source}
100     or $class->throw_exception('Sourceless rows are not supported with DBIx::Class::FilterColumn');
101
102   my $obj = $class->next::method($attrs, @rest);
103   foreach my $key (keys %{$attrs||{}}) {
104     if ($obj->has_column($key) &&
105           exists $obj->column_info($key)->{_filter_info} ) {
106       my $val = delete $attrs->{$key};
107       $obj->set_filtered_column($key, $val);
108     }
109   }
110   return $obj;
111 }
112
113 1;
114
115 =head1 NAME
116
117 DBIx::Class::FilterColumn - Automatically convert column data
118
119 =head1 SYNOPSIS
120
121  # In your result classes
122  __PACKAGE__->filter_column( money => {
123      filter_to_storage => 'to_pennies',
124      filter_from_storage => 'from_pennies',
125  });
126
127  sub to_pennies   { $_[1] * 100 }
128
129  sub from_pennies { $_[1] / 100 }
130
131  1;
132
133 =head1 DESCRIPTION
134
135 This component is meant to be a more powerful, but less DWIM-y,
136 L<DBIx::Class::InflateColumn>.  One of the major issues with said component is
137 that it B<only> works with references.  Generally speaking anything that can
138 be done with L<DBIx::Class::InflateColumn> can be done with this component.
139
140 =head1 METHODS
141
142 =head2 filter_column
143
144  __PACKAGE__->filter_column( colname => {
145      filter_from_storage => 'method',
146      filter_to_storage   => 'method',
147  })
148
149 This is the method that you need to call to set up a filtered column.  It takes
150 exactly two arguments; the first being the column name the second being a
151 C<HashRef> with C<filter_from_storage> and C<filter_to_storage> having
152 something that can be called as a method.  The method will be called with
153 the value of the column as the first non-C<$self> argument.
154
155 =head2 get_filtered_column
156
157  $obj->get_filtered_column('colname')
158
159 Returns the filtered value of the column
160
161 =head2 set_filtered_column
162
163  $obj->set_filtered_column(colname => 'new_value')
164
165 Sets the filtered value of the column
166
167 =head2 update
168
169 Just overridden to filter changed columns through this component
170
171 =head2 new
172
173 Just overridden to filter columns through this component