I18n.pm

filter: Statementstmt 76.4% 78 / 102 Branchbran 54.1% 13 / 24 Conditioncond 55.5% 10 / 18 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 76.1% 16 / 21 Podpod 50.0% 4 / 8 totaltotal 66.3% 122 / 184 CC 22 SCAR 16.5
CC
22
Cov
70%
__ANON__
16.6
inject
12.0
register
6.4
CRAP 5.2
« I18n/Storage/CDBI.pm ↑ Summary I18n/Storage.pm »
1 package Object::I18n;
2  
3 2 ▶use 5.008003;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # Not using Exporter
8 2 our @EXPORT = qw(i18n);
9 2 our $VERSION = '0.02';
10  
11 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 Object::I18n - Internationalize objects
17  
18 =head1 SYNOPSIS
19  
20 package Greeting;
21 sub new { my $x = $_[1]; bless \$x; }
22 sub greeting { @_ ? ${$_[1]} : (${$_[1]} = $_[2]) }
23 use Object::I18n qw(id);
24 __PACKAGE__->i18n->storage('Greeting::CDBI::I18n');
25 __PACKAGE__->i18n->register('greeting');
26
27 package main;
28 my $obj = Greeting->new("Hello, world\n");
29 print $obj->greeting;# "Hello, world\n"
30 $obj->i18n->language('fr');
31 print $obj->greeting;# exception
32 $obj->greeting("Bonjour, monde\n");
33 print $obj->greeting;# "Bonjour, monde\n"
34
35 =cut
36  
37 2 my %i18n_object;
38  
39 21 ▶sub i18n {
Subroutine i18n: called CC=4 SCAR=13.9
Pod undocumented
40 21 my $self = shift;
41 21 ▶ my $class = ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
11100
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
42 21 my $i18n = $i18n_object{$class};
43 21 ▶ return $i18n unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 10 11 21
44  
45 11 _prune($i18n->{instance});
46 11 my $oid_method = $i18n->{oid_method};
47 11 my $oid = $self->$oid_method;
48 11 my $instance_i18n = $i18n->{instance}{$oid};
49
50 11 ▶ $i18n->{instance}{$oid} ||= $i18n->_clone($self);
Condition66%2/3
$i18n->{"instance"}{$oid} ||= $i18n->_clone($self)
l!l&&r!l&&!r
740
MC/DC0%0/2
$i18n->{"instance"}{$oid} ||= $i18n->_clone($self)
$i18n->{"instance"}{$oid} $i18n->_clone($self)
Truth table66%2/3
A ||= B
A= $i18n->{"instance"}{$oid}B= $i18n->_clone($self)
ABresult
1X1
011
000
51 }
52  
53 2 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
54 2 my ($class, %opts) = (@_);
55 2 my ($pkg) = caller;
56 2 $class->_init_i18n_object($pkg, %opts);
57 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
58 2 *{ "$pkg\::i18n" } = \&i18n;
59 }
60  
61 0 ▶sub oid {
Subroutine oid: not called CC=1 SCAR=6.9
Pod undocumented
62 0 my $self = shift;
63 0 my $oid_method = $self->{oid_method};
64 0 return $self->{object}->$oid_method;
65 }
66  
67 11 ▶sub _prune {
Subroutine _prune: called CC=3 SCAR=11.0
68 11 my ($instance) = @_;
69 11 for my $oid (keys %$instance) {
70 20 ▶ next if $instance->{$oid}{object};
Branch: 1 branch
BranchTrueFalseTotal
if $instance->{$oid}{"object"} 19 1 20
71 1 delete $instance->{$oid};
72 }
73 }
74  
75 4 ▶sub _clone {
Subroutine _clone: called CC=1 SCAR=0.0
76 4 my $self = shift;
77 4 my ($obj) = @_;
78 # XXX - may need deeper cloning than this
79 4 my $clone = { %$self };
80 4 delete $clone->{instance};
81 4 delete $clone->{language};
82 4 $clone->{object} = $obj;
83 # $clone->{object} will be undef when the object is DESTROYed.
84 4 weaken($clone->{object});
85 4 return bless $clone, __PACKAGE__;
86 }
87  
88 # The i18n object has these attributes:
89 # class - The package of the class or object
90 # oid_method - A method in "class" that returns a unique object id
91 # language - The current language for the class or object
92 # object - The object that contains the i18n object
93 # instance - A hash of i18n instances stored in the class i18n
94 # registered_methods - A hash of overridden methods
95 #
96 2 ▶sub _init_i18n_object {
Subroutine _init_i18n_object: called CC=5 SCAR=16.8
97 2 my $class = shift;
98 2 my ($pkg, %attrs) = @_;
99 2 ▶ $attrs{oid_method} ||= 'id';
Condition50%1/2
$attrs{"oid_method"} ||= "id"
l!l
02
MC/DC0%0/1
$attrs{"oid_method"} ||= "id"
$attrs{"oid_method"}
Truth table50%1/2
A
A= $attrs{"oid_method"}
Aresult
11
00
100 2 ▶ $attrs{storage_class} ||= 'Object::I18n::Storage::MemHash';
Condition50%1/2
$attrs{"storage_class"} ||= "Object::I18n::Storage::MemHash"
l!l
02
MC/DC0%0/1
$attrs{"storage_class"} ||= "Object::I18n::Storage::MemHash"
$attrs{"storage_class"}
Truth table50%1/2
A
A= $attrs{"storage_class"}
Aresult
11
00
101 2 ▶ croak "invalid storage_class" if $attrs{storage_class} =~ /[^\w:]/;
Branch: 1 branch
BranchTrueFalseTotal
if $attrs{"storage_class"} =~ /[^\w:]/ 0 2 2
102 2 ▶ eval "require $attrs{storage_class}; 1" or die $@;
Branch: 1 branch
BranchTrueFalseTotal
eval qq[require $attrs{"storage_class"}; 1] or die $@ 2 0 2
103  
104 2 my $obj = bless {
105 class => $pkg,
106 %attrs,
107 }, $class;
108 2 $i18n_object{$pkg} = $obj;
109 }
110  
111 3 ▶sub register {
Subroutine register: called CC=5 SCAR=18.6
Pod documented
112 3 my $self = shift;
113 3 my @methods = @_;
114 3 ▶ my $registered = $self->{registered_methods} ||= {};
Condition100%2/2
$self->{"registered_methods"} ||= {}
l!l
12
MC/DC100%1/1
$self->{"registered_methods"} ||= {}
$self->{"registered_methods"}
Truth table100%2/2
A
A= $self->{"registered_methods"}
Aresult
11
00
115 my $class = $self->{class}
116 3 ▶ or die "register must be called as class method";
Branch: 1 branch
BranchTrueFalseTotal
my $class = $self->{"class"} or die "register must be called as class method" 3 0 3
117 3 for my $method (@methods) {
118 4 ▶ croak "No such method '$method' found for class '$class'"
Branch: 1 branch
BranchTrueFalseTotal
unless my $code = $class->can($method) 1 3 4
119 unless my $code = $class->can($method);
120 3 $registered->{$method} = $code;
121 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
122 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
123 3 ▶ *{ "$class\::$method" } = sub {
Subroutine __ANON__: not called CC=4 SCAR=28.1
124 0 my $self = shift;
125 0 ▶ return $self->$code(@_) unless $self->i18n->language;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->i18n->language 0 0 0
126  
127 0 my $storage_class = $self->i18n->{storage_class};
128 0 ▶ my $storage = $storage_class->new($self, $method)
Branch: 1 branch
BranchTrueFalseTotal
my $storage = $storage_class->new($self, $method) or croak("Could not create instance of '${storage_class}'" . "for '${method}'") 0 0 0
129 or croak "Could not create instance of '$storage_class'"
130 . "for '$method'";
131 0 ▶ return $storage->store(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
132 0 return $storage->fetch;
133 3 };
134 }
135 2 keys %$registered;
136 }
137  
138 0 ▶sub storage_class {
Subroutine storage_class: not called CC=1 SCAR=6.9
Pod documented
139 0 my $self = shift;
140 0 $self->{storage_class};
141 }
142  
143 1 ▶sub registered_methods {
Subroutine registered_methods: called CC=1 SCAR=0.0
Pod undocumented
144 1 my $self = shift;
145 1 keys %{ $self->{registered_methods} };
146 }
147  
148 14 ▶sub language {
Subroutine language: called CC=4 SCAR=13.9
Pod documented
149 14 my $self = shift;
150 14 ▶ return $self->{language} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 11 14
151  
152 11 ▶ return $self->{language} unless exists $self->{object};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"object"} 6 5 11
153 5 ▶ return $self->{language} || $self->{class}->i18n->language;
Condition66%2/3
$self->{'language'} || $self->{'class'}->i18n->language
l!l&&r!l&&!r
140
MC/DC0%0/2
$self->{'language'} || $self->{'class'}->i18n->language
$self->{'language'} $self->{'class'}->i18n->language
Truth table66%2/3
A || B
A= $self->{'language'}B= $self->{'class'}->i18n->language
ABresult
1X1
011
000
154 }
155  
156 # ->inject(attr=>'question',language=>'fr',notes=>$notes, data=>$data);
157 0 ▶sub inject {
Subroutine inject: not called CC=3 SCAR=24.8
Pod documented
158 0 my $self = shift;
159 0 my %opts = @_;
160  
161 0 my $storage_class = $self->{storage_class};
162 0 local($self->{language}) = $opts{language};
163 0 my $storage = $storage_class->new($self->{object}, $opts{attr});
164 0 $storage->store($opts{data});
165 0 ▶ return unless $opts{notes} and my $history_class = $self->{history_class};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"notes"} and my $history_class = $self->{"history_class"} 0 0 0
Condition0%0/3
$opts{"notes"} and my $history_class = $self->{"history_class"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{"notes"} and my $history_class = $self->{"history_class"}
$opts{"notes"} my $history_class = $self->{"history_class"}
Truth table0%0/3
A and B
A= $opts{"notes"}B= my $history_class = $self->{"history_class"}
ABresult
0X0
100
111
166  
167 0 $history_class->record($opts{notes});
168 }
169  
170 0 ▶sub is_injected {
Subroutine is_injected: not called CC=1 SCAR=6.9
Pod undocumented
171 0 my $self = shift;
172 0 my %opts = @_;
173  
174 0 my $storage_class = $self->{storage_class};
175 0 my $storage = $storage_class->new($self->{object}, $opts{attr});
176 0 return $storage->fetch;
177 }
178  
179 =head1 DESCRIPTION
180  
181 Object::I18n overrides methods in your class to return international content.
182 It provides one mixin method, i18n(), which returns an Object::I18n object.
183 The object returned is different depending on whether you call it on your
184 class or an instance of your class. See L<"METHODS"> below.
185  
186 =head2 METHODS
187  
188 Most methods can be either class methods or object methods but this
189 doesn't mean what you may be accustomed to. A method is considered to
190 act as a class method if it is called on an Object::I18n object returned
191 from the class form of the i18n() method. It acts as an object method
192 when called on the object returned from the object form of the i18n()
193 method.
194  
195 =over
196  
197 =item language [LANGUAGE]
198  
199 Returns and optionally sets the current language. If called as a class
200 method it affects all instances of a class, except those that have set
201 language() for themselves. If unset, methods should behave as if
202 Object::I18n was not being used at all.
203  
204 =item register METHODLIST
205  
206 Registers the list of method names as i14able. The methods will be
207 overridden so that they return i14ed content when C<language> is set.
208  
209 =item storage_class [CLASS]
210  
211 Returns and optionally set the class that controls how translations
212 are stored.
213  
214 =item inject OPTIONS
215  
216 Injects a new translation into your C<storage_class>. The options are:
217  
218 =over
219  
220 =item language
221  
222 The language the translation is in. If not set then the language
223 returned by the language() method will be used.
224  
225 =item attr
226  
227 The attribute (method) in your class that the translation is for.
228  
229 =item data
230  
231 The actual translated text to be stored.
232  
233 =item notes
234  
235 Any notes to be saved along with the translation. Requires you
236 to have configured a C<history_class>.
237  
238 =back
239  
240 =back
241  
242 =head1 EXPORT
243  
244 C<i18n>.
245  
246 =head1 AUTHOR
247  
248 Rick Delaney, E<lt>rick@bort.caE<gt>
249  
250 =head1 ACKNOWLEDGEMENTS
251  
252 To be filled in.
253  
254 =head1 COPYRIGHT AND LICENSE
255  
256 Copyright (C) 2005 by Rick Delaney
257  
258 This library is free software; you can redistribute it and/or modify
259 it under the same terms as Perl itself, either Perl version 5.8.3 or,
260 at your option, any later version of Perl 5 you may have available.
261  
262  
263 =cut
264  
265 2 1;
« I18n/Storage/CDBI.pm ↑ Summary I18n/Storage.pm »