SPOPS/Tool/UTFConvert.pm untested

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 86 CC 5 SCAR 17.1
CC
5
Cov
0%
ruleset_factory
12.0
from_utf
6.0
to_utf
6.0
CRAP 5.5
1 package SPOPS::Tool::UTFConvert;
2  
3 # $Id: UTFConvert.pm,v 3.4 2004/06/02 00:48:24 lachoy Exp $
4  
5 use strict;
6 use Log::Log4perl qw( get_logger );
7 use SPOPS;
8  
9 my $log = get_logger();
10  
11 $SPOPS::Tool::UTFConvert::VERSION = sprintf("%d.%02d", q$Revision: 3.4 $ =~ /(\d+)\.(\d+)/);
12  
13 sub ruleset_factory {
14 my ( $class, $ruleset ) = @_;
15 $log->is_info &&
16 $log->info( "Installing UTF8 conversion methods for ($class)" );
17  
18 my ( $routines );
19  
20 # Why do we have to do this runtime eval stuff? 5.6.1 and greater
21 # barfs on the regexes used for 5.6.0 and earlier, so we eval them
22 # into existence so everyone is happy.
23  
24 if ( $] < 5.006001 ) {
25 require utf8;
26  
27 $routines = <<'ROUTINES_56'
28  
29 sub _from_utf {
30 my ( $self, $field ) = @_;
31 $self->{ $field } =~ tr/\0-\x{FF}//UC;
32 }
33  
34 sub _to_utf {
35 my ( $self, $field ) = @_;
36 $self->{ $field } =~ tr/\0-\x{FF}//CU;
37 }
38  
39 ROUTINES_56
40  
41 }
42 else {
43 require Encode;
44 require Unicode::String;
45 Unicode::String->import( qw( latin1 utf8 ) );
46 $routines = <<'ROUTINES_58';
47  
48 sub _from_utf {
49 my ( $self, $field ) = @_;
50 my $old = $self->{ $field };
51 $old = utf8( $old )->latin1;
52 $self->{ $field } = $old;
53 }
54  
55 sub _to_utf {
56 my ( $self, $field ) = @_;
57 utf8::encode( $self->{ $field } );
58 }
59  
60 ROUTINES_58
61 }
62  
63 eval "$routines";
64 if ( $@ ) {
65 die "Failed to initialize tool for UTF conversion: $@";
66 }
67 push @{ $ruleset->{post_fetch_action} }, \&from_utf;
68 push @{ $ruleset->{pre_save_action} }, \&to_utf;
69 }
70  
71  
72 sub from_utf {
73 my ( $self ) = @_;
74 my $convert_fields = $self->CONFIG->{utf_fields};
75 return 1 unless ( ref $convert_fields eq 'ARRAY' and
76 scalar @{ $convert_fields } );
77 foreach my $field ( @{ $convert_fields } ) {
78 _from_utf( $self, $field );
79 }
80 return 1;
81 }
82  
83 sub to_utf {
84 my ( $self ) = @_;
85 my $convert_fields = $self->CONFIG->{utf_fields};
86 return 1 unless ( ref $convert_fields eq 'ARRAY' and
87 scalar @{ $convert_fields } );
88 foreach my $field ( @{ $convert_fields } ) {
89 _to_utf( $self, $field );
90 }
91 return 1;
92 }
93  
94 1;
95  
96 __END__