Valiant/Naming.pm

filter: Statementstmt 96.4% 81 / 84 Branchbran 85.7% 12 / 14 Conditioncond 66.6% 6 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 94.7% 18 / 19 Podpod 20.0% 1 / 5 totaltotal 86.1% 118 / 137 CC 11 SCAR 8.3
CC
11
Cov
93%
human
5.0
model_name
3.1
prepare_model_name_args
3.0
CRAP 2.3
« Valiant/Validator/Boolean.pm ↑ Summary Valiant/Filter/Template.pm »
1 package Valiant::Name;
2  
3 46 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 46 ▶use String::CamelCase 'decamelize';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 46 ▶use Text::Autoformat 'autoformat';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 46 ▶use Lingua::EN::Inflexion 'noun';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 46 ▶use Valiant::I18N ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # These first few are permitted arguments
10  
11 46 has class => (is=>'ro', required=>1);
12 46 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 default => sub {
28 2 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.1
29 2 ▶ return unless $self->has_namespace;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_namespace 0 2 2
30 2 my $class = $self->class;
31 2 my $ns = $self->namespace;
32 2 $class =~s/^${ns}:://;
33 2 return $class;
34 0 return lc decamelize($class);
35 },
36 46 );
37  
38 has 'singular' => (
39 is => 'ro',
40 init_arg => undef,
41 lazy => 1,
42 required => 1,
43 default => sub {
44 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
45 1 my $class = $self->class;
46 1 $class = decamelize($class);
47 1 $class =~ s/::/_/g;
48 1 return lc noun($class)->singular;
49 },
50 46 );
51  
52 has 'plural' => (
53 is => 'ro',
54 init_arg => undef,
55 lazy => 1,
56 required => 1,
57 default => sub {
58 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 0 return noun($self->singular)->plural;
60 },
61 46 );
62  
63 has 'element' => (
64 is => 'ro',
65 init_arg => undef,
66 lazy => 1,
67 required => 1,
68 default => sub {
69 66 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
70 66 my $class = $self->class;
71 66 $class =~ s/^.+:://;
72 66 $class = decamelize($class);
73 },
74 46 );
75  
76 has _human => (
77 is => 'ro',
78 init_arg => undef,
79 lazy => 1,
80 required => 1,
81 default => sub {
82 66 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
83 66 my $name = $self->element;
84 66 $name =~s/_id$//; # remove trailing _id
85 66 $name =~s/_/ /g;
86 66 my $formated = autoformat $name, {case=>'title'};
87 66 $formated=~s/\n//g; # some sort of bug in autoformat?
88 66 return $formated;
89 },
90 46 );
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 default => sub {
103 81 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
104 81 my $class = $self->class;
105 81 $class =~s/::/\//g;
106 81 return Valiant::I18N->make_tag(decamelize($class));
107 },
108 46 );
109  
110 83 ▶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 83 ▶ default => sub { Module::Runtime::use_module(shift->i18n_class) },
Subroutine __ANON__: called CC=1 SCAR=0.0
116 46 );
117  
118 has param_key => (
119 is => 'ro',
120 required => 1,
121 lazy => 1,
122 default => sub {
123 3 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
124 3 ▶ $self->has_namespace ? decamelize($self->unnamespaced) : $self->singular;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_namespace ? : 2 1 3
125 },
126 46 );
127  
128 sub human {
129 356 ▶ my ($self, %options) = @_;
Subroutine human: called CC=5 SCAR=16.1
Pod undocumented
130  
131 356 ▶ return $self->_human unless $self->class->can('i18n_scope'); # Don't waste time if there's no lookup scope
Branch: 1 branch
BranchTrueFalseTotal
unless $self->class->can("i18n_scope") 7 349 356
132  
133 my @defaults = map {
134 349 ▶ $_->model_name->i18n_key;
Branch: 1 branch
BranchTrueFalseTotal
if $self->class->can("i18n_lookup") 348 1 349
135 } $self->class->i18n_lookup if $self->class->can('i18n_lookup');
136  
137 349 ▶ return $self->_human unless @defaults; # If $self doesn't do i18n_lookup then there's no tags to attempt translations
Branch: 1 branch
BranchTrueFalseTotal
unless @defaults 1 348 349
138
139 348 ▶ push @defaults, delete $options{default} if exists $options{default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"default"} 0 348 348
140 348 push @defaults, $self->_human; # The guessed named used by looking at the package name is the ultimate default
141  
142  
143 348 %options = (
144 scope => [$self->class->i18n_scope, 'models'],
145 count => 1,
146 default => \@defaults,
147 %options,
148 );
149  
150 348 $self->i18n->translate(shift(@defaults), %options);
151 }
152  
153 package Valiant::Naming;
154  
155 46 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
156  
157 83 ▶sub name_class { 'Valiant::Name' }
Subroutine name_class: called CC=1 SCAR=0.0
Pod undocumented
158  
159 46 my %_model_name = ();
160 sub model_name {
161 7035 ▶ my ($self) = @_;
Subroutine model_name: called CC=3 SCAR=11.2
Pod documented
162 7035 ▶ my $class = ref($self) || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
37066650
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
163  
164 7035 ▶ return $_model_name{$class} ||= do {
Condition66%2/3
$_model_name{$class} ||= do {
l!l&&r!l&&!r
6952830
MC/DC0%0/2
$_model_name{$class} ||= do { my(%args) = $self->prepare_model_name_args; &Module::Runtime::use_module(scalar $self->name_class)->new(%args) }
$_model_name{$class} do { my(%args) = $self->prepare_model_name_args; &Module::Runtime::use_module(scalar $self->name_class)->new(%args) }
Truth table66%2/3
A ||= B
A= $_model_name{$class}B= do { my(%args) = $self->prepare_model_name_args; &Module::Runtime::use_module(scalar $self->name_class)->new(%args) }
ABresult
1X1
011
000
165 83 my %args = $self->prepare_model_name_args;
166 83 Module::Runtime::use_module($self->name_class)->new(%args);
167 };
168 }
169  
170 sub prepare_model_name_args {
171 83 ▶ my ($self) = @_;
Subroutine prepare_model_name_args: called CC=3 SCAR=11.0
Pod undocumented
172 83 ▶ my $class = ref($self) || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
55280
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
173 83 my %args = (class => $class);
174 83 ▶ if($self->can('namespace')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("namespace")) 33 50 83
175 33 $args{namespace} = $self->namespace;
176 }
177  
178 83 return %args;
179 }
180  
181 46 1;
182  
183 =head1 NAME
184  
185 Valiant::Naming - Standard naming information for your models
186  
187 =head1 SYNOPSIS
188  
189 $model->model_name->human;
190 $model->model_name->singular;
191 $model->model_name->plural;
192 $model->model_name->param_key;
193  
194  
195 =head1 DESCRIPTION
196  
197 Exposes a method on your models called C<model_name> which returns an instance of
198 L<Valiant::Name>. This object contains various attributes used for creating a standard
199 approach to naming or referencing your object.
200  
201 If your object defines a method C<i18n_scope> that will be used as the base namespace
202 part to lookup your objects naming information from a set of defined translations.
203  
204 =head1 METHODS
205  
206 This component adds the following methods to your result classes.
207  
208 =head2 model_name
209  
210 An instance of L<Valiant::Name>. This object exposes the following attributes:
211  
212 =head2 human
213  
214 A human readable name for your object. This will either be inferred from the package
215 name of the object or if C<i18n_scope> is defined will be looked up in translations.
216  
217 =head2 singular
218  
219 =head2 plural
220  
221 Your model name in singular or plural form.
222  
223 =head2 param_key
224  
225 A name for your object that is suitable for serialization such as in an HTML form or
226 other serialization formats.
227  
228 =head1 AUTHOR
229
230 John Napiorkowski L<email:jjnapiork@cpan.org>
231
232 =head1 SEE ALSO
233
234 L<Valiant>
235  
236 =head1 COPYRIGHT & LICENSE
237
238 Copyright 2020, John Napiorkowski L<email:jjnapiork@cpan.org>
239
240 This library is free software; you can redistribute it and/or modify it under
241 the same terms as Perl itself.
242  
243 =cut
« Valiant/Validator/Boolean.pm ↑ Summary Valiant/Filter/Template.pm »