| 1 | package Mojolicious::Plugin::TagHelpersI18N; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: TagHelpers with I18N support | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Mojolicious::Plugin::TagHelpers; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Mojo::Collection; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Mojo::Util qw(deprecated xml_escape); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Unicode::Collate; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 3 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 3 | my ($self, $app, $config) = @_; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 3 | my $collate = Unicode::Collate->new; | |||||||||||||||||||||||||||
| 23 | 15 | ▶ | $app->helper( select_field => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 15 | _select_field(@_, collate => $collate ) | |||||||||||||||||||||||||||
| 25 | 3 | } ); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | $app->attr( | ||||||||||||||||||||||||||||
| 28 | 3 | ▶ | translation_method => $config->{method} || 'l' | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{'method'} || 'l'
MC/DC100%1/1
$config->{'method'} || 'l'
$config->{'method'}
Truth table100%2/2
A
A= $config->{'method'}
| |||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 18 | ▶ | sub _prepare { | ||||||||||||||||||||||||||
|
Subroutine
_prepare: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 33 | 18 | my ($self, $options, %attr) = @_; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 18 | my %translated; | |||||||||||||||||||||||||||
| 36 | 18 | my $index = 0; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 18 | ▶ | $options = [$options] if ref $options ne 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 18 | for my $option ( @{$options} ) { | |||||||||||||||||||||||||||
| 41 | 32 | my $ref = ref $option; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 32 | ▶ | if ( !$ref ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 44 | 22 | $option = [$option => $option]; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | elsif ( $ref eq 'HASH') { | ||||||||||||||||||||||||||||
| 47 | 2 | deprecated | |||||||||||||||||||||||||||
| 48 | 'hash references are DEPRECATED in favor of Mojo::Collection objects'; | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 2 | my @values = map{ [ $_ => $option->{$_} ] } sort keys %{$option}; | |||||||||||||||||||||||||||
| 51 | 2 | $option = Mojo::Collection->new( '' => [ @values ] ); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 32 | ▶ | if ( !$attr{no_translation} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 26 | my $method = $self->app->translation_method; | |||||||||||||||||||||||||||
| 56 | 26 | $option->[0] = $self->$method( $option->[0] ); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 32 | push @{ $translated{ $option->[0] } }, $index; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 32 | $index++; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 18 | ▶ | if ( $attr{sort} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 3 | my @sorted = $attr{collate}->sort( keys %translated ); | |||||||||||||||||||||||||||
| 66 | 3 | my @sorted_options = map{ @{$options}[ @{ $translated{$_} } ] }@sorted; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 3 | return \@sorted_options; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 15 | return $options; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 15 | ▶ | sub _select_field { | ||||||||||||||||||||||||||
|
Subroutine
_select_field: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 75 | 15 | my ($self, $name, $options, %attrs) = (shift, shift, shift, @_); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 15 | my %values = map { $_ => 1 } @{ $self->every_param($name) }; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 15 | my %opts; | |||||||||||||||||||||||||||
| 80 | 15 | my @fields = qw(no_translation sort collate); | |||||||||||||||||||||||||||
| 81 | 15 | @opts{@fields} = delete @attrs{@fields}; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 15 | $options = _prepare( $self, $options, %opts ); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 15 | my $groups = ''; | |||||||||||||||||||||||||||
| 86 | 15 | for my $group (@$options) { | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # "optgroup" tag | ||||||||||||||||||||||||||||
| 89 | 26 | ▶ | if (blessed $group && $group->isa('Mojo::Collection')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($group) and $group->isa("Mojo::Collection")
MC/DC100%2/2
builtin::blessed($group) and $group->isa("Mojo::Collection")
builtin::blessed($group)
$group->isa("Mojo::Collection")
Truth table100%3/3
A and B
A= builtin::blessed($group)B= $group->isa("Mojo::Collection")
| |||||||||||||||||||||||||||||
| 90 | 3 | my ($label, $values) = splice @$group, 0, 2; | |||||||||||||||||||||||||||
| 91 | 3 | my $prepared = _prepare( $self, $values, %opts ); | |||||||||||||||||||||||||||
| 92 | 3 | my $content = join '', map { Mojolicious::Plugin::TagHelpers::_option(\%values, $_) } @$prepared; | |||||||||||||||||||||||||||
| 93 | 3 | ▶ | $groups .= Mojolicious::Plugin::TagHelpers::_tag('optgroup', label => $label, @$group, sub {$content}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # "option" tag | ||||||||||||||||||||||||||||
| 97 | 23 | else { $groups .= Mojolicious::Plugin::TagHelpers::_option(\%values, $group) } | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | return Mojolicious::Plugin::TagHelpers::_validation( | ||||||||||||||||||||||||||||
| 101 | 15 | ▶ | $self, $name, 'select', %attrs, name => $name, sub {$groups}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 102 | 15 | ); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 3 | 1; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | __END__ | ||||||||||||||||||||||||||||