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   65904 use strict;
  2         12  
  2         44  
4 2     2   8 use warnings;
  2         4  
  2         40  
5 2     2   664 use Locale::TextDomain::OO::Util::Constants;
  2         7  
  2         57  
6 2     2   13 use namespace::autoclean;
  2         3  
  2         11  
7            
8             our $VERSION = '4.001';
9            
10             sub instance {
11 1     1 1 60 return __PACKAGE__;
12             }
13            
14             sub join_lexicon_key {
15 2     2 1 9 my ( undef, $arg_ref ) = @_;
16            
17 2         7 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     6 ( defined $arg_ref->{project} ? $arg_ref->{project} : () );
    100          
    100          
    100          
28             }
29            
30             sub split_lexicon_key {
31 3     3 1 7 my ( undef, $lexicon_key ) = @_;
32            
33 3 100       15 defined $lexicon_key
34             or return {};
35 2         6 my $const = Locale::TextDomain::OO::Util::Constants->instance;
36 2         6 my ( $language, $category, $domain, $project )
37             = split $const->lexicon_key_separator, $lexicon_key, 4; ## no critic (Magic Numbers)
38            
39             return {(
40 2 100       18 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 1898 my ( undef, $arg_ref, $format ) = @_;
58            
59 8         20 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         19 $length_or_empty_list->( $arg_ref->{msgctxt} );
68             }
69            
70             sub split_message_key {
71 8     8 1 15 my ( undef, $message_key, $format ) = @_;
72            
73 8 100       23 defined $message_key
74             or return {};
75 6         27 my $const = Locale::TextDomain::OO::Util::Constants->instance;
76 6         14 my ( $text, $context )
77             = split $const->msg_key_separator($format), $message_key;
78 6 100       14 defined $text
79             or $text = q{};
80 6         14 my ( $singular, $plural )
81             = split $const->plural_separator($format), $text;
82 6 100       13 defined $singular
83             or $singular = q{};
84 6 100   12   19 my $list_if_defined = sub { return defined shift() ? @_ : () };
  12         74  
85            
86             return {(
87 6         22 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 2076 my ( $self, $message_key, $message_value_ref, $format ) = @_;
95            
96 4 100       9 defined $message_key
97             or $message_key = q{};
98 4 100       10 ref $message_value_ref eq 'HASH'
99             or $message_value_ref = {};
100            
101             return {(
102 4         10 %{$message_value_ref},
103 4         7 %{ $self->split_message_key($message_key, $format) },
  4         7  
104             )};
105             }
106            
107             sub split_message {
108 4     4 1 11 my ( $self, $message, $format ) = @_;
109            
110 4 100       13 ref $message eq 'HASH'
111             or $message = {};
112            
113             my $message_key = $self->join_message_key(
114             {(
115             map {
116 4         7 $_ => delete $message->{$_};
  12         27  
117             }
118             qw( msgctxt msgid msgid_plural )
119             )},
120             $format,
121             );
122            
123 4         24 return $message_key, $message;
124             }
125            
126             1;
127            
128             __END__