line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Localize::Trait::WithStorage; |
2
|
1
|
|
|
1
|
|
852
|
use Moo::Role; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
7
|
|
3
|
1
|
|
|
1
|
|
338
|
use Data::Localize; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
43
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
BEGIN { |
6
|
1
|
|
|
1
|
|
915
|
if (Data::Localize::DEBUG) { |
7
|
|
|
|
|
|
|
require Data::Localize::Log; |
8
|
|
|
|
|
|
|
Data::Localize::Log->import; |
9
|
|
|
|
|
|
|
} |
10
|
|
|
|
|
|
|
} |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has storage_class => ( |
13
|
|
|
|
|
|
|
is => 'ro', |
14
|
|
|
|
|
|
|
default => sub { |
15
|
|
|
|
|
|
|
return '+Data::Localize::Storage::Hash'; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
has storage_args => ( |
20
|
|
|
|
|
|
|
is => 'ro', |
21
|
|
|
|
|
|
|
default => sub { +{} } |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
has 'load_from_storage' => ( |
25
|
|
|
|
|
|
|
is => 'ro', |
26
|
|
|
|
|
|
|
default => sub { [] }, |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
has lexicon_map => ( |
30
|
|
|
|
|
|
|
is => 'ro', |
31
|
|
|
|
|
|
|
default => sub { +{} }, |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
after BUILD => sub { |
35
|
|
|
|
|
|
|
my $self = shift; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
my $langs = $self->load_from_storage; |
38
|
|
|
|
|
|
|
if (! $langs || ! @$langs) { |
39
|
|
|
|
|
|
|
if (Data::Localize::DEBUG) { |
40
|
|
|
|
|
|
|
debugf("No languages to load"); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
return; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
my $storage_class = $self->_canonicalize_storage_class; |
45
|
|
|
|
|
|
|
my $storage_args = $self->storage_args; |
46
|
|
|
|
|
|
|
if (Data::Localize::DEBUG) { |
47
|
|
|
|
|
|
|
debugf("Building lexicon map (%s)", $storage_class); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
Module::Load::load( $storage_class ); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
unless ( $storage_class->is_volatile ) { |
53
|
|
|
|
|
|
|
foreach my $lang ( @$langs ) { |
54
|
|
|
|
|
|
|
if (Data::Localize::DEBUG) { |
55
|
|
|
|
|
|
|
debugf("Loading storage for lang '%s'", $lang); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
$storage_args->{lang} = $lang; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
$self->set_lexicon_map( |
60
|
|
|
|
|
|
|
$lang, |
61
|
|
|
|
|
|
|
$storage_class->new( $storage_args ) |
62
|
|
|
|
|
|
|
); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
}; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub get_lexicon_map { |
68
|
11
|
|
|
11
|
1
|
17
|
my ($self, $key) = @_; |
69
|
11
|
|
|
|
|
33
|
return $self->lexicon_map->{ $key }; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub set_lexicon_map { |
73
|
3
|
|
|
3
|
1
|
6
|
my ($self, $key, $value) = @_; |
74
|
3
|
|
|
|
|
12
|
return $self->lexicon_map->{ $key } = $value; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub get_lexicon { |
78
|
7
|
|
|
7
|
1
|
13
|
my ($self, $lang, $id) = @_; |
79
|
7
|
|
|
|
|
14
|
my $lexicon = $self->get_lexicon_map($lang); |
80
|
7
|
50
|
|
|
|
20
|
return () unless $lexicon; |
81
|
7
|
|
|
|
|
22
|
$lexicon->get($id); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub set_lexicon { |
85
|
0
|
|
|
0
|
1
|
0
|
my ($self, $lang, $id, $value) = @_; |
86
|
0
|
|
|
|
|
0
|
my $lexicon = $self->get_lexicon_map($lang); |
87
|
0
|
0
|
|
|
|
0
|
if (! $lexicon) { |
88
|
0
|
|
|
|
|
0
|
$lexicon = $self->_build_storage($lang); |
89
|
0
|
|
|
|
|
0
|
$self->set_lexicon_map($lang, $lexicon); |
90
|
|
|
|
|
|
|
} |
91
|
0
|
|
|
|
|
0
|
$lexicon->set($id, $value); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub merge_lexicon { |
95
|
4
|
|
|
4
|
1
|
9
|
my ($self, $lang, $new_lexicon) = @_; |
96
|
|
|
|
|
|
|
|
97
|
4
|
|
|
|
|
6
|
if (Data::Localize::DEBUG) { |
98
|
|
|
|
|
|
|
debugf("Merging lexicon for lang '%s'", $lang); |
99
|
|
|
|
|
|
|
} |
100
|
4
|
|
|
|
|
12
|
my $lexicon = $self->get_lexicon_map($lang); |
101
|
4
|
100
|
|
|
|
11
|
if (! $lexicon) { |
102
|
3
|
|
|
|
|
11
|
$lexicon = $self->_build_storage($lang); |
103
|
3
|
|
|
|
|
1062
|
$self->set_lexicon_map($lang, $lexicon); |
104
|
|
|
|
|
|
|
} |
105
|
4
|
|
|
|
|
26
|
while (my ($key, $value) = each %$new_lexicon) { |
106
|
13
|
|
|
|
|
14
|
if (Data::Localize::DEBUG) { |
107
|
|
|
|
|
|
|
debugf("Setting lexicon '%s' on '%s'", $key, Scalar::Util::blessed $lexicon); |
108
|
|
|
|
|
|
|
} |
109
|
13
|
|
|
|
|
39
|
$lexicon->set($key, $value); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub _build_storage { |
114
|
3
|
|
|
3
|
|
5
|
my ($self, $lang) = @_; |
115
|
|
|
|
|
|
|
|
116
|
3
|
|
|
|
|
10
|
my $class = $self->_canonicalize_storage_class; |
117
|
3
|
|
|
|
|
10
|
my $args = $self->storage_args; |
118
|
|
|
|
|
|
|
|
119
|
3
|
|
|
|
|
16
|
Module::Load::load($class); |
120
|
|
|
|
|
|
|
|
121
|
3
|
|
|
|
|
229
|
$args->{lang} = $lang; |
122
|
|
|
|
|
|
|
|
123
|
3
|
|
|
|
|
5
|
if (Data::Localize::DEBUG) { |
124
|
|
|
|
|
|
|
debugf("Creating storage '%s'", $class); |
125
|
|
|
|
|
|
|
} |
126
|
3
|
|
|
|
|
74
|
return $class->new( $args ); |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub _canonicalize_storage_class { |
130
|
3
|
|
|
3
|
|
6
|
my $self = shift; |
131
|
3
|
|
|
|
|
8
|
my $class = $self->storage_class; |
132
|
3
|
50
|
|
|
|
18
|
if ($class !~ s/^\+//) { |
133
|
0
|
|
|
|
|
0
|
$class = "Data::Localize::Storage::$class"; |
134
|
|
|
|
|
|
|
} |
135
|
3
|
|
|
|
|
8
|
$class; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
1
|
|
|
1
|
|
6
|
no Moo::Role; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
1; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
__END__ |