Valiant/Translation.pm

filter: Statementstmt 85.8% 79 / 92 Branchbran 59.3% 19 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 4 totaltotal 78.4% 109 / 139 CC 17 SCAR 20.2
CC
17
Cov
79%
human_label_name
12.2
human_attribute_name
9.5
BEGIN
1.0
CRAP 7.6
« DBIx/Class/Valiant/Validator/ResultSet.pm ↑ Summary Valiant/Filterable.pm »
1 package Valiant::Translation;
2  
3 48 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 48 ▶use Text::Autoformat 'autoformat';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 48 ▶use Valiant::Util 'debug';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 48 with 'Valiant::Naming';
8  
9 343 ▶sub i18n_class { 'Valiant::I18N' }
Subroutine i18n_class: called CC=1 SCAR=0.0
Pod undocumented
10  
11 has 'i18n' => (
12 is => 'ro',
13 required => 1,
14 lazy => 1,
15 343 ▶ default => sub { Module::Runtime::use_module(shift->i18n_class) },
Subroutine __ANON__: called CC=1 SCAR=0.0
16 48 );
17  
18 2003 ▶sub i18n_scope { 'valiant' }
Subroutine i18n_scope: called CC=1 SCAR=0.0
Pod undocumented
19  
20 sub human_attribute_name {
21 828 ▶ my ($self, $attribute, $options) = @_;
Subroutine human_attribute_name: called CC=9 SCAR=22.5
Pod undocumented
22 828 ▶ return undef unless defined($attribute);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attribute 0 828 828
23 828 debug 1, "Begin building human name for attribute '$attribute'";
24  
25 # TODO I think we need to clean $option here so I don't need to manually
26 # set count=>1 as I do below.
27 #
28  
29 828 my @defaults = ();
30 828 my $i18n_scope = $self->i18n_scope;
31 828 my @parts = split '.', $attribute;
32 828 my $attribute_name = pop @parts;
33 828 ▶ my $namespace = join '/', @parts if @parts;
Branch: 1 branch
BranchTrueFalseTotal
if @parts 0 828 828
34 828 my $attributes_scope = "${i18n_scope}.attributes";
35  
36 828 ▶ if($self->can('i18n_lookup')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("i18n_lookup")) 827 1 828
37 827 debug 2, "Building defaults for attributes '$attribute'";
38 827 ▶ if($namespace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($namespace) { } 0 827 827
39 @defaults = map {
40 0 my $class = $_;
41 0 "${attributes_scope}.${\$class->model_name->i18n_key}/${namespace}.${attribute}"
42 0 } grep { $_->model_name->can('i18n_key') } $self->i18n_lookup;
43 } else {
44 @defaults = map {
45 1278 my $class = $_;
46 1278 "${attributes_scope}.${\$class->model_name->i18n_key}.${attribute}"
47 827 } grep { $_->model_name->can('i18n_key') } $self->i18n_lookup;
48 }
49 }
50  
51 828 @defaults = map { $self->i18n->make_tag($_) } (@defaults, "attributes.${attribute}");
52  
53 # Not sure if this should move up above the preceeding map...
54  
55 828 ▶ if(exists $options->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"default"}) 402 426 828
56 402 my $default = delete $options->{default};
57 402 ▶ my @default = ref($default) ? @$default : ($default);
Branch: 1 branch
BranchTrueFalseTotal
ref $default ? : 1 401 402
58 402 push @defaults, @default;
59 }
60 # The final default is just our best attempt to make a name out of the actual
61 # attribute name. This is passed as a plain string so we don't actually try
62 # to localize it.
63 828 push @defaults, $self->_humanize_attribute($attribute);
64  
65 828 my $key = shift @defaults;
66 828 $options->{default} = \@defaults;
67 828 ▶ $options->{count} = 1 unless exists $options->{count};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"count"} 640 188 828
68  
69 828 ▶ return my $localized = $self->i18n->translate($key, %{$options||+{}});
Branch: 1 branch
BranchTrueFalseTotal
$options or {} 0 828 828
70 }
71  
72 sub human_label_name {
73 33 ▶ my ($self, $attribute, $options) = @_;
Subroutine human_label_name: called CC=9 SCAR=25.0
Pod undocumented
74 33 ▶ return undef unless defined($attribute);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attribute 0 33 33
75 33 debug 1, "Begin building human name for label '$attribute'";
76  
77 # TODO I think we need to clean $option here so I don't need to manually
78 # set count=>1 as I do below.
79 #
80  
81 33 my @defaults = ();
82 33 my $i18n_scope = $self->i18n_scope;
83 33 my @parts = split '.', $attribute;
84 33 my $attribute_name = pop @parts;
85 33 ▶ my $namespace = join '/', @parts if @parts;
Branch: 1 branch
BranchTrueFalseTotal
if @parts 0 33 33
86 33 my $attributes_scope = "${i18n_scope}.labels";
87  
88 33 ▶ if($self->can('i18n_lookup')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("i18n_lookup")) 33 0 33
89 33 debug 2, "Building defaults for labels '$attribute'";
90 33 ▶ if($namespace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($namespace) { } 0 33 33
91 @defaults = map {
92 0 my $class = $_;
93 0 "${attributes_scope}.${\$class->model_name->i18n_key}/${namespace}.${attribute}"
94 0 } grep { $_->model_name->can('i18n_key') } $self->i18n_lookup;
95 } else {
96 @defaults = map {
97 33 my $class = $_;
98 33 "${attributes_scope}.${\$class->model_name->i18n_key}.${attribute}"
99 33 } grep { $_->model_name->can('i18n_key') } $self->i18n_lookup;
100 }
101 }
102  
103 33 @defaults = map { $self->i18n->make_tag($_) } (@defaults, "labels.${attribute}");
104  
105 # Not sure if this should move up above the preceeding map...
106  
107 33 ▶ if(exists $options->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"default"}) 0 33 33
108 0 my $default = delete $options->{default};
109 0 ▶ my @default = ref($default) ? @$default : ($default);
Branch: 1 branch
BranchTrueFalseTotal
ref $default ? : 0 0 0
110 0 push @defaults, @default;
111 }
112
113 # The final default is just our best attempt to make a name out of the actual
114 # attribute name. This is passed as a plain string so we don't actually try
115 # to localize it.
116 33 push @defaults, $self->_humanize_label($attribute);
117  
118 33 my $key = shift @defaults;
119 33 $options->{default} = \@defaults;
120 33 ▶ $options->{count} = 1 unless exists $options->{count};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"count"} 33 0 33
121  
122 33 ▶ return my $localized = $self->i18n->translate($key, %{$options||+{}});
Branch: 1 branch
BranchTrueFalseTotal
$options or {} 0 33 33
123 }
124  
125 33 ▶sub _humanize_label { shift->_humanize(@_) }
Subroutine _humanize_label: called CC=1 SCAR=0.0
126 828 ▶sub _humanize_attribute { shift->_humanize(@_) }
Subroutine _humanize_attribute: called CC=1 SCAR=0.0
127  
128 sub _humanize {
129 837 ▶ my ($self, $text) = @_;
Subroutine _humanize: called CC=1 SCAR=0.0
130 837 my $humanized = $text;
131
132 837 $humanized =~s/_id$//; # remove trailing _id
133 837 $humanized =~s/_/ /g;
134 837 $humanized = autoformat($humanized, +{case=>'title'});
135 837 $humanized =~s/[\n]//g; # Is this a bug in Text::Autoformat???
136  
137 837 return $humanized;
138 }
139  
140 48 1;
« DBIx/Class/Valiant/Validator/ResultSet.pm ↑ Summary Valiant/Filterable.pm »