blib/lib/DBIx/Class/ForceUTF8.pm

filter: Statementstmt 42.1% 16 / 38 Branchbran 4.1% 1 / 24 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 62.5% 5 / 8 Podpod 100% 3 / 3 totaltotal 30.1% 25 / 83 CC 16 SCAR 33.6
CC
16
Cov
25%
get_column
42.0
store_column
42.0
get_columns
30.0
CRAP 28.8
↑ Summary
1 package DBIx::Class::ForceUTF8;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 $VERSION = '0.0.2';
6  
7 1 ▶use base qw/DBIx::Class/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=8.1
10  
11 # Perl 5.8.0 doesn't have utf8::is_utf8()
12 # Yes, 5.8.0 support for Unicode is suboptimal, but things like RHEL3 ship with it.
13 1 ▶ if ($] <= 5.008000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] <= 5.008) { } 0 1 1
14 0 require Encode;
15 } else {
16 1 require utf8;
17 }
18 }
19  
20 =head1 NAME
21  
22 DBIx::Class::ForceUTF8 - Force UTF8 (Unicode) flag on columns
23  
24 =head1 SYNOPSIS
25  
26 package Artist;
27 __PACKAGE__->load_components(qw/ForceUTF8 Core/);
28
29 # then belows return strings with utf8 flag
30 $artist->name;
31 $artist->get_column('description');
32  
33 # with DBIx::Class::Schema::Loader
34 package My::Schema;
35 use base qw/DBIx::Class::Schema::Loader/;
36  
37 __PACKAGE__->loader_options(
38 components => [qw/ForceUTF8/],
39 );
40  
41  
42 =head1 DESCRIPTION
43  
44 DBIx::Class::ForceUTF8 allows you to get columns data that have Unicode flag without specifying a column name.
45 Best used with DBIx::Class::Schema::Loader.
46  
47 =head1 SEE ALSO
48  
49 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::UTF8Columns>.
50  
51 =head1 EXTENDED METHODS
52  
53 =head2 get_column
54  
55 =cut
56  
57 0 ▶sub get_column {
Subroutine get_column: not called CC=6 SCAR=37.4
Pod documented
58 0 my ( $self, $column ) = @_;
59 0 my $value = $self->next::method($column);
60  
61 0 ▶ if ( defined $value and !ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not ref $value) 0 0 0
Condition0%0/3
defined $value and not ref $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and not ref $value
defined $value not ref $value
Truth table0%0/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
62 0 ▶ if ($] <= 5.008000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] <= 5.008) { } 0 0 0
63 0 ▶ Encode::_utf8_on($value) unless Encode::is_utf8($value);
Branch: 1 branch
BranchTrueFalseTotal
unless &Encode::is_utf8($value) 0 0 0
64 } else {
65 0 ▶ utf8::decode($value) unless utf8::is_utf8($value);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($value) 0 0 0
66 }
67 }
68  
69 0 $value;
70 }
71  
72 =head2 get_columns
73  
74 =cut
75  
76 0 ▶sub get_columns {
Subroutine get_columns: not called CC=5 SCAR=34.0
Pod documented
77 0 my $self = shift;
78 0 my %data = $self->next::method(@_);
79  
80 0 foreach my $col ( grep { !ref $data{$_} } keys %data ) {
81  
82 0 ▶ if ($] <= 5.008000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] <= 5.008) { } 0 0 0
83 0 ▶ Encode::_utf8_on($data{$col}) unless Encode::is_utf8($data{$col});
Branch: 1 branch
BranchTrueFalseTotal
unless &Encode::is_utf8($data{$col}) 0 0 0
84 } else {
85 0 ▶ utf8::decode($data{$col}) unless utf8::is_utf8($data{$col});
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($data{$col}) 0 0 0
86 }
87 }
88  
89 0 %data;
90 }
91  
92 =head2 store_column
93  
94 =cut
95  
96 0 ▶sub store_column {
Subroutine store_column: not called CC=6 SCAR=37.4
Pod documented
97 0 my ( $self, $column, $value ) = @_;
98  
99 0 ▶ if ( defined $value and !ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not ref $value) 0 0 0
Condition0%0/3
defined $value and not ref $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and not ref $value
defined $value not ref $value
Truth table0%0/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
100  
101 0 ▶ if ($] <= 5.008000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] <= 5.008) { } 0 0 0
102 0 ▶ Encode::_utf8_off($value) if Encode::is_utf8($value);
Branch: 1 branch
BranchTrueFalseTotal
if &Encode::is_utf8($value) 0 0 0
103 } else {
104 0 ▶ utf8::encode($value) if utf8::is_utf8($value);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($value) 0 0 0
105 }
106 }
107  
108 0 $self->next::method( $column, $value );
109 }
110  
111 =head1 AUTHOR
112  
113 Takahiro Horikawa <horikawa.takahiro@gmail.com>
114  
115 =head1 COPYRIGHT
116  
117 This program is free software; you can redistribute
118 it and/or modify it under the same terms as Perl itself.
119  
120 The full text of the license can be found in the
121 LICENSE file included with this module.
122  
123 =cut
124  
125 1 1;
126  
↑ Summary