File Coverage

blib/lib/Perinci/Sub/Convert/default_lang.pm
Criterion Covered Total %
statement 44 44 100.0
branch 15 22 68.1
condition 11 14 78.5
subroutine 5 5 100.0
pod 1 1 100.0
total 76 86 88.3


line stmt bran cond sub pod time code
1             package Perinci::Sub::Convert::default_lang;
2              
3 1     1   847 use 5.010001;
  1         4  
  1         37  
4 1     1   6 use strict;
  1         1  
  1         29  
5 1     1   5 use warnings;
  1         2  
  1         25  
6              
7 1     1   5 use Exporter qw(import);
  1         1  
  1         492  
8             our @EXPORT_OK = qw(convert_property_default_lang);
9              
10             our $VERSION = '0.01'; # VERSION
11             our $DATE = '2014-04-30'; # DATE
12              
13             our %SPEC;
14              
15             $SPEC{convert_property_default_lang} = {
16             v => 1.1,
17             summary => 'Convert default_lang property in Rinci function metadata',
18             args => {
19             meta => {
20             schema => 'hash*', # XXX defhash
21             req => 1,
22             pos => 0,
23             },
24             new => {
25             summary => 'New value',
26             schema => ['str*'],
27             req => 1,
28             pos => 1,
29             },
30             },
31             result_naked => 1,
32             };
33             sub convert_property_default_lang {
34 1     1 1 69 my %args = @_;
35              
36 1 50       7 my $meta = $args{meta} or die "Please specify meta";
37 1 50       6 my $new = $args{new} or die "Please specify new";
38              
39             # collect defhashes
40 1         4 my @dh = ($meta);
41 1 50       8 push @dh, @{ $meta->{links} } if $meta->{links};
  1         4  
42 1 50       6 push @dh, @{ $meta->{examples} } if $meta->{examples};
  1         2  
43 1 50       6 push @dh, $meta->{result} if $meta->{result};
44 1 50       6 push @dh, values %{ $meta->{args} } if $meta->{args};
  1         5  
45 1         3 push @dh, grep {ref($_) eq 'HASH'} @{ $meta->{tags} };
  2         10  
  1         3  
46              
47 1         3 my $i = 0;
48 1         3 for my $dh (@dh) {
49 8         9 $i++;
50 8   100     33 my $old = $dh->{default_lang} // "en_US";
51 8 50 66     24 return if $old eq $new && $i == 1;
52 8         14 $dh->{default_lang} = $new;
53 8         11 for my $prop (qw/summary description/) {
54 16         30 my $propold = "$prop.alt.lang.$old";
55 16         24 my $propnew = "$prop.alt.lang.$new";
56 16 100 100     104 next unless defined($dh->{$prop}) ||
      100        
57             defined($dh->{$propold}) || defined($dh->{$propnew});
58 6 100       16 if (defined $dh->{$prop}) {
59 4   33     20 $dh->{$propold} //= $dh->{$prop};
60             }
61 6 100       16 if (defined $dh->{$propnew}) {
62 3         6 $dh->{$prop} = $dh->{$propnew};
63             } else {
64 3         5 delete $dh->{$prop};
65             }
66 6 100       21 if (defined $dh->{$propnew}) {
67 3         10 delete $dh->{$propnew};
68             }
69             }
70             }
71 1         6 $meta;
72             }
73              
74             1;
75             # ABSTRACT: Convert default_lang property value in Rinci function metadata
76              
77             __END__