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