Bump trunk version
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / UTF8Columns.pm
CommitLineData
5dd9c59c 1package DBIx::Class::UTF8Columns;
2use strict;
3use warnings;
4use base qw/DBIx::Class/;
5dd9c59c 5
4e8964d5 6__PACKAGE__->mk_classdata( '_utf8_columns' );
5dd9c59c 7
8=head1 NAME
9
10DBIx::Class::UTF8Columns - Force UTF8 (Unicode) flag on columns
11
12=head1 SYNOPSIS
13
14 package Artist;
d88ecca6 15 use base 'DBIx::Class::Core';
16
17 __PACKAGE__->load_components(qw/UTF8Columns/);
5dd9c59c 18 __PACKAGE__->utf8_columns(qw/name description/);
d4daee7b 19
5dd9c59c 20 # then belows return strings with utf8 flag
21 $artist->name;
22 $artist->get_column('description');
23
24=head1 DESCRIPTION
25
26This module allows you to get columns data that have utf8 (Unicode) flag.
27
d38cd95c 28=head2 Warning
29
30Note that this module overloads L<DBIx::Class::Row/store_column> in a way
31that may prevent other components overloading the same method from working
32correctly. This component must be the last one before L<DBIx::Class::Row>
33(which is provided by L<DBIx::Class::Core>). DBIx::Class will detect such
34incorrect component order and issue an appropriate warning, advising which
35components need to be loaded differently.
36
5dd9c59c 37=head1 SEE ALSO
38
39L<Template::Stash::ForceUTF8>, L<DBIx::Class::UUIDColumns>.
40
41=head1 METHODS
42
43=head2 utf8_columns
44
45=cut
46
47sub utf8_columns {
48 my $self = shift;
4e8964d5 49 if (@_) {
50 foreach my $col (@_) {
51 $self->throw_exception("column $col doesn't exist")
52 unless $self->has_column($col);
d38cd95c 53 }
4e8964d5 54 return $self->_utf8_columns({ map { $_ => 1 } @_ });
55 } else {
56 return $self->_utf8_columns;
5dd9c59c 57 }
5dd9c59c 58}
59
60=head1 EXTENDED METHODS
61
62=head2 get_column
63
64=cut
65
66sub get_column {
67 my ( $self, $column ) = @_;
68 my $value = $self->next::method($column);
69
d38cd95c 70 utf8::decode($value) if (
71 defined $value and $self->_is_utf8_column($column) and ! utf8::is_utf8($value)
72 );
5dd9c59c 73
55087b99 74 return $value;
5dd9c59c 75}
76
e063fe2c 77=head2 get_columns
78
79=cut
80
81sub get_columns {
82 my $self = shift;
83 my %data = $self->next::method(@_);
84
d38cd95c 85 foreach my $col (keys %data) {
86 utf8::decode($data{$col}) if (
87 exists $data{$col} and defined $data{$col} and $self->_is_utf8_column($col) and ! utf8::is_utf8($data{$col})
88 );
e063fe2c 89 }
90
55087b99 91 return %data;
e063fe2c 92}
93
5dd9c59c 94=head2 store_column
95
96=cut
97
98sub store_column {
99 my ( $self, $column, $value ) = @_;
100
d38cd95c 101 # the dirtyness comparison must happen on the non-encoded value
102 my $copy;
103
104 if ( defined $value and $self->_is_utf8_column($column) and utf8::is_utf8($value) ) {
105 $copy = $value;
106 utf8::encode($value);
5dd9c59c 107 }
108
109 $self->next::method( $column, $value );
d38cd95c 110
111 return $copy || $value;
5dd9c59c 112}
113
d38cd95c 114# override this if you want to force everything to be encoded/decoded
115sub _is_utf8_column {
9738897e 116 return (shift->utf8_columns || {})->{shift @_};
d38cd95c 117}
5dd9c59c 118
d38cd95c 119=head1 AUTHORS
5dd9c59c 120
d38cd95c 121See L<DBIx::Class/CONTRIBUTORS>.
5dd9c59c 122
d38cd95c 123=head1 LICENSE
5dd9c59c 124
d38cd95c 125You may distribute this code under the same terms as Perl itself.
5dd9c59c 126
127=cut
128
1291;