File Coverage

blib/lib/Localizer/Resource.pm
Criterion Covered Total %
statement 51 54 94.4
branch 13 18 72.2
condition 3 5 60.0
subroutine 11 11 100.0
pod 2 2 100.0
total 80 90 88.8


line stmt bran cond sub pod time code
1             package Localizer::Resource;
2 5     5   172992 use strict;
  5         41  
  5         126  
3 5     5   24 use warnings;
  5         6  
  5         111  
4 5     5   20 use utf8;
  5         9  
  5         25  
5 5     5   162 use 5.010_001;
  5         14  
6              
7 5     5   1787 use Localizer::Style::Gettext;
  5         11  
  5         123  
8 5     5   1989 use Localizer::BuiltinFunctions;
  5         11  
  5         129  
9 5     5   1605 use Localizer::Lexicon;
  5         11  
  5         335  
10              
11             our $BUILTIN_FUNCTIONS = {
12             numf => \&Localizer::BuiltinFunctions::numf,
13             numerate => \&Localizer::BuiltinFunctions::numerate,
14             quant => \&Localizer::BuiltinFunctions::quant,
15             sprintf => \&Localizer::BuiltinFunctions::sprintf,
16             };
17              
18             use Class::Accessor::Lite 0.05 (
19 5         25 rw => [qw(dictionary compiled precompile style functions)],
20 5     5   30 );
  5         56  
21              
22             sub new {
23 6     6 1 4750 my $class = shift;
24 6 50       42 my %args = @_==1 ? %{$_[0]} : @_;
  0         0  
25              
26 6 50       24 unless (exists $args{dictionary}) {
27 0         0 Carp::confess("Missing mandatory parameter: dictionary");
28             }
29              
30             $args{dictionary} = Localizer::Lexicon->new($args{dictionary})
31 6 50       59 if ref($args{dictionary}) eq 'HASH';
32              
33 6   66     47 $args{style} ||= Localizer::Style::Gettext->new();
34              
35 6         12 my $functions = do {
36 6 100       15 if (exists $args{functions}) {
37             +{
38             %$BUILTIN_FUNCTIONS,
39 2         7 %{delete $args{functions}},
  2         15  
40             };
41             } else {
42 4         23 $BUILTIN_FUNCTIONS
43             }
44             };
45              
46 6         37 my $self = bless {
47             compiled => +{},
48             precompile => 1,
49             functions => $functions,
50             %args,
51             }, $class;
52              
53             # Compile dictionary data to CodeRef or ScalarRef
54 6 100       37 if ($self->precompile) {
55 3         29 for my $msgid ($self->dictionary->msgids) {
56 5         25 $self->_compile($msgid);
57             }
58             }
59              
60 6         40 return $self;
61             }
62              
63             sub maketext {
64 30     30 1 6617 my ($self, $msgid, @args) = @_;
65              
66 30         63 my $compiled = $self->_compile($msgid);
67 28 50       64 return unless defined $compiled;
68              
69 28 100       75 if (ref $compiled eq 'CODE') {
    50          
70 19         42 return $compiled->($self, @args);
71             } elsif (ref $compiled eq 'SCALAR') {
72 9         50 return $$compiled;
73             } else {
74 0         0 die "SHOULD NOT REACH HERE";
75             }
76             }
77              
78             sub _compile {
79 35     35   57 my ($self, $msgid) = @_;
80              
81 35 100       86 if (my $code = $self->compiled->{$msgid}) {
82 5         25 return $code;
83             }
84              
85 30   50     165 my $fmt = $self->dictionary->msgstr($msgid) || return;
86 30         306 my $code = $self->style->compile($msgid, $fmt, $self->functions);
87 28         69 $self->compiled->{$msgid} = $code;
88 28         119 return $code;
89             }
90              
91             1;
92              
93             __END__