File Coverage

blib/lib/Locale/TextDomain/OO/Plugin/Expand/Gettext/Named.pm
Criterion Covered Total %
statement 40 44 90.9
branch 19 26 73.0
condition 2 3 66.6
subroutine 9 11 81.8
pod 2 2 100.0
total 72 86 83.7


line stmt bran cond sub pod time code
1             package Locale::TextDomain::OO::Plugin::Expand::Gettext::Named; ## no critic (TidyCode)
2            
3 5     5   8161 use strict;
  5         13  
  5         164  
4 5     5   46 use warnings;
  5         12  
  5         168  
5 5     5   54 use Carp qw(cluck confess);
  5         22  
  5         373  
6 5     5   3074 use Hash::Util qw(lock_keys);
  5         14654  
  5         35  
7 5     5   2274 use Locale::Utils::PlaceholderNamed;
  5         23229  
  5         143  
8 5     5   33 use Moo::Role;
  5         12  
  5         29  
9 5     5   2010 use Try::Tiny;
  5         12  
  5         4325  
10            
11             our $VERSION = '1.027';
12            
13             requires qw(
14             translate
15             filter
16             run_filter
17             category
18             domain
19             );
20            
21             has _shadow_domains_named => (
22             is => 'rw',
23             default => sub { [] },
24             );
25            
26             has _shadow_categories_named => (
27             is => 'rw',
28             default => sub { [] },
29             );
30            
31             has expand_gettext_named => (
32             is => 'rw',
33             default => sub {
34             return Locale::Utils::PlaceholderNamed->new;
35             },
36             );
37            
38             my $begin_d = sub {
39             my ($self, $domain) = @_;
40            
41             defined $domain
42             or confess 'Domain is not defined';
43             push
44             @{ $self->_shadow_domains_named },
45             $self->domain;
46             $self->domain($domain);
47            
48             return $self;
49             };
50            
51             my $begin_c = sub {
52             my ($self, $category) = @_;
53            
54             defined $category
55             or confess 'Category is not defined';
56             push
57             @{ $self->_shadow_categories_named },
58             $self->category;
59             $self->category($category);
60            
61             return $self;
62             };
63            
64             my $end_d = sub {
65             my $self = shift;
66            
67             if ( ! @{ $self->_shadow_domains_named } ) {
68             cluck 'Tried to get the domain from stack but no domain is not stored';
69             return $self;
70             }
71             $self->domain( pop @{ $self->_shadow_domains_named } );
72            
73             return $self;
74             };
75            
76             my $end_c = sub {
77             my $self = shift;
78            
79             if ( ! @{ $self->_shadow_categories_named } ) {
80             cluck 'Tried to get the category from stack but no category is stored',
81             return $self;
82             }
83             $self->category( pop @{ $self->_shadow_categories_named } );
84            
85             return $self;
86             };
87            
88             sub locn {
89 21     21 1 7765 my ($self, @args) = @_;
90            
91 21 100       100 my $arg_ref = ref $args[0] eq 'HASH' ? $args[0] : { @args };
92             try {
93 21     21   1030 lock_keys( %{$arg_ref}, qw( category domain context text plural replace ) );
  21         94  
94             exists $arg_ref->{plural}
95 21 100       1333 or return;
96 8         12 lock_keys( %{ $arg_ref->{plural} }, qw( singular plural count ) );
  8         29  
97             }
98             catch {
99 0     0   0 confess $_;
100 21         157 };
101            
102             $arg_ref->{domain}
103 21 100       680 and $self->$begin_d( $arg_ref->{domain} );
104             $arg_ref->{category}
105 21 100       69 and $self->$begin_c( $arg_ref->{category} );
106             my $translation
107             = exists $arg_ref->{text}
108             ? $self->translate(
109 13         62 @{$arg_ref}{ qw( context text ) },
110             )
111             : exists $arg_ref->{plural}
112             ? $self->translate(
113             $arg_ref->{context},
114 21 50       77 @{ $arg_ref->{plural} }{ qw( singular plural count ) },
  8 100       34  
115             1,
116             )
117             : q{};
118             $arg_ref->{replace}
119             and $translation = $self->expand_gettext_named->expand_named(
120             $translation,
121             $arg_ref->{replace},
122 21 100       92 );
123 21 50       951 $self->filter
124             and $self->run_filter(\$translation);
125 21 50 66     216 if ( exists $arg_ref->{text} || exists $arg_ref->{plural} ) {
126             $arg_ref->{domain}
127 21 100       59 and $self->$end_d;
128             $arg_ref->{category}
129 21 100       61 and $self->$end_c;
130             }
131            
132 21         175 return $translation;
133             }
134            
135             sub Nlocn { ## no critic (Capitalization)
136 0     0 1   my (undef, @args) = @_;
137            
138 0 0         my $arg_ref = ref $args[0] eq 'HASH' ? $args[0] : { @args };
139            
140 0 0         return wantarray ? @args : $args[0];
141             }
142            
143             1;
144            
145             __END__