File Coverage

blib/lib/Data/Localize/MultiLevel.pm
Criterion Covered Total %
statement 32 46 69.5
branch 6 18 33.3
condition 1 3 33.3
subroutine 8 9 88.8
pod 1 1 100.0
total 48 77 62.3


line stmt bran cond sub pod time code
1             package Data::Localize::MultiLevel;
2 1     1   479 use Moo;
  1         2  
  1         6  
3 1     1   243 use Config::Any;
  1         1  
  1         23  
4 1     1   3 use Data::Localize;
  1         1  
  1         22  
5 1     1   517 use MooX::Types::MooseLike::Base qw(ArrayRef);
  1         4875  
  1         100  
6             BEGIN {
7 1     1   699 if (Data::Localize::DEBUG) {
8             require Data::Localize::Log;
9             Data::Localize::Log->import;
10             }
11             }
12              
13             extends 'Data::Localize::Localizer';
14             with 'Data::Localize::Trait::WithStorage';
15              
16             has paths => (
17             is => 'ro',
18             isa => ArrayRef,
19             trigger => sub {
20             my $self = shift;
21             if ($self->initialized) {
22             $self->load_from_path($_) for @{$_[0]};
23             }
24             },
25             );
26              
27             after BUILD => sub {
28             my $self = shift;
29             my $paths = $self->paths;
30             foreach my $path (@$paths) {
31             $self->load_from_path($path);
32             }
33             };
34              
35             after register => sub {
36             my ($self, $loc) = @_;
37             $loc->add_localizer_map('*', $self);
38             $loc->add_localizer_map( $_, $self )
39             for keys %{ $self->lexicon_map }
40             };
41              
42             around get_lexicon => sub{
43             my ($next, $self, $lang, $key) = @_;
44              
45             my ($storage_key, @key_path) = split /\./, $key;
46             my $lexicon = $self->$next($lang, $storage_key);
47              
48             return _rfetch( $lexicon, 0, \@key_path )
49             if @key_path;
50              
51             return $lexicon;
52             };
53              
54             around set_lexicon => sub {
55             my ($next, $self, $lang, $key, $value) = @_;
56              
57             my ($storage_key, @key_path) = split /\./, $key;
58              
59             if ( @key_path ) {
60             my $lexicon = $self->get_lexicon($lang, $storage_key);
61             _rstore( $lexicon, 0, \@key_path, $value );
62             $self->$next( $storage_key, $lexicon );
63             }
64             else {
65             $self->$next( $storage_key, $value );
66             }
67              
68             return;
69             };
70              
71             sub _build_formatter {
72 1     1   860 Module::Load::load('Data::Localize::Format::NamedArgs');
73 1         13 return Data::Localize::Format::NamedArgs->new();
74             }
75              
76             sub load_from_path {
77 1     1 1 2 my ($self, $path) = @_;
78              
79 1         84 my @files = glob( $path );
80 1         15 my $cfg = Config::Any->load_files({ files => \@files, use_ext => 1 });
81              
82 1         10492 foreach my $x (@$cfg) {
83 2         522 my ($filename, $lexicons) = %$x;
84             # should have one root item
85 2         5 my ($lang) = keys %$lexicons;
86              
87 2         3 if (Data::Localize::DEBUG) {
88             debugf("load_from_path - Loaded %s for languages %s",
89             Scalar::Util::blessed($self),
90             $filename,
91             $lang,
92             );
93             }
94              
95 2         9 $self->merge_lexicon($lang, $lexicons->{$lang});
96 2 50       31 $self->_localizer->add_localizer_map($lang, $self) if $self->_localizer;
97             }
98             }
99              
100             sub _rfetch {
101 14     14   16 my ($lexicon, $i, $keys) = @_;
102              
103 14 100       49 return unless $lexicon;
104              
105 8         16 my $thing = $lexicon->{$keys->[$i]};
106 8 50       13 return unless defined $thing;
107              
108 8         9 my $ref = ref $thing;
109 8 50 33     40 return unless $ref || length $thing;
110              
111 8 50       18 if (@$keys <= $i + 1) {
112 8         31 return $thing;
113             }
114              
115 0 0         if ($ref ne 'HASH') {
116 0           if (Data::Localize::DEBUG) {
117             debugf("%s does not point to a hash",
118             join('.', map { $keys->[$_] } 0..$i)
119             );
120             }
121 0           return ();
122             }
123              
124 0           return _rfetch( $thing, $i + 1, $keys )
125             }
126              
127             sub _rstore {
128 0     0     my ($lexicon, $i, $keys, $value) = @_;
129              
130 0 0         return unless $lexicon;
131              
132 0 0         if (@$keys <= $i + 1) {
133 0           $lexicon->{ $keys->[$i] } = $value;
134 0           return;
135             }
136              
137 0           my $thing = $lexicon->{$keys->[$i]};
138              
139 0 0         if (ref $thing ne 'HASH') {
140 0           if (Data::Localize::DEBUG) {
141             debugf("%s does not point to a hash",
142             join('.', map { $keys->[$_] } 0..$i)
143             );
144             }
145 0           return ();
146             }
147              
148 0           return _rstore( $thing, $i + 1, $keys, $value );
149             }
150              
151             1;
152              
153             __END__