File Coverage

blib/lib/Log/Report/Message.pm
Criterion Covered Total %
statement 79 96 82.2
branch 36 64 56.2
condition 11 28 39.2
subroutine 27 32 84.3
pod 18 18 100.0
total 171 238 71.8


line stmt bran cond sub pod time code
1             # Copyrights 2007-2017 by [Mark Overmeer].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.02.
5 15     15   85 use warnings;
  15         28  
  15         477  
6 15     15   77 use strict;
  15         28  
  15         460  
7              
8             package Log::Report::Message;
9 15     15   88 use vars '$VERSION';
  15         26  
  15         749  
10             $VERSION = '1.23';
11              
12              
13 15     15   98 use Log::Report 'log-report';
  15         27  
  15         105  
14 15     15   103 use POSIX qw/locale_h/;
  15         31  
  15         88  
15 15     15   2075 use List::Util qw/first/;
  15         31  
  15         895  
16 15     15   109 use Scalar::Util qw/blessed/;
  15         28  
  15         575  
17              
18 15     15   83 use Log::Report::Util qw/to_html/;
  15         55  
  15         543  
19              
20             # Work-around for missing LC_MESSAGES on old Perls and Windows
21 15     15   69 { no warnings;
  15         25  
  15         1701  
22             eval "&LC_MESSAGES";
23             *LC_MESSAGES = sub(){5} if $@;
24             }
25              
26              
27             use overload
28             '""' => 'toString'
29 2     2   9 , '&{}' => sub { my $obj = shift; sub{$obj->clone(@_)} }
  2         13  
  2         8  
30 15         171 , '.' => 'concat'
31 15     15   85 , fallback => 1;
  15         25  
32              
33              
34             sub new($@)
35 143     143 1 1728 { my ($class, %s) = @_;
36              
37 143 50       403 if(ref $s{_count})
38 0         0 { my $c = $s{_count};
39 0 0       0 $s{_count} = ref $c eq 'ARRAY' ? @$c : keys %$c;
40             }
41              
42             defined $s{_join}
43 143 100       430 or $s{_join} = $";
44              
45 143 100       292 if($s{_msgid})
46             { $s{_append} = defined $s{_append} ? $1.$s{_append} : $1
47 130 50       606 if $s{_msgid} =~ s/(\s+)$//s;
    100          
48              
49             $s{_prepend}.= $1
50 130 100       410 if $s{_msgid} =~ s/^(\s+)//s;
51             }
52 143 100       271 if($s{_plural})
53 20         68 { s/\s+$//, s/^\s+// for $s{_plural};
54             }
55              
56 143         586 bless \%s, $class;
57             }
58              
59             # internal use only: to simplify __*p* functions
60 0     0   0 sub _msgctxt($) {$_[0]->{_msgctxt} = $_[1]; $_[0]}
  0         0  
