File Coverage

blib/lib/Locale/File/PO/Header/MailItem.pm
Criterion Covered Total %
statement 34 35 97.1
branch 14 20 70.0
condition 2 3 66.6
subroutine 7 7 100.0
pod 0 4 0.0
total 57 69 82.6


line stmt bran cond sub pod time code
1             package Locale::File::PO::Header::MailItem; ## no critic (TidyCode)
2              
3 5     5   36 use Moose;
  5         12  
  5         43  
4 5     5   34401 use MooseX::StrictConstructor;
  5         14  
  5         39  
5 5     5   16426 use namespace::autoclean;
  5         11  
  5         45  
6              
7             our $VERSION = '0.004';
8              
9             extends qw(Locale::File::PO::Header::Base);
10              
11             has name => (
12             is => 'rw',
13             isa => 'Str',
14             );
15              
16             has mail_name => (
17             is => 'rw',
18             isa => 'Str|Undef',
19             default => q{},
20             trigger => sub {
21             my ($self, $mail_name, $current_mail_name) = @_;
22              
23             return $self->trigger_helper({
24             new => $mail_name,
25             current => $current_mail_name,
26             default => q{},
27             writer => 'mail_name',
28             });
29             },
30             );
31              
32             has mail_address => (
33             is => 'rw',
34             isa => 'Str|Undef',
35             default => q{},
36             trigger => sub {
37             my ($self, $mail_address, $current_mail_address) = @_;
38              
39             return $self->trigger_helper({
40             new => $mail_address,
41             current => $current_mail_address,
42             default => q{},
43             writer => 'mail_address',
44             });
45             },
46             );
47              
48             sub header_keys {
49 9     9 0 13 my $self = shift;
50              
51 9         220 my $name = $self->name;
52              
53 9         29 return "${name}_name", "${name}_address";
54             }
55              
56             sub data {
57 7     7 0 16 my ($self, $key, @args) = @_;
58              
59 7 50       16 defined $key
60             or confess 'Undefined key';
61 7 100       16 my $value = @args ? $args[0] : ();
62 7 100       20 if ( $key =~ m{ _name \z }xms ) {
63             return
64             @args
65             ? $self->mail_name( # set
66             ref $value eq 'HASH'
67             ? $value->{name}
68 3 50       83 : $value
    50          
69             )
70             : $self->mail_name; # get
71             }
72 4 50       13 if ( $key =~ m{ _address \z }xms ) {
73             return
74             @args
75             ? $self->mail_address( # set
76             ref $value eq 'HASH'
77             ? $value->{address}
78 4 50       114 : $value
    100          
79             )
80             : $self->mail_address; #get
81             }
82              
83 0         0 confess "Unknown key $key";
84             }
85              
86             sub extract_msgstr {
87 6     6 0 13 my ($self, $msgstr_ref) = @_;
88              
89 6         148 my $name = $self->name;
90 6         161 ${$msgstr_ref} =~ s{
91             ^
92             \Q$name\E :
93             \s*
94             ( [^<\n]*? )
95             \s+
96             < ( [^>\n]*? ) >
97             \s*
98             $
99             }{}xmsi
100 6 100       10 || ${$msgstr_ref} =~ s{
  5         67  
101             ^
102             \Q$name\E :
103             \s*
104             ( [^\n]*? )
105             ()
106             \s*
107             $
108             }{}xmsi;
109 6         177 $self->mail_name($1); ## no critic (CaptureWithoutTest)
110 6         160 $self->mail_address($2); ## no critic (CaptureWithoutTest)
111              
112 6         17 return;
113             };
114              
115             sub lines {
116 9     9 0 15 my $self = shift;
117              
118 9 50 66     239 if ( ! length $self->mail_name && ! length $self->mail_address ) {
119 4         10 return;
120             }
121 5         123 my $line = $self->format_line(
122             '{name}: {mail_name} <{mail_address}>',
123             name => $self->name,
124             mail_name => $self->mail_name,
125             mail_address => $self->mail_address,
126             );
127 5         18 $line =~ s{\s* <> \z}{}xms; # delete an empty mail address
128 5         22 $line =~ s{\s+}{ }xmsg; # delete space before a mail address
129              
130 5         15 return $line;
131             }
132              
133             __PACKAGE__->meta->make_immutable;
134              
135             # $Id: Utils.pm 602 2011-11-13 13:49:23Z steffenw $
136              
137             1;