File Coverage

blib/lib/Locale/Utils/PlaceholderNamed.pm
Criterion Covered Total %
statement 37 37 100.0
branch 13 16 81.2
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 60 63 95.2


line stmt bran cond sub pod time code
1             package Locale::Utils::PlaceholderNamed; ## no critic (TidyCode)
2            
3 4     4   148624 use strict;
  4         313  
  4         405  
4 4     4   14 use warnings;
  4         5  
  4         84  
5 4     4   11 use Carp qw(confess);
  4         7  
  4         141  
6 4     4   1790 use Moo;
  4         36726  
  4         14  
7 4     4   5626 use MooX::StrictConstructor;
  4         36553  
  4         13  
8 4     4   58541 use MooX::Types::MooseLike::Base qw(Bool CodeRef);
  4         16758  
  4         236  
9 4     4   1662 use namespace::autoclean;
  4         37060  
  4         10  
10            
11             our $VERSION = '1.002';
12            
13             has is_strict => (
14             is => 'rw',
15             isa => Bool,
16             );
17            
18             has modifier_code => (
19             is => 'rw',
20             isa => CodeRef,
21             clearer => 'clear_modifier_code',
22             );
23            
24             sub _mangle_value {
25 17     17   28 my ($self, $placeholder, $value, $attribute) = @_;
26            
27 17 100       53 defined $value
    100          
28             or return $self->is_strict ? $placeholder : q{};
29 15 100       59 defined $attribute
30             or return $value;
31 2 50       17 $self->modifier_code
32             or return $value;
33 2         410 $value = $self->modifier_code->($value, $attribute);
34 2 50       19 defined $value
35             or confess 'modifier_code returns nothing or undef';
36            
37 2         9 return $value;
38             }
39            
40             sub expand_named {
41 10     10 1 138568 my ($self, $text, @args) = @_;
42            
43 10 100       44 defined $text
44             or return $text;
45 9 50       32 my $arg_ref = @args == 1
    100          
46             ? $args[0]
47             : {
48             @args % 2
49             ? confess 'Arguments expected pairwise'
50             : @args
51             };
52            
53 9         8 my $regex = join q{|}, map { quotemeta } keys %{$arg_ref};
  16         25  
  9         21  
54             ## no critic (EscapedMetacharacters)
55 9         211 $text =~ s{
56             (
57             \{
58             ( $regex )
59             (?: [ ]* [:] ( [^\}]+ ) )?
60             \}
61             )
62             }
63             {
64 17         39 $self->_mangle_value($1, $arg_ref->{$2}, $3)
65             }xmsge;
66             ## use critic (EscapedMetacharacters)
67            
68 9         53 return $text;
69             }
70            
71             __PACKAGE__->meta->make_immutable;
72            
73             1;
74            
75             __END__