61              
62              
63             sub clone(@)
64 2     2 1 4 { my $self = shift;
65 2         12 (ref $self)->new(%$self, @_);
66             }
67              
68              
69             sub fromTemplateToolkit($$;@)
70 0     0 1 0 { my ($class, $domain, $msgid) = splice @_, 0, 3;
71 0 0       0 my $plural = $msgid =~ s/\|(.*)// ? $1 : undef;
72 0 0 0     0 my $args = @_ && ref $_[-1] eq 'HASH' ? pop : {};
73              
74 0         0 my $count;
75 0 0       0 if(defined $plural)
76 0 0       0 { @_==1 or $msgid .= " (ERROR: missing count for plural)";
77 0   0     0 $count = shift || 0;
78 0 0       0 $count = @$count if ref $count eq 'ARRAY';
79             }
80             else
81 0 0       0 { @_==0 or $msgid .= " (ERROR: only named parameters expected)";
82             }
83              
84 0         0 $class->new
85             ( _msgid => $msgid, _plural => $plural, _count => $count
86             , %$args, _expand => 1, _domain => $domain);
87             }
88              
89             #----------------
90              
91 2     2 1 11 sub prepend() {shift->{_prepend}}
92 103     103 1 1109 sub msgid() {shift->{_msgid}}
93 2     2 1 11 sub append() {shift->{_append}}
94 27     27 1 80 sub domain() {shift->{_domain}}
95 0     0 1 0 sub count() {shift->{_count}}
96 0     0 1 0 sub context() {shift->{_context}}
97 0     0 1 0 sub msgctxt() {shift->{_msgctxt}}
98              
99              
100             sub classes()
101 9   50 9 1 44 { my $class = $_[0]->{_class} || $_[0]->{_classes} || [];
102 9 50       70 ref $class ? @$class : split(/[\s,]+/, $class);
103             }
104              
105              
106             sub to(;$)
107 16     16 1 31 { my $self = shift;
108 16 50       204 @_ ? $self->{_to} = shift : $self->{_to};
109             }
110              
111              
112 2     2 1 15 sub valueOf($) { $_[0]->{$_[1]} }
113              
114             #--------------
115              
116             sub inClass($)
117 8     8 1 1425 { my @classes = shift->classes;
118             ref $_[0] eq 'Regexp'
119 3     3   26 ? (first { $_ =~ $_[0] } @classes)
120 8 100   10   64 : (first { $_ eq $_[0] } @classes);
  10         48  
121             }
122            
123              
124             sub toString(;$)
125 106     106 1 8634 { my ($self, $locale) = @_;
126 106   100     412 my $count = $self->{_count} || 0;
127 106 50       256 $locale = $self->{_lang} if $self->{_lang};
128              
129             $self->{_msgid} # no translation, constant string
130             or return (defined $self->{_prepend} ? $self->{_prepend} : '')
131 106 50       310 . (defined $self->{_append} ? $self->{_append} : '');
    50          
    100          
132              
133             # assumed is that switching locales is expensive
134 89         339 my $oldloc = setlocale(LC_MESSAGES);
135 89 0 0     193 setlocale(LC_MESSAGES, $locale)
      33        
136             if defined $locale && (!defined $oldloc || $locale ne $oldloc);
137              
138             # translate the msgid
139 89         137 my $domain = $self->{_domain};
140 89 50       325 $domain = textdomain $domain
141             unless blessed $domain;
142              
143 89   33     373 my $format = $domain->translate($self, $locale || $oldloc);
144 89 50       178 defined $format or return ();
145              
146             # fill-in the fields
147             my $text = $self->{_expand}
148             ? $domain->interpolate($format, $self)
149 89 100 100     429 : ($self->{_prepend} // '') . $format . ($self->{_append} // '');
      100        
150              
151 89 50 33     9818 setlocale(LC_MESSAGES, $oldloc)
      33        
152             if defined $oldloc && (!defined $locale || $oldloc ne $locale);
153              
154 89         395 $text;
155             }
156              
157              
158              
159             my %tohtml = qw/ > gt < lt " quot & amp /;
160              
161 1     1 1 5 sub toHTML(;$) { to_html($_[0]->toString($_[1])) }
162              
163              
164             sub untranslated()
165 2     2 1 4 { my $self = shift;
166             (defined $self->{_prepend} ? $self->{_prepend} : '')
167             . (defined $self->{_msgid} ? $self->{_msgid} : '')
168 2 100       15 . (defined $self->{_append} ? $self->{_append} : '');
    50          
    50          
169             }
170              
171              
172             sub concat($;$)
173 26     26 1 569 { my ($self, $what, $reversed) = @_;
174 26 100       42 if($reversed)
175 12 100       20 { $what .= $self->{_prepend} if defined $self->{_prepend};
176 12         38 return ref($self)->new(%$self, _prepend => $what);
177             }
178              
179 14 100       35 $what = $self->{_append} . $what if defined $self->{_append};
180 14         52 ref($self)->new(%$self, _append => $what);
181             }
182              
183             #----------------
184              
185             1;