lib/Web/Components/Util.pm

filter: Statementstmt 80.0% 84 / 105 Branchbran 77.2% 16 / 22 (1 uncoverable) Conditioncond 64.5% 19 / 31 (1 uncoverable) MC/DCmcdc 47.3% 7 / 19 (2 uncoverable) Subroutinesub 84.6% 22 / 26 Podpod 100% 11 / 11 totaltotal 76.1% 159 / 214 (4 uncoverable) CC 27 SCAR 18.0
CC
27
Cov
77%
_qualify
20.0
load_components
12.6
deref
6.0
CRAP 6.1
« lib/Web/Components/Model.pm ↑ Summary lib/Web/Components/Role.pm »
1 package Web::Components::Util;
2  
3 1 ▶use strictures;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use parent 'Exporter::Tiny';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Web::ComposableRequest::Constants qw( EXCEPTION_CLASS FALSE TRUE );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Unexpected::Functions qw( Unspecified );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use List::Util qw( pairs );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Sys::Hostname qw( hostname );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Web::ComposableRequest::Util qw( is_hashref );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Module::Pluggable::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Moo::Role ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our @EXPORT_OK = qw( build_routes clear_redirect deref exception first_char
16 formpost fqdn is_arrayref load_components ns_environment
17 throw );
18  
19 =pod
20  
21 =encoding utf-8
22  
23 =head1 Name
24  
25 Web::Components::Util - Functions used in this distribution
26  
27 =head1 Synopsis
28  
29 use Web::Components::Util qw( load_components );
30  
31 =head1 Description
32  
33 Functions used in this distribution
34  
35 =head1 Configuration and Environment
36  
37 Defines no attributes
38  
39 =head1 Subroutines/Methods
40  
41 Defines the following functions;
42  
43 =over 3
44  
45 =item C<build_routes>
46  
47 Takes a list of pairs of L<Web::Simple> routing expressions and
48 L<Web::Components> action paths. Returns a list of pairs of routing expressions
49 and subroutines used by L<Web::Simple>s C<dispatch_request> function
50  
51 =cut
52  
53 1 ▶sub build_routes (@) {
Subroutine build_routes: called CC=2 SCAR=6.9
Pod documented
54 1 my @pairs = @_;
55 1 my @routes = ();
56  
57 1 for my $pair (pairs @pairs) {
58 1 ▶ push @routes, $pair->key, sub {[$pair->value, @_]};
Subroutine __ANON__: called CC=1 SCAR=0.0
59 }
60  
61 1 return @routes;
62 }
63  
64 =item C<clear_redirect>
65  
66 Deletes the stashed entry that would otherwise cause a redirect response
67  
68 =cut
69  
70 0 ▶sub clear_redirect ($) {
Subroutine clear_redirect: not called CC=1 SCAR=6.9
Pod documented
71 0 return delete shift->stash->{redirect};
72 }
73  
74 =item C<deref>
75  
76 $value = deref $object_or_hash_ref, $key, $optional_default;
77  
78 Returns the value associated with the supplied key. Accepts either an object
79 or hash reference as the first argument. Returns the default if the result
80 is otherwise undefined
81  
82 =cut
83  
84 21 ▶sub deref ($$;$) {
Subroutine deref: called CC=6 SCAR=17.9
Pod documented
85 21 my ($x, $k, $default) = @_;
86  
87 21 my $r = $default;
88  
89 21 ▶ if (blessed $x and $x->can($k)) { $r = $x->$k() // $default }
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($x) and $x->can($k)) { } 5 16 21
elsif (&is_hashref($x)) { } 6 10 16
Condition83%5/6
builtin::blessed($x) and $x->can($k)
!ll&&!rl&&r
6105
$x->$k // $default
l!l&&r!l&&!r
320
MC/DC100%2/2
builtin::blessed($x) and $x->can($k)
builtin::blessed($x) $x->can($k)
MC/DC0%0/2
$x->$k // $default
$x->$k $default
Truth table100%3/3
A and B
A= builtin::blessed($x)B= $x->can($k)
ABresult
0X0
100
111
Truth table66%2/3
A // B
A= $x->$kB= $default
ABresult
1X1
011
000
90 6 ▶ elsif (is_hashref $x) { $r = $x->{$k} // $default }
Condition100%3/3
$x->{$k} // $default
l!l&&r!l&&!r
222
MC/DC100%2/2
$x->{$k} // $default
$x->{$k} $default
Truth table100%3/3
A // B
A= $x->{$k}B= $default
ABresult
1X1
011
000
91  
92 21 return $r;
93 }
94  
95 =item C<exception>
96  
97 $e = exception $error;
98  
99 Expose the C<catch> method in the exception class C<EXCEPTION_CLASS> defined in
100 constants. Returns a new exception object
101  
102 =cut
103  
104 4 ▶sub exception (;@) {
Subroutine exception: called CC=1 SCAR=0.0
Pod documented
105 4 return EXCEPTION_CLASS->caught( @_ );
106 }
107  
108 =item C<first_char>
109  
110 $single_char = first_char $some_string;
111  
112 Returns the first character of C<$string>
113  
114 =cut
115  
116 4 ▶sub first_char ($) {
Subroutine first_char: called CC=1 SCAR=0.0
Pod documented
117 4 return substr $_[ 0 ], 0, 1;
118 }
119  
120 =item C<formpost>
121  
122 Returns a hash references which is stashed to indicate that a link is really
123 a button wrapped in a form
124  
125 =cut
126  
127 0 ▶sub formpost () {
Subroutine formpost: not called CC=1 SCAR=6.9
Pod documented
128 0 return { method => 'post' };
129 }
130  
131 =item C<fqdn>
132  
133 $domain_name = fqdn $hostname;
134  
135 Call C<gethostbyname> on the supplied hostname which defaults to this host
136  
137 =cut
138  
139 0 ▶sub fqdn (;$) {
Subroutine fqdn: not called CC=2 SCAR=17.9
Pod documented
140 0 ▶ my $x = shift // hostname; return (gethostbyname($x))[0];
Condition0%0/3
shift() // hostname()
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() // hostname()
shift() hostname()
Truth table0%0/3
A // B
A= shift()B= hostname()
ABresult
1X1
011
000
141 }
142  
143 =item C<is_arrayref>
144  
145 $bool = is_arrayref $scalar_variable;
146  
147 Tests to see if the scalar variable is an array ref
148  
149 =cut
150  
151 6 ▶sub is_arrayref (;$) {
Subroutine is_arrayref: called CC=3 SCAR=11.0
Pod documented
152 6 ▶ return $_[ 0 ] && ref $_[ 0 ] eq 'ARRAY' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] && ref $_[0] eq 'ARRAY' ? : 4 2 6
Condition100%3/3
$_[0] and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
114
MC/DC100%2/2
$_[0] and ref $_[0] eq "ARRAY"
$_[0] ref $_[0] eq "ARRAY"
Truth table100%3/3
A and B
A= $_[0]B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
153 }
154  
155 =item C<load_components>
156  
157 $hash_ref_of_objects = load_components $search_path, @options_list;
158  
159 Load and instantiates MVC components. The search path is appended to the
160 applications classname to define the package namespace that is searched for
161 components
162  
163 The options list is a list of keys and values. Either C<application> or,
164 C<config> and C<log> must be specified. If C<application> is specified it
165 must define C<config> and C<log> attributes
166  
167 The configuration object or hash reference must define the C<appclass> and
168 C<web_components> attributes
169  
170 The C<web_components> attribute (one of the collection references held by
171 L<Web::Components::Loader>) is passed to the component constructor method and
172 is used by a component to discover it's dependencies
173  
174 An adaptor pattern is possible using the C<web_components_adaptors> attribute
175  
176 =cut
177  
178 5 ▶sub load_components ($;@) {
Subroutine load_components: called CC=11 SCAR=25.3
Pod documented
179 5 my $base = shift;
180 5 ▶ my $opts = (is_hashref $_[0]) ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
&is_hashref($_[0]) ? : 1 4 5
181  
182 5 ▶ throw(Unspecified, ['component base']) unless $base;
Branch: 1 branch
BranchTrueFalseTotal
unless $base 1 4 5
183  
184 4 my $app = $opts->{application};
185 # If the app object is defined it must have a config attribute
186 # uncoverable condition false
187 4 ▶ my $config = $opts->{config} // $app->config;
Condition100%2/3
$opts->{"config"} // $app->config
l!l&&r!l&&!r
130
MC/DC100%0/2
$opts->{"config"} // $app->config
$opts->{"config"} $app->config
Truth table100%2/3
A // B
A= $opts->{"config"}B= $app->config
ABresult
1X1
011
000
188 4 my $appclass = deref $config, 'appclass';
189 # The config object/hash ref is required. It must have an appclass attribute
190 # uncoverable branch true
191 4 ▶ throw(Unspecified, ['config appclass']) unless $appclass;
Branch: 1 branch
BranchTrueFalseTotal
unless $appclass 0 4 4
192  
193 4 my $search_path;
194  
195 4 ▶ if (first_char $base eq '+') { $search_path = $base = substr $base, 1 }
Branch: 1 branch
BranchTrueFalseTotal
if (&first_char($base) eq '+') { } 1 3 4
196 3 else { $search_path = "${appclass}::${base}" }
197  
198 4 my $depth = () = split m{ :: }mx, $search_path, -1;
199  
200 4 $depth += 1;
201  
202 4 my $finder = Module::Pluggable::Object->new(
203 max_depth => $depth,
204 min_depth => $depth,
205 search_path => [$search_path],
206 require => TRUE,
207 );
208 4 ▶ my $compos = $opts->{components} //= {}; # Dependency injection
Condition100%2/2
$opts->{"components"} //= {}
l!l
13
MC/DC100%1/1
$opts->{"components"} //= {}
$opts->{"components"}
Truth table100%2/2
A
A= $opts->{"components"}
Aresult
11
00
209 4 ▶ my $comp_cfg = (deref $config, 'web_components') // {};
Condition50%1/2
&deref($config, "web_components") // {}
l!l
04
MC/DC0%0/1
&deref($config, "web_components") // {}
&deref($config, "web_components")
Truth table50%1/2
A
A= &deref($config, "web_components")
Aresult
11
00
210  
211 4 for my $class ($finder->plugins) {
212 4 _setup_component($compos, $comp_cfg, $opts, $appclass, $class);
213 }
214  
215 4 my $cfgcomps = deref $config, 'web_components_adaptors';
216  
217 0 ▶ if ($cfgcomps) { $cfgcomps = $cfgcomps->{$base} }
Branch: 1 branch
BranchTrueFalseTotal
if ($cfgcomps) { } 0 4 4
218 4 else { return $compos }
219  
220 0 for my $moniker (keys %{$cfgcomps}) {
221 0 my $class = "${base}::".(ucfirst $moniker);
222 0 my @roles = @{ _qualify($appclass, $cfgcomps->{$moniker}) };
223 0 my $cwr = Moo::Role->create_class_with_roles($search_path, @roles);
224  
225 0 _setup_component($compos, $comp_cfg, $opts, $appclass, $class, $cwr);
226 }
227  
228 0 return $compos;
229 }
230  
231 2 ▶sub _app_prefix ($) {
Subroutine _app_prefix: called CC=2 SCAR=7.2
232 2 ▶ (my $v = lc ($_[0] // q())) =~ s{ :: }{_}gmx;
Condition50%1/2
$_[0] // ""
l!l
20
MC/DC0%0/1
$_[0] // ""
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
233  
234 2 return $v;
235 }
236  
237 2 ▶sub _env_prefix ($) {
Subroutine _env_prefix: called CC=1 SCAR=0.0
238 2 return uc _app_prefix $_[0];
239 }
240  
241 =item C<ns_environment>
242  
243 $value = ns_environment $class, $key, [$value];
244  
245 An accessor/mutator for the environment variables prefixed by the supplied
246 class name. Providing a value is optional, always returns the current value
247  
248 =cut
249  
250 2 ▶sub ns_environment ($$;$) {
Subroutine ns_environment: called CC=2 SCAR=6.9
Pod documented
251 2 my ($class, $k, $v) = @_;
252  
253 2 $k = (_env_prefix $class) . '_' . (uc $k);
254  
255 2 ▶ return defined $v ? $ENV{$k} = $v : $ENV{$k};
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 1 1 2
256 }
257  
258 =item C<throw>
259  
260 throw 'message', [ 'arg1', 'arg2' ];
261  
262 Expose L<Web::ComposableRequest::Util/throw>.
263 L<Web::ComposableRequest::Constants> has a class attribute I<Exception_Class>
264 which can be set change the class of the thrown exception
265  
266 =cut
267  
268 2 ▶sub throw (;@) {
Subroutine throw: called CC=1 SCAR=0.0
Pod documented
269 2 EXCEPTION_CLASS->throw( @_ );
270 }
271  
272 # Private functions
273 0 ▶sub _qualify {
Subroutine _qualify: not called CC=4 SCAR=30.0
274 0 ▶ my ($appclass, $roles) = @_; $roles //= [];
Condition0%0/2
$roles //= []
l!l
00
MC/DC0%0/1
$roles //= []
$roles
Truth table0%0/2
A
A= $roles
Aresult
11
00
275  
276 0 ▶ for (my $i = 0, my $len = @{ $roles }; $i < $len; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 0 0 0
277 0 ▶ if (first_char( $roles->[ $i ] ) eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if (&first_char($roles->[$i]) eq '+') { } 0 0 0
278 0 $roles->[ $i ] = substr $roles->[ $i ], 1;
279 }
280 0 else { $roles->[ $i ] = "${appclass}::".$roles->[ $i ] }
281 }
282  
283 0 return $roles;
284 }
285  
286 4 ▶sub _setup_component {
Subroutine _setup_component: called CC=3 SCAR=11.3
287 4 my ($compos, $comp_cfg, $opts, $appclass, $class, $composite) = @_;
288  
289 4 ▶ $composite //= $class;
Condition33%1/3
$composite //= $class
l!l&&r!l&&!r
040
MC/DC0%0/2
$composite //= $class
$composite $class
Truth table33%1/3
A //= B
A= $compositeB= $class
ABresult
1X1
011
000
290  
291 4 (my $klass = $class) =~ s{ \A $appclass :: }{}mx;
292 4 ▶ my $attr = { %{ $comp_cfg->{ $klass } // {} }, %{ $opts } };
Condition50%1/2
$comp_cfg->{$klass} // {}
l!l
04
MC/DC0%0/1
$comp_cfg->{$klass} // {}
$comp_cfg->{$klass}
Truth table50%1/2
A
A= $comp_cfg->{$klass}
Aresult
11
00
293 4 my $comp = $composite->new( $attr );
294  
295 4 $compos->{ $comp->moniker } = $comp;
296 4 return;
297 }
298  
299 1 1;
300  
301 __END__
« lib/Web/Components/Model.pm ↑ Summary lib/Web/Components/Role.pm »