Venus/Role/Coercible.pm

filter: Statementstmt 83.3% 35 / 42 Branchbran 62.5% 5 / 8 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.9% 10 / 11 Podpod 71.4% 5 / 7 totaltotal 76.7% 56 / 73 CC 8 SCAR 8.0
CC
8
Cov
77%
coerce_into
3.3
coerce_onto
3.0
BUILD
2.9
CRAP 2.2
« Venus/Core.pm ↑ Summary Venus/Role/Explainable.pm »
1 package Venus::Role::Coercible;
2  
3 1 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Venus::Role 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # MODIFIERS
11  
12 sub BUILD {
13 2 ▶ my ($self, $data) = @_;
Subroutine BUILD: called CC=2 SCAR=10.5
Pod undocumented
14  
15 2 $data = $self->coercion($data);
16  
17 0 for my $name (keys %$data) {
18 0 $self->{$name} = $data->{$name};
19 }
20  
21 0 return $self;
22 };
23  
24 # METHODS
25  
26 sub coerce {
27 0 ▶ my ($self) = @_;
Subroutine coerce: not called CC=1 SCAR=6.9
Pod documented
28  
29 0 return {};
30 }
31  
32 sub coerce_args {
33 2 ▶ my ($self, $data, $spec) = @_;
Subroutine coerce_args: called CC=2 SCAR=7.2
Pod documented
34  
35 2 for my $name (grep exists($data->{$_}), sort keys %$spec) {
36 $data->{$name} = $self->coerce_onto(
37 2 $data, $name, $spec->{$name}, $data->{$name},
38 );
39 }
40  
41 0 return $data;
42 }
43  
44 sub coerce_into {
45 2 ▶ my ($self, $class, $value) = @_;
Subroutine coerce_into: called CC=3 SCAR=12.0
Pod documented
46  
47 2 require Scalar::Util;
48 2 require Venus::Space;
49  
50 2 $class = Venus::Space->new($class)->load;
51  
52 2 ▶ if (Scalar::Util::blessed($value) && $value->isa($class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa($class)) { } 0 2 2
Condition33%1/3
builtin::blessed($value) and $value->isa($class)
!ll&&!rl&&r
200
MC/DC0%0/2
builtin::blessed($value) and $value->isa($class)
builtin::blessed($value) $value->isa($class)
Truth table33%1/3
A and B
A= builtin::blessed($value)B= $value->isa($class)
ABresult
0X0
100
111
53 0 return $value;
54 }
55 else {
56 2 return $class->new($value);
57 }
58 }
59  
60 sub coerce_onto {
61 2 ▶ my ($self, $data, $name, $class, $value) = @_;
Subroutine coerce_onto: called CC=3 SCAR=11.0
Pod documented
62  
63 2 require Venus::Space;
64  
65 2 $class = Venus::Space->new($class)->load;
66  
67 2 ▶ $value = $data->{$name} if $#_ < 4;
Branch: 1 branch
BranchTrueFalseTotal
if $#_ < 4 0 2 2
68  
69 2 ▶ if (my $method = $self->can("coerce_${name}")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $self->can("coerce_$name")) { } 1 1 2
70 1 return $data->{$name} = $self->$method(\&coerce_into, $class, $value);
71 }
72 else {
73 1 return $data->{$name} = $self->coerce_into($class, $value);
74 }
75 }
76  
77 sub coercion {
78 2 ▶ my ($self, $data) = @_;
Subroutine coercion: called CC=2 SCAR=7.0
Pod documented
79  
80 2 my $spec = $self->coerce;
81  
82 2 ▶ return $data if !%$spec;
Branch: 1 branch
BranchTrueFalseTotal
unless %$spec 0 2 2
83  
84 2 return $self->coerce_args($data, $spec);
85 }
86  
87 # EXPORTS
88  
89 sub EXPORT {
90 [
91 1 ▶ 'coerce',
Subroutine EXPORT: called CC=1 SCAR=0.0
Pod undocumented
92 'coerce_args',
93 'coerce_into',
94 'coerce_onto',
95 'coercion',
96 ]
97 }
98  
99 1 1;
100  
101  
102  
103 =head1 NAME
104  
105 Venus::Role::Coercible - Coercible Role
106  
107 =cut
108  
109 =head1 ABSTRACT
110  
111 Coercible Role for Perl 5
112  
113 =cut
114  
115 =head1 SYNOPSIS
116  
117 package Person;
118  
119 use Venus::Class;
120  
121 with 'Venus::Role::Coercible';
122  
123 attr 'name';
124 attr 'father';
125 attr 'mother';
126 attr 'siblings';
127  
128 sub coerce {
129 {
130 father => 'Person',
131 mother => 'Person',
132 name => 'Venus/String',
133 siblings => 'Person',
134 }
135 }
136  
137 sub coerce_name {
138 my ($self, $code, @args) = @_;
139  
140 return $self->$code(@args);
141 }
142  
143 sub coerce_siblings {
144 my ($self, $code, $class, $value) = @_;
145  
146 return [map $self->$code($class, $_), @$value];
147 }
148  
149 package main;
150  
151 my $person = Person->new(
152 name => 'me',
153 father => {name => 'father'},
154 mother => {name => 'mother'},
155 siblings => [{name => 'brother'}, {name => 'sister'}],
156 );
157  
158 # $person
159 # bless({...}, 'Person')
160  
161 # $person->name
162 # bless({...}, 'Venus::String')
163  
164 # $person->father
165 # bless({...}, 'Person')
166  
167 # $person->mother
168 # bless({...}, 'Person')
169  
170 # $person->siblings
171 # [bless({...}, 'Person'), bless({...}, 'Person'), ...]
172  
173 =cut
174  
175 =head1 DESCRIPTION
176  
177 This package modifies the consuming package and provides methods for hooking
178 into object construction and coercing arguments into objects and values.
179  
180 =cut
181  
182 =head1 METHODS
183  
184 This package provides the following methods:
185  
186 =cut
187  
188 =head2 coerce
189  
190 coerce() (HashRef)
191  
192 The coerce method, if defined, is called during object construction, or by the
193 L</coercion> method, and returns key/value pairs where the keys map to class
194 attributes (or input parameters) and the values are L<Venus::Space> compatible
195 package names.
196  
197 I<Since C<0.02>>
198  
199 =over 4
200  
201 =item coerce example 1
202  
203 package main;
204  
205 my $person = Person->new(
206 name => 'me',
207 );
208  
209 my $coerce = $person->coerce;
210  
211 # {
212 # father => "Person",
213 # mother => "Person",
214 # name => "Venus/String",
215 # siblings => "Person",
216 # }
217  
218 =back
219  
220 =cut
221  
222 =head2 coerce_args
223  
224 coerce_args(HashRef $data, HashRef $spec) (HashRef)
225  
226 The coerce_args method replaces values in the data provided with objects
227 corresponding to the specification provided. The specification should contains
228 key/value pairs where the keys map to class attributes (or input parameters)
229 and the values are L<Venus::Space> compatible package names.
230  
231 I<Since C<0.07>>
232  
233 =over 4
234  
235 =item coerce_args example 1
236  
237 package main;
238  
239 my $person = Person->new;
240  
241 my $data = $person->coerce_args(
242 {
243 father => { name => 'father' }
244 },
245 {
246 father => 'Person',
247 },
248 );
249  
250 # {
251 # father => bless({...}, 'Person'),
252 # }
253  
254 =back
255  
256 =cut
257  
258 =head2 coerce_into
259  
260 coerce_into(Str $class, Any $value) (Object)
261  
262 The coerce_into method attempts to build and return an object based on the
263 class name and value provided, unless the value provided is already an object
264 derived from the specified class.
265  
266 I<Since C<0.07>>
267  
268 =over 4
269  
270 =item coerce_into example 1
271  
272 package main;
273  
274 my $person = Person->new;
275  
276 my $friend = $person->coerce_into('Person', {
277 name => 'friend',
278 });
279  
280 # bless({...}, 'Person')
281  
282 =back
283  
284 =cut
285  
286 =head2 coerce_onto
287  
288 coerce_onto(HashRef $data, Str $name, Str $class, Any $value) (Object)
289  
290 The coerce_onto method attempts to build and assign an object based on the
291 class name and value provided, as the value corresponding to the name
292 specified, in the data provided. If the C<$value> is omitted, the value
293 corresponding to the name in the C<$data> will be used.
294  
295 I<Since C<0.07>>
296  
297 =over 4
298  
299 =item coerce_onto example 1
300  
301 package main;
302  
303 my $person = Person->new;
304  
305 my $data = { friend => { name => 'friend' } };
306  
307 my $friend = $person->coerce_onto($data, 'friend', 'Person');
308  
309 # bless({...}, 'Person'),
310  
311 # $data was updated
312 #
313 # {
314 # friend => bless({...}, 'Person'),
315 # }
316  
317 =back
318  
319 =cut
320  
321 =head2 coercion
322  
323 coercion(HashRef $data) (HashRef)
324  
325 The coercion method is called automatically during object construction but can
326 be called manually as well, and is passed a hashref to coerce and return.
327  
328 I<Since C<0.02>>
329  
330 =over 4
331  
332 =item coercion example 1
333  
334 package main;
335  
336 my $person = Person->new;
337  
338 my $coercion = $person->coercion({
339 name => 'me',
340 });
341  
342 # $coercion
343 # {...}
344  
345 # $coercion->{name}
346 # bless({...}, 'Venus::String')
347  
348 # $coercion->{father}
349 # undef
350  
351 # $coercion->{mother}
352 # undef
353  
354 # $coercion->{siblings}
355 # undef
356  
357 =back
358  
359 =over 4
360  
361 =item coercion example 2
362  
363 package main;
364  
365 my $person = Person->new;
366  
367 my $coercion = $person->coercion({
368 name => 'me',
369 mother => {name => 'mother'},
370 siblings => [{name => 'brother'}, {name => 'sister'}],
371 });
372  
373 # $coercion
374 # {...}
375  
376 # $coercion->{name}
377 # bless({...}, 'Venus::String')
378  
379 # $coercion->{father}
380 # undef
381  
382 # $coercion->{mother}
383 # bless({...}, 'Person')
384  
385 # $coercion->{siblings}
386 # [bless({...}, 'Person'), bless({...}, 'Person'), ...]
387  
388 =back
389  
390 =cut
« Venus/Core.pm ↑ Summary Venus/Role/Explainable.pm »