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   115 use strict;
  27         33  
  27         1010  
3 27     27   107 use warnings;
  27         35  
  27         1156  
4              
5             package Jifty::DBI::Filter::Truncate;
6 27     27   112 use base qw/Jifty::DBI::Filter/;
  27         29  
  27         11577  
7 27     27   134 use Encode ();
  27         28  
  27         3096  
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 338     338 1 353 my $self = shift;
33              
34 338         806 my $value_ref = $self->value_ref;
35 338 100       1709 return undef unless ( defined($$value_ref) );
36              
37 299         679 my $column = $self->column();
38              
39 299         770 my $truncate_to;
40 299 100 100     767 if ( $column->max_length && !$column->is_numeric ) {
    100 66        
41 3         7 $truncate_to = $column->max_length;
42             } elsif ( $column->type && $column->type =~ /char\((\d+)\)/ ) {
43 92         1691 $truncate_to = $1;
44             }
45              
46 299 100       3606 return unless ($truncate_to); # don't need to truncate
47              
48 95         265 my $utf8 = Encode::is_utf8($$value_ref);
49             {
50 27     27   136 use bytes;
  27         32  
  27         171  
  95         99  
51 95         240 $$value_ref = substr( $$value_ref, 0, $truncate_to );
52             }
53 95 100       646 if ($utf8) {
54              
55             # return utf8 flag back, but use Encode::FB_QUIET because
56             # we could broke tail char
57 2         10 $$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;