blib/lib/Venus/Role/Buildable.pm

filter: Statementstmt 100% 29 / 29 Branchbran 100% 10 / 10 Conditioncond 90.4% 19 / 21 MC/DCmcdc 76.9% 10 / 13 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 3 totaltotal 90.3% 75 / 83 SCAR 25.7
CC
13
Cov
97%
BUILDARGS
12.0
BUILD
2.0
BEGIN
1.0
CRAP 13.0
« blib/lib/Venus/Vars.pm ↑ Summary blib/lib/Venus/Kind.pm »
1 package Venus::Role::Buildable;
2  
3 96 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 96 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 96 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 96 ▶use Venus::Role 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # BUILDERS
11  
12 sub BUILD {
13 15118 ▶ my ($self, $args) = @_;
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
14  
15 15118 ▶ if ($self->can('build_self')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("build_self")) 6955 8163 15118
16 6955 $self->build_self($args);
17 }
18  
19 15118 return $self;
20 }
21  
22 sub BUILDARGS {
23 15118 ▶ my ($self, @args) = @_;
Subroutine BUILDARGS: called CC=12 SCAR=24.9
Pod undocumented
24  
25 # build_nil accepts a single-arg (empty hash)
26 15118 ▶ 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
1452253858
@args == 1 and ref $args[0] eq "HASH"
!ll&&!rl&&r
97414781596
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
27  
28 # empty hash argument
29 15118 ▶ if ($self->can('build_nil') && $present) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("build_nil") and $present) 32 15086 15118
Condition100%3/3
$self->can("build_nil") and $present
!ll&&!rl&&r
11490359632
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
30 32 @args = ($self->build_nil($args[0]));
31 }
32  
33 # build_arg accepts a single-arg (non-hash)
34 15118 ▶ my $inflate = @args == 1 && ref $args[0] ne 'HASH';
Condition100%3/3
@args == 1 && ref $args[0] ne 'HASH'
!ll&&!rl&&r
97415964781
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
35  
36 # single argument
37 15118 ▶ if ($self->can('build_arg') && $inflate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("build_arg") and $inflate) 4781 10337 15118
Condition100%3/3
$self->can("build_arg") and $inflate
!ll&&!rl&&r
4103334781
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
38 4781 @args = ($self->build_arg($args[0]));
39 }
40  
41 # build_args should not accept a single-arg (non-hash)
42 15118 ▶ my $ignore = @args == 1 && ref $args[0] ne 'HASH';
Condition66%2/3
@args == 1 && ref $args[0] ne 'HASH'
!ll&&!rl&&r
974153770
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
43  
44 # standard arguments
45 15118 ▶ if ($self->can('build_args') && !$ignore) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("build_args") and not $ignore) 5655 9463 15118
Condition66%2/3
$self->can("build_args") and not $ignore
!ll&&!rl&&r
946305655
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
46 5655 ▶ @args = ($self->build_args(@args == 1 ? $args[0] : {@args}));
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 ? : 1766 3889 5655
47 }
48  
49 15118 return (@args);
50 }
51  
52 # EXPORTS
53  
54 sub EXPORT {
55 515 ▶ ['BUILDARGS']
Subroutine EXPORT: called CC=1 SCAR=0.0
Pod undocumented
56 }
57  
58 96 1;
59  
60  
61  
62 =head1 NAME
63  
64 Venus::Role::Buildable - Buildable Role
65  
66 =cut
67  
68 =head1 ABSTRACT
69  
70 Buildable Role for Perl 5
71  
72 =cut
73  
74 =head1 SYNOPSIS
75  
76 package Example;
77  
78 use Venus::Class;
79  
80 with 'Venus::Role::Buildable';
81  
82 attr 'test';
83  
84 package main;
85  
86 my $example = Example->new;
87  
88 # $example->test;
89  
90 =cut
91  
92 =head1 DESCRIPTION
93  
94 This package modifies the consuming package and provides methods for hooking
95 into object construction of the consuming class, e.g. handling single-arg
96 object construction.
97  
98 =cut
99  
100 =head1 METHODS
101  
102 This package provides the following methods:
103  
104 =cut
105  
106 =head2 build_arg
107  
108 build_arg(any $data) (hashref)
109  
110 The build_arg method, if defined, is only called during object construction
111 when a single non-hashref is provided.
112  
113  
114  
115  
116 I<Since C<0.01>>
117  
118 =over 4
119  
120 =item build_arg example 1
121  
122 package Example1;
123  
124 use Venus::Class;
125  
126 attr 'x';
127 attr 'y';
128  
129 with 'Venus::Role::Buildable';
130  
131 sub build_arg {
132 my ($self, $data) = @_;
133  
134 $data = { x => $data, y => $data };
135  
136 return $data;
137 }
138  
139 package main;
140  
141 my $example = Example1->new(10);
142  
143 # $example->x;
144 # $example->y;
145  
146 =back
147  
148 =cut
149  
150 =head2 build_args
151  
152 build_args(hashref $data) (hashref)
153  
154 The build_args method, if defined, is only called during object construction to
155 hook into the handling of the arguments provided.
156  
157 I<Since C<0.01>>
158  
159 =over 4
160  
161 =item build_args example 1
162  
163 package Example2;
164  
165 use Venus::Class;
166  
167 attr 'x';
168 attr 'y';
169  
170 with 'Venus::Role::Buildable';
171  
172 sub build_args {
173 my ($self, $data) = @_;
174  
175 $data->{x} ||= int($data->{x} || 100);
176 $data->{y} ||= int($data->{y} || 100);
177  
178 return $data;
179 }
180  
181 package main;
182  
183 my $example = Example2->new(x => 10, y => 10);
184  
185 # $example->x;
186 # $example->y;
187  
188 =back
189  
190 =cut
191  
192 =head2 build_nil
193  
194 build_nil(hashref $data) (any)
195  
196 The build_nil method, if defined, is only called during object construction
197 when a single empty hashref is provided.
198  
199 I<Since C<0.01>>
200  
201 =over 4
202  
203 =item build_nil example 1
204  
205 package Example4;
206  
207 use Venus::Class;
208  
209 attr 'x';
210 attr 'y';
211  
212 with 'Venus::Role::Buildable';
213  
214 sub build_nil {
215 my ($self, $data) = @_;
216  
217 $data = { x => 10, y => 10 };
218  
219 return $data;
220 }
221  
222 package main;
223  
224 my $example = Example4->new({});
225  
226 # $example->x;
227 # $example->y;
228  
229 =back
230  
231 =cut
232  
233 =head2 build_self
234  
235 build_self(hashref $data) (object)
236  
237 The build_self method, if defined, is only called during object construction
238 after all arguments have been handled and set.
239  
240 I<Since C<0.01>>
241  
242 =over 4
243  
244 =item build_self example 1
245  
246 package Example3;
247  
248 use Venus::Class;
249  
250 attr 'x';
251 attr 'y';
252  
253 with 'Venus::Role::Buildable';
254  
255 sub build_self {
256 my ($self, $data) = @_;
257  
258 die if !$self->x;
259 die if !$self->y;
260  
261 return $self;
262 }
263  
264 package main;
265  
266 my $example = Example3->new(x => 10, y => 10);
267  
268 # $example->x;
269 # $example->y;
270  
271 =back
272  
273 =cut
274  
275 =head1 AUTHORS
276  
277 Awncorp, C<awncorp@cpan.org>
278  
279 =cut
280  
281 =head1 LICENSE
282  
283 Copyright (C) 2000, Awncorp, C<awncorp@cpan.org>.
284  
285 This program is free software, you can redistribute it and/or modify it under
286 the terms of the Apache license version 2.0.
287  
288 =cut
« blib/lib/Venus/Vars.pm ↑ Summary blib/lib/Venus/Kind.pm »