File Coverage

blib/lib/Locale/TextDomain/OO/Util/JoinSplitLexiconKeys.pm
Criterion Covered Total %
statement 45 45 100.0
branch 26 26 100.0
condition 2 3 66.6
subroutine 12 12 100.0
pod 7 7 100.0
total 92 93 98.9


line stmt bran cond sub pod time code
1             package Locale::TextDomain::OO::Util::JoinSplitLexiconKeys; ## no critic (TidyCode)
2            
3 2     2   42205 use strict;
  2         16  
  2         67  
4 2     2   13 use warnings;
  2         7  
  2         69  
5 2     2   534 use Locale::TextDomain::OO::Util::Constants;
  2         7  
  2         57  
6 2     2   12 use namespace::autoclean;
  2         7  
  2         28  
7            
8             our $VERSION = '3.004';
9            
10             sub instance {
11 1     1 1 16 return __PACKAGE__;
12             }
13            
14             sub join_lexicon_key {
15 2     2 1 13 my ( undef, $arg_ref ) = @_;
16            
17 2         12 my $const = Locale::TextDomain::OO::Util::Constants->instance;
18            
19             return join $const->lexicon_key_separator,
20             (
21             ( defined $arg_ref->{language} && length $arg_ref->{language} )
22             ? $arg_ref->{language}
23             : 'i-default'
24             ),
25             ( defined $arg_ref->{category} ? $arg_ref->{category} : q{} ),
26             ( defined $arg_ref->{domain} ? $arg_ref->{domain} : q{} ),
27 2 100 66     9 ( defined $arg_ref->{project} ? $arg_ref->{project} : () );
    100          
    100          
    100          
28             }
29            
30             sub split_lexicon_key {
31 3     3 1 10 my ( undef, $lexicon_key ) = @_;
32            
33 3 100       17 defined $lexicon_key
34             or return {};
35 2         10 my $const = Locale::TextDomain::OO::Util::Constants->instance;
36 2         9 my ( $language, $category, $domain, $project )
37             = split $const->lexicon_key_separator, $lexicon_key, 4; ## no critic (Magic Numbers)
38            
39             return {(
40 2 100       31 language => $language,
41             category => $category,
42             domain => $domain,
43             ( defined $project ? ( project => $project ) : () ),
44             )};
45             }
46            
47             my $length_or_empty_list = sub {
48             my $item = shift;
49            
50             defined $item or return;
51             length $item or return;
52            
53             return $item;
54             };
55            
56             sub join_message_key {
57 8     8 1 2559 my ( undef, $arg_ref, $format ) = @_;
58            
59 8         34 my $const = Locale::TextDomain::OO::Util::Constants->instance;
60            
61             return join $const->msg_key_separator($format),
62             (
63             join $const->plural_separator($format),
64             $length_or_empty_list->( $arg_ref->{msgid} ),
65             $length_or_empty_list->( $arg_ref->{msgid_plural} ),
66             ),
67 8         29 $length_or_empty_list->( $arg_ref->{msgctxt} );
68             }
69            
70             sub split_message_key {
71 8     8 1 22 my ( undef, $message_key, $format ) = @_;
72            
73 8 100       30 defined $message_key
74             or return {};
75 6         26 my $const = Locale::TextDomain::OO::Util::Constants->instance;
76 6         22 my ( $text, $context )
77             = split $const->msg_key_separator($format), $message_key;
78 6 100       40 defined $text
79             or $text = q{};
80 6         25 my ( $singular, $plural )
81             = split $const->plural_separator($format), $text;
82 6 100       24 defined $singular
83             or $singular = q{};
84 6 100   12   28 my $list_if_defined = sub { return defined shift() ? @_ : () };
  12         118  
85            
86             return {(
87 6         21 msgid => $singular,
88             $list_if_defined->( $context, msgctxt => $context ),
89             $list_if_defined->( $plural, msgid_plural => $plural ),
90             )};
91             }
92            
93             sub join_message {
94 4     4 1 2933 my ( $self, $message_key, $message_value_ref, $format ) = @_;
95            
96 4 100       16 defined $message_key
97             or $message_key = q{};
98 4 100       15 ref $message_value_ref eq 'HASH'
99             or $message_value_ref = {};
100            
101             return {(
102 4         14 %{$message_value_ref},
103 4         9 %{ $self->split_message_key($message_key, $format) },
  4         11  
104             )};
105             }
106            
107             sub split_message {
108 4     4 1 15 my ( $self, $message, $format ) = @_;
109            
110 4 100       17 ref $message eq 'HASH'
111             or $message = {};
112            
113             my $message_key = $self->join_message_key(
114             {(
115             map {
116 4         11 $_ => delete $message->{$_};
  12         52  
117             }
118             qw( msgctxt msgid msgid_plural )
119             )},
120             $format,
121             );
122            
123 4         38 return $message_key, $message;
124             }
125            
126             1;
127            
128             __END__