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 13     13   67 use warnings;
  13         22  
  13         354  
6 13     13   56 use strict;
  13         17  
  13         297  
7              
8             package Log::Report::Message;
9 13     13   49 use vars '$VERSION';
  13         20  
  13         524  
10             $VERSION = '1.22';
11              
12              
13 13     13   64 use Log::Report 'log-report';
  13         22  
  13         63  
14 13     13   69 use POSIX qw/locale_h/;
  13         21  
  13         58  
15 13     13   1488 use List::Util qw/first/;
  13         21  
  13         629  
16 13     13   61 use Scalar::Util qw/blessed/;
  13         18  
  13         465  
17              
18 13     13   64 use Log::Report::Util qw/to_html/;
  13         20  
  13         430  
19              
20             # Work-around for missing LC_MESSAGES on old Perls and Windows
21 13     13   55 { no warnings;
  13         19  
  13         1317  
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         9  
  2         5  
30 13         99 , '.' => 'concat'
31 13     13   65 , fallback => 1;
  13         20  
32              
33              
34             sub new($@)
35 139     139 1 1335 { my ($class, %s) = @_;
36              
37 139 50       324 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       341 or $s{_join} = $";
44              
45 139 100       240 if($s{_msgid})
46             { $s{_append} = defined $s{_append} ? $1.$s{_append} : $1
47 127 50       482 if $s{_msgid} =~ s/(\s+)$//s;
    100          
48              
49             $s{_prepend}.= $1
50 127 100       348 if $s{_msgid} =~ s/^(\s+)//s;
51             }
52 139 100       227 if($s{_plural})
53 20         58 { s/\s+$//, s/^\s+// for $s{_plural};
54             }
55              
56 139         478 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 3 { my $self = shift;
65 2         9 (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 101     101 1 1073 sub msgid() {shift->{_msgid}}
93 2     2 1 11 sub append() {shift->{_append}}
94 26     26 1 68 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 48 { my $class = $_[0]->{_class} || $_[0]->{_classes} || [];
102 9 50       67 ref $class ? @$class : split(/[\s,]+/, $class);
103             }
104              
105              
106             sub to(;$)
107 15     15 1 26 { my $self = shift;
108 15 50       155 @_ ? $self->{_to} = shift : $self->{_to};
109             }
110              
111              
112 2     2 1 10 sub valueOf($) { $_[0]->{$_[1]} }
113              
114             #--------------
115              
116             sub inClass($)
117 8     8 1 1341 { my @classes = shift->classes;
118             ref $_[0] eq 'Regexp'
119 3     3   18 ? (first { $_ =~ $_[0] } @classes)
120 8 100   10   50 : (first { $_ eq $_[0] } @classes);
  10         35  
121             }
122            
123              
124             sub toString(;$)
125 103     103 1 6948 { my ($self, $locale) = @_;
126 103   100     336 my $count = $self->{_count} || 0;
127 103 50       191 $locale = $self->{_lang} if $self->{_lang};
128              
129             $self->{_msgid} # no translation, constant string
130             or return (defined $self->{_prepend} ? $self->{_prepend} : '')
131 103 50       259 . (defined $self->{_append} ? $self->{_append} : '');
    50          
    100          
132              
133             # assumed is that switching locales is expensive
134 87         261 my $oldloc = setlocale(LC_MESSAGES);
135 87 0 0     165 setlocale(LC_MESSAGES, $locale)
      33        
136             if defined $locale && (!defined $oldloc || $locale ne $oldloc);
137              
138             # translate the msgid
139 87         122 my $domain = $self->{_domain};
140 87 50       259 $domain = textdomain $domain
141             unless blessed $domain;
142              
143 87   33     305 my $format = $domain->translate($self, $locale || $oldloc);
144 87 50       152 defined $format or return ();
145              
146             # fill-in the fields
147             my $text = $self->{_expand}
148             ? $domain->interpolate($format, $self)
149 87 100 100     349 : ($self->{_prepend} // '') . $format . ($self->{_append} // '');
      100        
150              
151 87 50 33     8396 setlocale(LC_MESSAGES, $oldloc)
      33        
152             if defined $oldloc && (!defined $locale || $oldloc ne $locale);
153              
154 87         345 $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       16 . (defined $self->{_append} ? $self->{_append} : '');
    50          
    50          
169             }
170              
171              
172             sub concat($;$)
173 26     26 1 484 { my ($self, $what, $reversed) = @_;
174 26 100       40 if($reversed)
175 12 100       17 { $what .= $self->{_prepend} if defined $self->{_prepend};
176 12         38 return ref($self)->new(%$self, _prepend => $what);
177             }
178              
179 14 100       31 $what = $self->{_append} . $what if defined $self->{_append};
180 14         41 ref($self)->new(%$self, _append => $what);
181             }
182              
183             #----------------
184              
185             1;