| 1 | package DBIx::Class::EncodeColumns; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use vars qw/$VERSION/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | $VERSION = '0.02'; | |||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use base qw/DBIx::Class/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | use Encode; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 1 | __PACKAGE__->mk_classdata( decode_columns => '' ); | |||||||||
| 13 | 1 | __PACKAGE__->mk_classdata( encode_columns => '' ); | |||||||||
| 14 | |||||||||||
| 15 | =head1 NAME | ||||||||||
| 16 | |||||||||||
| 17 | DBIx::Class::EncodeColumns - Handle column encodings | ||||||||||
| 18 | |||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||
| 20 | |||||||||||
| 21 | package Artist; | ||||||||||
| 22 | __PACKAGE__->load_components(qw/EncodeColumns Core/); | ||||||||||
| 23 | __PACKAGE__->decode_columns('latin-1'); | ||||||||||
| 24 | __PACKAGE__->encode_columns('utf-8'); | ||||||||||
| 25 | |||||||||||
| 26 | |||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||
| 28 | |||||||||||
| 29 | This module allows you to handle column encodings | ||||||||||
| 30 | |||||||||||
| 31 | =head1 METHODS | ||||||||||
| 32 | |||||||||||
| 33 | =head2 decode_columns($encoding) | ||||||||||
| 34 | |||||||||||
| 35 | Use this function to set the default encoding of all your columns. | ||||||||||
| 36 | The data of all columns will be decoded to internal encoding of perl. | ||||||||||
| 37 | |||||||||||
| 38 | =cut | ||||||||||
| 39 | |||||||||||
| 40 | =head2 encode_columns($encoding) | ||||||||||
| 41 | |||||||||||
| 42 | Before returning the data form a column, it will be encoded using this | ||||||||||
| 43 | encoding type. | ||||||||||
| 44 | |||||||||||
| 45 | =cut | ||||||||||
| 46 | |||||||||||
| 47 | =head1 EXTENDED METHODS | ||||||||||
| 48 | |||||||||||
| 49 | =head2 get_column | ||||||||||
| 50 | |||||||||||
| 51 | =cut | ||||||||||
| 52 | |||||||||||
| 53 | 0 | ▶ | sub get_column { | ||||||||
|
Subroutine
get_column: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 54 | 0 | my ( $self, $column ) = @_; | |||||||||
| 55 | 0 | my $value = $self->next::method($column); | |||||||||
| 56 | |||||||||||
| 57 | 0 | ▶ | if( defined $value ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | |||||||||||
| 59 | 0 | ▶ | $value = decode( $self->decode_columns, $value ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | if $self->decode_columns; | ||||||||||
| 61 | |||||||||||
| 62 | 0 | ▶ | $value = encode( $self->encode_columns, $value ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | if $self->encode_columns; | ||||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 0 | return $value; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | =head2 get_columns | ||||||||||
| 70 | |||||||||||
| 71 | =cut | ||||||||||
| 72 | |||||||||||
| 73 | 0 | ▶ | sub get_columns { | ||||||||
|
Subroutine
get_columns: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 74 | 0 | my $self = shift; | |||||||||
| 75 | 0 | my %data = $self->next::method(@_); | |||||||||
| 76 | |||||||||||
| 77 | 0 | foreach my $col (keys %data) { | |||||||||
| 78 | |||||||||||
| 79 | 0 | ▶ | if(defined(my $value = $data{$col}) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | |||||||||||
| 81 | 0 | ▶ | $value = decode( $self->decode_columns, $value ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | if $self->decode_columns; | ||||||||||
| 83 | |||||||||||
| 84 | 0 | ▶ | $value = encode( $self->encode_columns, $value ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | if $self->encode_columns; | ||||||||||
| 86 | |||||||||||
| 87 | 0 | $data{$col} = $value; | |||||||||
| 88 | } | ||||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 0 | return %data; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | =head2 store_column | ||||||||||
| 95 | |||||||||||
| 96 | =cut | ||||||||||
| 97 | |||||||||||
| 98 | 0 | ▶ | sub store_column { | ||||||||
|
Subroutine
store_column: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 99 | 0 | my ( $self, $column, $value ) = @_; | |||||||||
| 100 | |||||||||||
| 101 | 0 | ▶ | $value = decode( $self->encode_columns, $value ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | if $self->encode_columns; | ||||||||||
| 103 | |||||||||||
| 104 | 0 | ▶ | $value = encode( $self->decode_columns, $value ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | if $self->decode_columns; | ||||||||||
| 106 | |||||||||||
| 107 | 0 | return $self->next::method( $column, $value ); | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | =head1 AUTHOR | ||||||||||
| 111 | |||||||||||
| 112 | Sascha Kiefer <esskar@cpan.org> | ||||||||||
| 113 | |||||||||||
| 114 | =head1 COPYRIGHT | ||||||||||
| 115 | |||||||||||
| 116 | This program is free software; you can redistribute | ||||||||||
| 117 | it and/or modify it under the same terms as Perl itself. | ||||||||||
| 118 | |||||||||||
| 119 | =cut | ||||||||||
| 120 | |||||||||||
| 121 | 1 | 1; | |||||||||
| 122 | |||||||||||
| 123 | |||||||||||
| 124 | |||||||||||
| 125 | |||||||||||