Valiant/Naming.pm

filter: Statementstmt 89.8% 80 / 89 Branchbran 59.0% 13 / 22 Conditioncond 44.4% 8 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 89.4% 17 / 19 Podpod 20.0% 1 / 5 totaltotal 72.1% 119 / 165 CC 18 SCAR 18.7
CC
18
Cov
78%
human
15.2
__ANON__
6.0
prepare_model_name_args
3.2
CRAP 6.5
« Valiant/Validator/With.pm ↑ Summary Valiant/HTML/FormBuilder/RadioButton.pm »
1 package Valiant::Name;
2  
3 43 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 43 ▶use String::CamelCase 'decamelize';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 43 ▶use Text::Autoformat 'autoformat';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 43 ▶use Lingua::EN::Inflexion 'noun';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 43 ▶use Valiant::I18N ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # These first few are permitted arguments
10  
11 43 has class => (is=>'ro', required=>1);
12 43 has namespace => (is=>'ro', required=>0, predicate=>'has_namespace');
13  
14 # Ok so when you have a namespace that means we want to localize the naming to under
15 # the namespace. For example if you model is package MyApp::Model::Person you might
16 # like to have the namespace set to 'MyApp::Model'. Right this code just uses it for
17 # determining the 'param_key' which is what we look for in a request to find this models
18 # data but we might need it for other stuff later. Rails uses it for tons of stuff due
19 # to their 'convention over configuration' thing.
20  
21 has 'unnamespaced' => (
22 is => 'ro',
23 init_arg => undef,
24 required => 0,
25 lazy => 1,
26 predicate => 'has_unnamespaced',
27 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
28 0 my $self = shift;
29 0 ▶ return unless $self->has_namespace;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_namespace 0 0 0
30 0 my $class = $self->class;
31 0 my $ns = $self->namespace;
32 0 $class =~s/^${ns}:://;
33 0 return $class;
34 },
35 43 );
36  
37 has 'singular' => (
38 is => 'ro',
39 init_arg => undef,
40 lazy => 1,
41 required => 1,
42 39 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
43 39 my $self = shift;
44 39 my $class = $self->class;
45 39 $class = decamelize($class);
46 39 $class =~ s/::/_/g;
47 #return lc noun($self->human)->singular;
48 39 return lc noun($class)->singular;
49 },
50 43 );
51  
52 has 'plural' => (
53 is => 'ro',
54 init_arg => undef,
55 lazy => 1,
56 required => 1,
57 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 0 my $self = shift;
59 0 return noun($self->singular)->plural;
60 },
61 43 );
62  
63 has 'element' => (
64 is => 'ro',
65 init_arg => undef,
66 lazy => 1,
67 required => 1,
68 171 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
69 171 my $self = shift;
70 171 my $class = $self->class;
71 171 $class =~ s/^.+:://;
72 171 $class = decamelize($class);
73 },
74 43 );
75  
76 has _human => (
77 is => 'ro',
78 init_arg => undef,
79 lazy => 1,
80 required => 1,
81 171 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
82 171 my $self = shift;
83 171 my $name = $self->element;
84 171 $name =~s/_id$//; # remove trailing _id
85 171 $name =~s/_/ /g;
86 171 my $formated = autoformat $name, {case=>'title'};
87 171 $formated=~s/\n//g; # some sort of bug in autoformat?
88 171 return $formated;
89 },
90 43 );
91  
92 # This is the translation tag used to lookup a translated version of the model
93 # name. This is only used via ->human if the consuming class does 'i18n_scope'.
94 # For example if the i18n_scope is 'valiant' and class package is AbcDef::Ghi then
95 # the i18n_key is 'abc_def/ghi'.
96
97 has i18n_key => (
98 is => 'ro',
99 init_arg => undef,
100 lazy => 1,
101 required => 1,
102 1749 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
103 1749 my $self = shift;
104 1749 my $class = $self->class;
105 1749 $class =~s/::/\//g;
106 1749 return Valiant::I18N->make_tag(decamelize($class));
107 },
108 43 );
109  
110 2872 ▶sub i18n_class { 'Valiant::I18N' }
Subroutine i18n_class: called CC=1 SCAR=0.0
Pod undocumented
111  
112 has 'i18n' => (
113 is => 'ro',
114 required => 1,
115 2872 ▶ default => sub { Module::Runtime::use_module(shift->i18n_class) },
Subroutine __ANON__: called CC=1 SCAR=0.0
116 43 );
117  
118 has param_key => (
119 is => 'ro',
120 required => 1,
121 lazy => 1,
122 39 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
123 39 my $self = shift;
124 39 ▶ $self->has_namespace ? decamelize($self->unnamespaced) : $self->singular;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_namespace ? : 0 39 39
125 },
126 43 );
127  
128 171 ▶sub human {
Subroutine human: called CC=12 SCAR=27.2
Pod undocumented
129 171 my ($self, %options) = @_;
130  
131 # if no scope then skip
132 171 ▶ unless($self->class->can('i18n_scope')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->class->can("i18n_scope")) 12 159 171
133 12 ▶ return (exists($options{count}) && ($options{count} > 1)) ?
Branch: 1 branch
BranchTrueFalseTotal
exists $options{'count'} && $options{'count'} > 1 ? : 0 12 12
Condition33%1/3
exists $options{"count"} and $options{"count"} > 1
!ll&&!rl&&r
1200
MC/DC0%0/2
exists $options{"count"} and $options{"count"} > 1
exists $options{"count"} $options{"count"} > 1
Truth table33%1/3
A and B
A= exists $options{"count"}B= $options{"count"} > 1
ABresult
0X0
100
111
134 noun($self->_human)->plural :
135 $self->_human;
136 }
137  
138 159 my $scope = $self->class->i18n_scope . '.models';
139  
140 my @defaults = map {
141 159 ▶ bless \"${scope}.@{[ $_->model_name->i18n_key ]}", 'Valiant::I18N::Tag';
Branch: 1 branch
BranchTrueFalseTotal
if $self->class->can("i18n_lookup") 158 1 159
142 } $self->class->i18n_lookup if $self->class->can('i18n_lookup');
143  
144 # If $self doesn't do i18n_lookup then there's no tags to attempt translations
145 159 ▶ unless(@defaults) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@defaults) 1 158 159
146 1 ▶ return (exists($options{count}) && ($options{count} > 1)) ?
Branch: 1 branch
BranchTrueFalseTotal
exists $options{'count'} && $options{'count'} > 1 ? : 0 1 1
Condition33%1/3
exists $options{"count"} and $options{"count"} > 1
!ll&&!rl&&r
100
MC/DC0%0/2
exists $options{"count"} and $options{"count"} > 1
exists $options{"count"} $options{"count"} > 1
Truth table33%1/3
A and B
A= exists $options{"count"}B= $options{"count"} > 1
ABresult
0X0
100
111
147 noun($self->_human)->plural :
148 $self->_human;
149 }
150  
151 158 ▶ push @defaults, delete $options{default} if exists $options{default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"default"} 0 158 158
152 158 ▶ push @defaults, (exists($options{count}) && ($options{count} > 1)) ?
Branch: 1 branch
BranchTrueFalseTotal
exists $options{'count'} && $options{'count'} > 1 ? : 0 158 158
Condition33%1/3
exists $options{"count"} and $options{"count"} > 1
!ll&&!rl&&r
15800
MC/DC0%0/2
exists $options{"count"} and $options{"count"} > 1
exists $options{"count"} $options{"count"} > 1
Truth table33%1/3
A and B
A= exists $options{"count"}B= $options{"count"} > 1
ABresult
0X0
100
111
153 noun($self->_human)->plural :
154 $self->_human; # The guessed named used by looking at the package name is the ultimate default
155  
156 158 %options = (
157 default => \@defaults,
158 %options,
159 );
160  
161 158 my $translated = $self->i18n->translate(shift(@defaults), %options);
162 158 ▶ return $translated if $translated;
Branch: 1 branch
BranchTrueFalseTotal
if $translated 158 0 158
163 }
164  
165 package Valiant::Naming;
166  
167 43 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
168  
169 2868 ▶sub name_class { 'Valiant::Name' }
Subroutine name_class: called CC=1 SCAR=0.0
Pod undocumented
170  
171 43 my %_model_name = ();
172 2868 ▶sub model_name {
Subroutine model_name: called CC=3 SCAR=11.4
Pod documented
173 2868 my ($self) = @_;
174 2868 ▶ my $class = ref($self) || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
20626620
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
175  
176 2868 ▶ return $_model_name{$class} ||= do {
Condition33%1/3
$_model_name{$class} ||= do { my(%args) = $self->prepare_model_name_args; my $naming = &Module::Runtime::use_module(scalar $self->name_class)->new(%args); return $naming }
l!l&&r!l&&!r
002868
MC/DC0%0/2
$_model_name{$class} ||= do { my(%args) = $self->prepare_model_name_args; my $naming = &Module::Runtime::use_module(scalar $self->name_class)->new(%args); return $naming }
$_model_name{$class} do { my(%args) = $self->prepare_model_name_args; my $naming = &Module::Runtime::use_module(scalar $self->name_class)->new(%args); return $naming }
Truth table33%1/3
A ||= B
A= $_model_name{$class}B= do { my(%args) = $self->prepare_model_name_args; my $naming = &Module::Runtime::use_module(scalar $self->name_class)->new(%args); return $naming }
ABresult
1X1
011
000
177 2868 my %args = $self->prepare_model_name_args;
178 2868 my $naming = Module::Runtime::use_module($self->name_class)->new(%args);
179 2868 return $naming;
180 };
181 }
182  
183 2872 ▶sub prepare_model_name_args {
Subroutine prepare_model_name_args: called CC=3 SCAR=11.6
Pod undocumented
184 2872 my ($self) = @_;
185 2872 ▶ my $class = ref($self) || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
21026620
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
186 2872 my %args = (class => $class);
187 2872 ▶ if($self->can('namespace')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("namespace")) 0 2872 2872
188 0 $args{namespace} = $self->namespace;
189 }
190  
191 2872 return %args;
192 }
193  
194 43 1;
195  
196 =head1 NAME
197  
198 Valiant::Naming - Standard naming information for your models
199  
200 =head1 SYNOPSIS
201  
202 $model->model_name->human;
203 $model->model_name->singular;
204 $model->model_name->plural;
205 $model->model_name->param_key;
206  
207  
208 =head1 DESCRIPTION
209  
210 Exposes a method on your models called C<model_name> which returns an instance of
211 L<Valiant::Name>. This object contains various attributes used for creating a standard
212 approach to naming or referencing your object.
213  
214 If your object defines a method C<i18n_scope> that will be used as the base namespace
215 part to lookup your objects naming information from a set of defined translations.
216  
217 =head1 METHODS
218  
219 This component adds the following methods to your result classes.
220  
221 =head2 model_name
222  
223 An instance of L<Valiant::Name>. This object exposes the following attributes:
224  
225 =head2 human
226  
227 A human readable name for your object. This will either be inferred from the package
228 name of the object or if C<i18n_scope> is defined will be looked up in translations.
229  
230 You can get the pluralized version of this name by passing a C<count> argument greater
231 than 1. Example:
232  
233 $model->model_name->human(count=>2); # returns 'People'
234  
235 =head2 singular
236  
237 =head2 plural
238  
239 Your model name in singular or plural form.
240  
241 =head2 param_key
242  
243 A name for your object that is suitable for serialization such as in an HTML form or
244 other serialization formats.
245  
246 =head1 AUTHOR
247
248 John Napiorkowski L<email:jjnapiork@cpan.org>
249
250 =head1 SEE ALSO
251
252 L<Valiant>
253  
254 =head1 COPYRIGHT & LICENSE
255
256 Copyright 2020, John Napiorkowski L<email:jjnapiork@cpan.org>
257
258 This library is free software; you can redistribute it and/or modify it under
259 the same terms as Perl itself.
260  
261 =cut
« Valiant/Validator/With.pm ↑ Summary Valiant/HTML/FormBuilder/RadioButton.pm »