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