File Coverage

blib/lib/Jifty/DBI/Filter/Truncate.pm
Criterion Covered Total %
statement 29 29 100.0
branch 10 10 100.0
condition 5 6 83.3
subroutine 6 6 100.0
pod 1 1 100.0
total 51 52 98.0


line stmt bran cond sub pod time code
1              
2 27     27   182 use strict;
  27         59  
  27         1584  
3 27     27   161 use warnings;
  27         57  
  27         1836  
4              
5             package Jifty::DBI::Filter::Truncate;
6 27     27   170 use base qw/Jifty::DBI::Filter/;
  27         55  
  27         38484  
7 27     27   190 use Encode ();
  27         64  
  27         6754  
8              
9             =head1 NAME
10              
11             Jifty::DBI::Filter::Truncate - Filter used to enforce max_length column trait
12              
13             =head1 DESCRIPTION
14              
15             You do not need to use this filter explicitly. This filter is used internally to enforce the L restrictions on columns:
16              
17             column name =>
18             type is 'text',
19             max_length is 10;
20              
21             In this case, the filter would be automatically added to the column named C and any value put into the column longer than 10 characters would be truncated to 10 characters.
22              
23             =head1 METHODS
24              
25             =head2 encode
26              
27             This method performs the work of performing truncation, when necessary.
28              
29             =cut
30              
31             sub encode {
32 337     337 1 543 my $self = shift;
33              
34 337         2498 my $value_ref = $self->value_ref;
35 337 100       3946 return undef unless ( defined($$value_ref) );
36              
37 298         1215 my $column = $self->column();
38              
39 298         5353 my $truncate_to;
40 298 100 100     1383 if ( $column->max_length && !$column->is_numeric ) {
    100 66        
41 3         10 $truncate_to = $column->max_length;
42             } elsif ( $column->type && $column->type =~ /char\((\d+)\)/ ) {
43 91         2839 $truncate_to = $1;
44             }
45              
46 298 100       6467 return unless ($truncate_to); # don't need to truncate
47              
48 94         419 my $utf8 = Encode::is_utf8($$value_ref);
49             {
50 27     27   194 use bytes;
  27         63  
  27         417  
  94         133  
51 94         354 $$value_ref = substr( $$value_ref, 0, $truncate_to );
52             }
53 94 100       1141 if ($utf8) {
54              
55             # return utf8 flag back, but use Encode::FB_QUIET because
56             # we could broke tail char
57 2         21 $$value_ref = Encode::decode_utf8( $$value_ref, Encode::FB_QUIET );
58             }
59             }
60              
61             =head1 LICENSE
62              
63             Jifty::DBI is Copyright 2005-2007 Best Practical Solutions, LLC.
64             Jifty::DBI is distributed under the same terms as Perl itself.
65              
66             =cut
67              
68             1;