blib/lib/Venus/Role/Buildable.pm

filter: Statementstmt 100% 34 / 34 Branchbran 100% 16 / 16 Conditioncond 87.5% 21 / 24 MC/DCmcdc 73.3% 11 / 15 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 3 totaltotal 89.9% 89 / 99 CC 18 SCAR 25.2
CC
18
Cov
96%
BUILDARGS
17.0
BUILD
2.0
BEGIN
1.0
CRAP 12.4
« blib/lib/Venus/Throw.pm ↑ Summary blib/lib/Venus/Role/Serializable.pm »
1 package Venus::Role::Buildable;
2  
3 119 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 119 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 119 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # IMPORTS
9  
10 119 ▶use Venus::Role 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # BUILDERS
13  
14 68414 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
15 68414 my ($self, $args) = @_;
16  
17 68414 ▶ if ($self->can('build_self')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("build_self")) 23878 44536 68414
18 23878 $self->build_self($args);
19 }
20  
21 68413 return $self;
22 }
23  
24 68414 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=17 SCAR=28.4
Pod undocumented
25 68414 my ($self, @args) = @_;
26  
27 # build_nil accepts a single-arg (empty hash)
28 68414 ▶ my $present = @args == 1 && ref $args[0] eq 'HASH' && !%{$args[0]};
Condition100%6/6
@args == 1 && ref $args[0] eq 'HASH' && !%{$args[0];}
!ll&&!rl&&r
6772759295
@args == 1 and ref $args[0] eq "HASH"
!ll&&!rl&&r
3959528132687
MC/DC100%3/3
@args == 1 && ref $args[0] eq 'HASH' && !%{$args[0];}
@args == 1 ref $args[0] eq "HASH" !%{$args[0];}
Truth table100%4/4
A and B && not(C)
A= @args == 1B= ref $args[0] eq "HASH"C= !%{$args[0];}
ABCresult
0XX0
10X0
1100
1111
29  
30 # empty hash argument
31 68414 ▶ if ($self->can('build_nil') && $present) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("build_nil") and $present) 33 68381 68414
Condition100%3/3
$self->can("build_nil") and $present
!ll&&!rl&&r
59564881733
MC/DC100%2/2
$self->can("build_nil") and $present
$self->can("build_nil") $present
Truth table100%3/3
A and B
A= $self->can("build_nil")B= $present
ABresult
0X0
100
111
32 33 @args = ($self->build_nil($args[0]));
33 }
34  
35 # build_arg accepts a single-arg (non-hash)
36 68414 ▶ my $inflate = @args == 1 && ref $args[0] ne 'HASH';
Condition100%3/3
@args == 1 && ref $args[0] ne 'HASH'
!ll&&!rl&&r
3959568728132
MC/DC100%2/2
@args == 1 && ref $args[0] ne 'HASH'
@args == 1 ref $args[0] ne 'HASH'
Truth table100%3/3
A && B
A= @args == 1B= ref $args[0] ne 'HASH'
ABresult
0X0
100
111
37  
38 # single argument
39 68414 ▶ if ($self->can('build_arg') && $inflate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("build_arg") and $inflate) 28132 40282 68414
Condition100%3/3
$self->can("build_arg") and $inflate
!ll&&!rl&&r
54027728132
MC/DC100%2/2
$self->can("build_arg") and $inflate
$self->can("build_arg") $inflate
Truth table100%3/3
A and B
A= $self->can("build_arg")B= $inflate
ABresult
0X0
100
111
40 28132 @args = ($self->build_arg($args[0]));
41 }
42  
43 # build_args should not accept a single-arg (non-hash)
44 68414 ▶ my $ignore = @args == 1 && ref $args[0] ne 'HASH';
Condition66%2/3
@args == 1 && ref $args[0] ne 'HASH'
!ll&&!rl&&r
39595288190
MC/DC0%0/2
@args == 1 && ref $args[0] ne 'HASH'
@args == 1 ref $args[0] ne 'HASH'
Truth table66%2/3
A && B
A= @args == 1B= ref $args[0] ne 'HASH'
ABresult
0X0
100
111
45  
46 # build_data partitions expected and unexpected arguments
47 68414 ▶ if ($self->can('build_data') && !$ignore) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("build_data") and not $ignore) 776 67638 68414
Condition66%2/3
$self->can("build_data") and not $ignore
!ll&&!rl&&r
676380776
MC/DC50%1/2
$self->can("build_data") and not $ignore
$self->can("build_data") not $ignore
Truth table66%2/3
A and not(B)
A= $self->can("build_data")B= not $ignore
ABresult
0X0
100
111
48 776 ▶ @args = @args == 1 ? ({}, $args[0]) : ({}, {@args});
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 ? : 58 718 776
49 776 ▶ do{$args[0]->{$_} = delete $args[1]->{$_} if exists $args[1]->{$_}} for $self->META->attrs;
Branch: 1 branch
BranchTrueFalseTotal
if exists $args[1]{$_} 1219 5485 6704
50 776 @args = ($self->build_data(@args));
51 }
52  
53 # standard arguments
54 68414 ▶ if ($self->can('build_args') && !$ignore) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("build_args") and not $ignore) 11736 56678 68414
Condition66%2/3
$self->can("build_args") and not $ignore
!ll&&!rl&&r
56678011736
MC/DC50%1/2
$self->can("build_args") and not $ignore
$self->can("build_args") not $ignore
Truth table66%2/3
A and not(B)
A= $self->can("build_args")B= not $ignore
ABresult
0X0
100
111
55 11736 ▶ @args = ($self->build_args(@args == 1 ? $args[0] : {@args}));
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 ? : 7711 4025 11736
56 }
57  
58 68414 return (@args);
59 }
60  
61 # EXPORTS
62  
63 851 ▶sub EXPORT {
Subroutine EXPORT: called CC=1 SCAR=0.0
Pod undocumented
64 851 ['BUILDARGS']
65 }
66  
67 119 1;
68  
69  
70  
71 =head1 NAME
72  
73 Venus::Role::Buildable - Buildable Role
74  
75 =cut
76  
77 =head1 ABSTRACT
78  
79 Buildable Role for Perl 5
80  
81 =cut
82  
83 =head1 SYNOPSIS
84  
85 package Example;
86  
87 use Venus::Class;
88  
89 with 'Venus::Role::Buildable';
90  
91 attr 'test';
92  
93 package main;
94  
95 my $example = Example->new;
96  
97 # $example->test;
98  
99 =cut
100  
101 =head1 DESCRIPTION
102  
103 This package modifies the consuming package and provides methods for hooking
104 into object construction of the consuming class, e.g. handling single-arg
105 object construction.
106  
107 =cut
108  
109 =head1 METHODS
110  
111 This package provides the following methods:
112  
113 =cut
114  
115 =head2 build_arg
116  
117 build_arg(any $data) (hashref)
118  
119 The build_arg method, if defined, is only called during object construction
120 when a single non-hashref is provided.
121  
122  
123  
124  
125 I<Since C<0.01>>
126  
127 =over 4
128  
129 =item build_arg example 1
130  
131 package Example1;
132  
133 use Venus::Class;
134  
135 attr 'x';
136 attr 'y';
137  
138 with 'Venus::Role::Buildable';
139  
140 sub build_arg {
141 my ($self, $data) = @_;
142  
143 $data = { x => $data, y => $data };
144  
145 return $data;
146 }
147  
148 package main;
149  
150 my $example = Example1->new(10);
151  
152 # $example->x;
153 # $example->y;
154  
155 =back
156  
157 =cut
158  
159 =head2 build_args
160  
161 build_args(hashref $data) (hashref)
162  
163 The build_args method, if defined, is only called during object construction to
164 hook into the handling of the arguments provided.
165  
166 I<Since C<0.01>>
167  
168 =over 4
169  
170 =item build_args example 1
171  
172 package Example2;
173  
174 use Venus::Class;
175  
176 attr 'x';
177 attr 'y';
178  
179 with 'Venus::Role::Buildable';
180  
181 sub build_args {
182 my ($self, $data) = @_;
183  
184 $data->{x} ||= int($data->{x} || 100);
185 $data->{y} ||= int($data->{y} || 100);
186  
187 return $data;
188 }
189  
190 package main;
191  
192 my $example = Example2->new(x => 10, y => 10);
193  
194 # $example->x;
195 # $example->y;
196  
197 =back
198  
199 =cut
200  
201 =head2 build_data
202  
203 build_data(hashref $args, hashref $xargs) (hashref)
204  
205 The build_data method, if defined, is only called during object construction to
206 hook into the handling of the arguments provided. This method is passed two
207 hashrefs, the first containing expected arguments provided to the constructor
208 (e.g. attributes), and the second containing all unexpected arguments. The
209 hashref or key/value pairs returned from this method will be used in subsequent
210 automation.
211  
212 I<Since C<4.15>>
213  
214 =over 4
215  
216 =item build_data example 1
217  
218 package Example5;
219  
220 use Venus::Class;
221  
222 attr 'x';
223 attr 'y';
224  
225 with 'Venus::Role::Buildable';
226  
227 sub build_data {
228 my ($self, $args, $xargs) = @_;
229  
230 $args->{z} = delete $xargs->{z} if !exists $args->{z} && exists $xargs->{z};
231  
232 return $args;
233 }
234  
235 package main;
236  
237 my $example = Example5->new(x => 10, y => 10, z => 10);
238  
239 # $example->x;
240 # $example->y;
241  
242 =back
243  
244 =cut
245  
246 =head2 build_nil
247  
248 build_nil(hashref $data) (any)
249  
250 The build_nil method, if defined, is only called during object construction
251 when a single empty hashref is provided.
252  
253 I<Since C<0.01>>
254  
255 =over 4
256  
257 =item build_nil example 1
258  
259 package Example4;
260  
261 use Venus::Class;
262  
263 attr 'x';
264 attr 'y';
265  
266 with 'Venus::Role::Buildable';
267  
268 sub build_nil {
269 my ($self, $data) = @_;
270  
271 $data = { x => 10, y => 10 };
272  
273 return $data;
274 }
275  
276 package main;
277  
278 my $example = Example4->new({});
279  
280 # $example->x;
281 # $example->y;
282  
283 =back
284  
285 =cut
286  
287 =head2 build_self
288  
289 build_self(hashref $data) (object)
290  
291 The build_self method, if defined, is only called during object construction
292 after all arguments have been handled and set.
293  
294 I<Since C<0.01>>
295  
296 =over 4
297  
298 =item build_self example 1
299  
300 package Example3;
301  
302 use Venus::Class;
303  
304 attr 'x';
305 attr 'y';
306  
307 with 'Venus::Role::Buildable';
308  
309 sub build_self {
310 my ($self, $data) = @_;
311  
312 die if !$self->x;
313 die if !$self->y;
314  
315 return $self;
316 }
317  
318 package main;
319  
320 my $example = Example3->new(x => 10, y => 10);
321  
322 # $example->x;
323 # $example->y;
324  
325 =back
326  
327 =cut
328  
329 =head1 AUTHORS
330  
331 Awncorp, C<awncorp@cpan.org>
332  
333 =cut
334  
335 =head1 LICENSE
336  
337 Copyright (C) 2022, Awncorp, C<awncorp@cpan.org>.
338  
339 This program is free software, you can redistribute it and/or modify it under
340 the terms of the Apache license version 2.0.
341  
342 =cut
« blib/lib/Venus/Throw.pm ↑ Summary blib/lib/Venus/Role/Serializable.pm »