File Coverage

blib/lib/Log/Report/Message.pm
Criterion Covered Total %
statement 79 93 84.9
branch 36 64 56.2
condition 11 28 39.2
subroutine 27 30 90.0
pod 17 17 100.0
total 170 232 73.2


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 13     13   77 use warnings;
  13         26  
  13         370  
6 13     13   60 use strict;
  13         25  
  13         342  
7              
8             package Log::Report::Message;
9 13     13   108 use vars '$VERSION';
  13         28  
  13         592  
10             $VERSION = '1.21';
11              
12              
13 13     13   71 use Log::Report 'log-report';
  13         26  
  13         65  
14 13     13   79 use POSIX qw/locale_h/;
  13         26  
  13         148  
15 13     13   1604 use List::Util qw/first/;
  13         39  
  13         697  
16 13     13   72 use Scalar::Util qw/blessed/;
  13         25  
  13         465  
17              
18 13     13   69 use Log::Report::Util qw/to_html/;
  13         25  
  13         445  
19              
20             # Work-around for missing LC_MESSAGES on old Perls and Windows
21 13     13   66 { no warnings;
  13         24  
  13         1410  
22             eval "&LC_MESSAGES";
23             *LC_MESSAGES = sub(){5} if $@;
24             }
25              
26              
27             use overload
28             '""' => 'toString'
29 2     2   6 , '&{}' => sub { my $obj = shift; sub{$obj->clone(@_)} }
  2         27  
  2         7  
30 13         139 , '.' => 'concat'
31 13     13   78 , fallback => 1;
  13         21  
32              
33              
34             sub new($@)
35 139     139 1 1535 { my ($class, %s) = @_;
36              
37 139 50       399 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 139 100       460 or $s{_join} = $";
44              
45 139 100       324 if($s{_msgid})
46             { $s{_append} = defined $s{_append} ? $1.$s{_append} : $1
47 127 50       551 if $s{_msgid} =~ s/(\s+)$//s;
    100          
48              
49             $s{_prepend}.= $1
50 127 100       427 if $s{_msgid} =~ s/^(\s+)//s;
51             }
52 139 100       318 if($s{_plural})
53 20         70 { s/\s+$//, s/^\s+// for $s{_plural};
54             }
55              
56 139         577 bless \%s, $class;
57             }
58              
59              
60             sub clone(@)
61 2     2 1 4 { my $self = shift;
62 2         9 (ref $self)->new(%$self, @_);
63             }
64              
65              
66             sub fromTemplateToolkit($$;@)
67 0     0 1 0 { my ($class, $domain, $msgid) = splice @_, 0, 3;
68 0 0       0 my $plural = $msgid =~ s/\|(.*)// ? $1 : undef;
69 0 0 0     0 my $args = @_ && ref $_[-1] eq 'HASH' ? pop : {};
70              
71 0         0 my $count;
72 0 0       0 if(defined $plural)
73 0 0       0 { @_==1 or $msgid .= " (ERROR: missing count for plural)";
74 0   0     0 $count = shift || 0;
75 0 0       0 $count = @$count if ref $count eq 'ARRAY';
76             }
77             else
78 0 0       0 { @_==0 or $msgid .= " (ERROR: only named parameters expected)";
79             }
80              
81 0         0 $class->new
82             ( _msgid => $msgid, _plural => $plural, _count => $count
83             , %$args, _expand => 1, _domain => $domain);
84             }
85              
86             #----------------
87              
88 2     2 1 9 sub prepend() {shift->{_prepend}}
89 101     101 1 1115 sub msgid() {shift->{_msgid}}
90 2     2 1 12 sub append() {shift->{_append}}
91 26     26 1 124 sub domain() {shift->{_domain}}
92 0     0 1 0 sub count() {shift->{_count}}
93 0     0 1 0 sub context() {shift->{_context}}
94              
95              
96             sub classes()
97 9   50 9 1 48 { my $class = $_[0]->{_class} || $_[0]->{_classes} || [];
98 9 50       63 ref $class ? @$class : split(/[\s,]+/, $class);
99             }
100              
101              
102             sub to(;$)
103 15     15 1 59 { my $self = shift;
104 15 50       174 @_ ? $self->{_to} = shift : $self->{_to};
105             }
106              
107              
108 2     2 1 12 sub valueOf($) { $_[0]->{$_[1]} }
109              
110             #--------------
111              
112             sub inClass($)
113 8     8 1 1147 { my @classes = shift->classes;
114             ref $_[0] eq 'Regexp'
115 3     3   21 ? (first { $_ =~ $_[0] } @classes)
116 8 100   10   58 : (first { $_ eq $_[0] } @classes);
  10         48  
117             }
118            
119              
120             sub toString(;$)
121 103     103 1 9051 { my ($self, $locale) = @_;
122 103   100     416 my $count = $self->{_count} || 0;
123 103 50       241 $locale = $self->{_lang} if $self->{_lang};
124              
125             $self->{_msgid} # no translation, constant string
126             or return (defined $self->{_prepend} ? $self->{_prepend} : '')
127 103 50       307 . (defined $self->{_append} ? $self->{_append} : '');
    50          
    100          
128              
129             # assumed is that switching locales is expensive
130 87         309 my $oldloc = setlocale(LC_MESSAGES);
131 87 0 0     210 setlocale(LC_MESSAGES, $locale)
      33        
132             if defined $locale && (!defined $oldloc || $locale ne $oldloc);
133              
134             # translate the msgid
135 87         156 my $domain = $self->{_domain};
136 87 50       344 $domain = textdomain $domain
137             unless blessed $domain;
138              
139 87   33     412 my $format = $domain->translate($self, $locale || $oldloc);
140 87 50       204 defined $format or return ();
141              
142             # fill-in the fields
143             my $text = $self->{_expand}
144             ? $domain->interpolate($format, $self)
145 87 100 100     456 : ($self->{_prepend} // '') . $format . ($self->{_append} // '');
      100        
146              
147 87 50 33     8950 setlocale(LC_MESSAGES, $oldloc)
      33        
148             if defined $oldloc && (!defined $locale || $oldloc ne $locale);
149              
150 87         414 $text;
151             }
152              
153              
154              
155             my %tohtml = qw/ > gt < lt " quot & amp /;
156              
157 1     1 1 5 sub toHTML(;$) { to_html($_[0]->toString($_[1])) }
158              
159              
160             sub untranslated()
161 2     2 1 5 { my $self = shift;
162             (defined $self->{_prepend} ? $self->{_prepend} : '')
163             . (defined $self->{_msgid} ? $self->{_msgid} : '')
164 2 100       20 . (defined $self->{_append} ? $self->{_append} : '');
    50          
    50          
165             }
166              
167              
168             sub concat($;$)
169 26     26 1 655 { my ($self, $what, $reversed) = @_;
170 26 100       56 if($reversed)
171 12 100       31 { $what .= $self->{_prepend} if defined $self->{_prepend};
172 12         55 return ref($self)->new(%$self, _prepend => $what);
173             }
174              
175 14 100       41 $what = $self->{_append} . $what if defined $self->{_append};
176 14         56 ref($self)->new(%$self, _append => $what);
177             }
178              
179             #----------------
180              
181             1;