| 1 | package Locale::TextDomain::OO::Lexicon::Role::StoreFilter; ## no critic (TidyCode) | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 2 | ▶ | use Carp qw(confess); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | 2 | ▶ | use Clone qw(clone); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | 2 | ▶ | use Locale::TextDomain::OO::Singleton::Lexicon; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 2 | ▶ | use Locale::TextDomain::OO::Util::JoinSplitLexiconKeys; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 2 | ▶ | use Moo::Role; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | 2 | ▶ | use MooX::Types::MooseLike::Base qw(HashRef); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | 2 | ▶ | use namespace::autoclean; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | 2 | our $VERSION = '1.017'; | |||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | 2 | for my $name ( qw( language category domain project ) ) { | |||||||||||||||||
| 16 | has "filter_$name" => ( | ||||||||||||||||||
| 17 | is => 'rw', | ||||||||||||||||||
| 18 | 68 | ▶ | isa => sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||
| 19 | 68 | my $value = shift; | |||||||||||||||||
| 20 | 68 | ▶ | defined $value | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 21 | or return; | ||||||||||||||||||
| 22 | 56 | my $ref = ref $value; | |||||||||||||||||
| 23 | 56 | ▶ | $ref eq 'Regexp' | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 24 | and return; | ||||||||||||||||||
| 25 | 55 | ▶ | $ref eq 'CODE' | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 26 | and return; | ||||||||||||||||||
| 27 | 54 | ▶ | $ref | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 28 | and confess 'Undef, Str, RegexpRef or CodeRef expected'; | ||||||||||||||||||
| 29 | }, | ||||||||||||||||||
| 30 | 8 | ); | |||||||||||||||||
| 31 | } | ||||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | 3 | ▶ | sub clear_filter { | ||||||||||||||||
|
Subroutine
clear_filter: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 34 | 3 | my $self = shift; | |||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | 3 | $self->filter_language(undef); | |||||||||||||||||
| 37 | 3 | $self->filter_category(undef); | |||||||||||||||||
| 38 | 3 | $self->filter_domain(undef); | |||||||||||||||||
| 39 | 3 | $self->filter_project(undef); | |||||||||||||||||
| 40 | |||||||||||||||||||
| 41 | 3 | return $self; | |||||||||||||||||
| 42 | }; | ||||||||||||||||||
| 43 | |||||||||||||||||||
| 44 | has data => ( | ||||||||||||||||||
| 45 | is => 'ro', | ||||||||||||||||||
| 46 | isa => HashRef, | ||||||||||||||||||
| 47 | lazy => 1, | ||||||||||||||||||
| 48 | 27 | ▶ | default => sub { {} }, | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 49 | 2 | ); | |||||||||||||||||
| 50 | |||||||||||||||||||
| 51 | 327 | ▶ | my $is_expected_lexicon_key = sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||
| 52 | 327 | my ( $self, $lexicon_key ) = @_; | |||||||||||||||||
| 53 | |||||||||||||||||||
| 54 | 327 | my $key_ref = Locale::TextDomain::OO::Util::JoinSplitLexiconKeys | |||||||||||||||||
| 55 | ->instance | ||||||||||||||||||
| 56 | ->split_lexicon_key($lexicon_key); | ||||||||||||||||||
| 57 | NAME: | ||||||||||||||||||
| 58 | 327 | for my $name ( qw( language category domain project ) ) { | |||||||||||||||||
| 59 | defined $key_ref->{$name} | ||||||||||||||||||
| 60 | 783 | ▶ | or $key_ref->{$name} = q{}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 61 | 783 | my $method = "filter_$name"; | |||||||||||||||||
| 62 | 783 | my $filter = $self->$method; | |||||||||||||||||
| 63 | 783 | ▶ | if ( defined $filter ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 64 | 398 | local $_ = $key_ref->{$name}; | |||||||||||||||||
| 65 | 398 | my $ref = ref $filter; | |||||||||||||||||
| 66 | 398 | ▶ | $ref eq 'Regexp' | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 67 | ? $_ =~ $filter | ||||||||||||||||||
| 68 | : $ref eq 'CODE' | ||||||||||||||||||
| 69 | ? $filter->($method) | ||||||||||||||||||
| 70 | : $_ eq $filter | ||||||||||||||||||
| 71 | or return; | ||||||||||||||||||
| 72 | } | ||||||||||||||||||
| 73 | } | ||||||||||||||||||
| 74 | |||||||||||||||||||
| 75 | 95 | return 1; | |||||||||||||||||
| 76 | 2 | }; | |||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | 92 | ▶ | my $prepare_lexicon = sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||
| 79 | 92 | my ( $self, $lexicon_ref ) = @_; | |||||||||||||||||
| 80 | |||||||||||||||||||
| 81 | 92 | $lexicon_ref = clone($lexicon_ref); | |||||||||||||||||
| 82 | |||||||||||||||||||
| 83 | # not able to serialize code references | ||||||||||||||||||
| 84 | 92 | delete $lexicon_ref->{ q{} }->{plural_code}; | |||||||||||||||||
| 85 | |||||||||||||||||||
| 86 | 92 | my $key_util = Locale::TextDomain::OO::Util::JoinSplitLexiconKeys->instance; | |||||||||||||||||
| 87 | MESSAGE_KEY: | ||||||||||||||||||
| 88 | 92 | for my $message_key ( keys %{$lexicon_ref} ) { | |||||||||||||||||
| 89 | 179 | ▶ | length $message_key | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 90 | or next MESSAGE_KEY; | ||||||||||||||||||
| 91 | 87 | my $new_message_key = $key_util->join_message_key( | |||||||||||||||||
| 92 | $key_util->split_message_key($message_key), | ||||||||||||||||||
| 93 | 'JSON', | ||||||||||||||||||
| 94 | ); | ||||||||||||||||||
| 95 | 87 | $lexicon_ref->{$new_message_key} = delete $lexicon_ref->{$message_key}; | |||||||||||||||||
| 96 | } | ||||||||||||||||||
| 97 | |||||||||||||||||||
| 98 | 92 | return $lexicon_ref; | |||||||||||||||||
| 99 | 2 | }; | |||||||||||||||||
| 100 | |||||||||||||||||||
| 101 | 29 | ▶ | sub copy { | ||||||||||||||||
|
Subroutine
copy: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||
| 102 | 29 | my $self = shift; | |||||||||||||||||
| 103 | |||||||||||||||||||
| 104 | 29 | my $data = Locale::TextDomain::OO::Singleton::Lexicon->instance->data; | |||||||||||||||||
| 105 | 29 | for my $lexicon_key ( keys %{$data} ) { | |||||||||||||||||
| 106 | $self->$is_expected_lexicon_key($lexicon_key) | ||||||||||||||||||
| 107 | and $self->data->{$lexicon_key} | ||||||||||||||||||
| 108 | 319 | ▶ | = $self->$prepare_lexicon( $data->{$lexicon_key} ); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 109 | } | ||||||||||||||||||
| 110 | |||||||||||||||||||
| 111 | 29 | return $self; | |||||||||||||||||
| 112 | } | ||||||||||||||||||
| 113 | |||||||||||||||||||
| 114 | 1 | ▶ | sub remove { | ||||||||||||||||
|
Subroutine
remove: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||
| 115 | 1 | my $self = shift; | |||||||||||||||||
| 116 | |||||||||||||||||||
| 117 | 1 | my $data = $self->data; | |||||||||||||||||
| 118 | 1 | for my $lexicon_key ( keys %{$data} ) { | |||||||||||||||||
| 119 | $self->$is_expected_lexicon_key($lexicon_key) | ||||||||||||||||||
| 120 | 8 | ▶ | and delete $data->{$lexicon_key}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 121 | } | ||||||||||||||||||
| 122 | |||||||||||||||||||
| 123 | 1 | return $self; | |||||||||||||||||
| 124 | } | ||||||||||||||||||
| 125 | |||||||||||||||||||
| 126 | 2 | 1; | |||||||||||||||||
| 127 | |||||||||||||||||||
| 128 | __END__ | ||||||||||||||||||