blib/lib/Venus/Role/Printable.pm

filter: Statementstmt 95.0% 58 / 61 Branchbran 76.6% 23 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 94.1% 16 / 17 Podpod 76.9% 10 / 13 totaltotal 88.4% 107 / 121 SCAR 27.9
CC
16
Cov
89%
print_json
4.0
print_yaml
4.0
say_json
4.0
CRAP 16.3
« blib/lib/Venus/Role/Throwable.pm ↑ Summary blib/lib/Venus/Box.pm »
1 package Venus::Role::Printable;
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 'fault';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # AUDITS
11  
12 sub AUDIT {
13 97 ▶ my ($self, $from) = @_;
Subroutine AUDIT: called CC=2 SCAR=7.6
Pod undocumented
14  
15 97 ▶ if (!$from->does('Venus::Role::Dumpable')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($from->does("Venus::Role::Dumpable")) 0 97 97
16 0 fault "${self} requires ${from} to consume Venus::Role::Dumpable";
17 }
18  
19 97 return $self;
20 }
21  
22 # METHODS
23  
24 sub print {
25 2 ▶ my ($self, @args) = @_;
Subroutine print: called CC=1 SCAR=0.0
Pod documented
26  
27 2 return $self->printer($self->dump(@args));
28 }
29  
30 sub print_json {
31 2 ▶ my ($self, $method, @args) = @_;
Subroutine print_json: called CC=4 SCAR=13.9
Pod documented
32  
33 2 require Venus::Json;
34  
35 2 ▶ my $value = $method ? scalar($self->$method(@args)) : $self;
Branch: 1 branch
BranchTrueFalseTotal
$method ? : 1 1 2
36  
37 2 require Scalar::Util;
38  
39 2 ▶ if (Scalar::Util::blessed($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value)) 1 1 2
40 1 ▶ $value = $value->value if $value->isa('Venus::Kind');
Branch: 1 branch
BranchTrueFalseTotal
if $value->isa("Venus::Kind") 0 1 1
41 }
42  
43 2 return $self->printer(Venus::Json->new($value)->encode);
44 }
45  
46 sub print_pretty {
47 2 ▶ my ($self, @args) = @_;
Subroutine print_pretty: called CC=1 SCAR=0.0
Pod documented
48  
49 2 return $self->printer($self->dump_pretty(@args));
50 }
51  
52 sub print_string {
53 1 ▶ my ($self, $method, @args) = @_;
Subroutine print_string: called CC=2 SCAR=7.1
Pod documented
54  
55 1 local $_ = $self;
56  
57 1 ▶ return $self->printer($method ? scalar($self->$method(@args)) : $self);
Branch: 1 branch
BranchTrueFalseTotal
$method ? : 0 1 1
58 }
59  
60 sub print_yaml {
61 2 ▶ my ($self, $method, @args) = @_;
Subroutine print_yaml: called CC=4 SCAR=13.9
Pod documented
62  
63 2 require Venus::Yaml;
64  
65 2 ▶ my $value = $method ? scalar($self->$method(@args)) : $self;
Branch: 1 branch
BranchTrueFalseTotal
$method ? : 1 1 2
66  
67 2 require Scalar::Util;
68  
69 2 ▶ if (Scalar::Util::blessed($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value)) 1 1 2
70 1 ▶ $value = $value->value if $value->isa('Venus::Kind');
Branch: 1 branch
BranchTrueFalseTotal
if $value->isa("Venus::Kind") 0 1 1
71 }
72  
73 2 return $self->printer(Venus::Yaml->new($value)->encode);
74 }
75  
76 sub printer {
77 0 ▶ my ($self, @args) = @_;
Subroutine printer: not called CC=1 SCAR=6.9
Pod undocumented
78  
79 0 return CORE::print(STDOUT @args);
80 }
81  
82 sub say {
83 2 ▶ my ($self, @args) = @_;
Subroutine say: called CC=1 SCAR=0.0
Pod documented
84  
85 2 return $self->printer($self->dump(@args), "\n");
86 }
87  
88 sub say_json {
89 2 ▶ my ($self, $method, @args) = @_;
Subroutine say_json: called CC=4 SCAR=13.9
Pod documented
90  
91 2 require Venus::Json;
92  
93 2 ▶ my $value = $method ? scalar($self->$method(@args)) : $self;
Branch: 1 branch
BranchTrueFalseTotal
$method ? : 1 1 2
94  
95 2 require Scalar::Util;
96  
97 2 ▶ if (Scalar::Util::blessed($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value)) 1 1 2
98 1 ▶ $value = $value->value if $value->isa('Venus::Kind');
Branch: 1 branch
BranchTrueFalseTotal
if $value->isa("Venus::Kind") 0 1 1
99 }
100  
101 2 return $self->printer(Venus::Json->new($value)->encode, "\n");
102 }
103  
104 sub say_pretty {
105 2 ▶ my ($self, @args) = @_;
Subroutine say_pretty: called CC=1 SCAR=0.0
Pod documented
106  
107 2 return $self->printer($self->dump_pretty(@args), "\n");
108 }
109  
110 sub say_string {
111 1 ▶ my ($self, $method, @args) = @_;
Subroutine say_string: called CC=2 SCAR=7.1
Pod documented
112  
113 1 local $_ = $self;
114  
115 1 ▶ return $self->printer(($method ? scalar($self->$method(@args)) : $self), "\n");
Branch: 1 branch
BranchTrueFalseTotal
$method ? : 0 1 1
116 }
117  
118 sub say_yaml {
119 2 ▶ my ($self, $method, @args) = @_;
Subroutine say_yaml: called CC=4 SCAR=13.9
Pod documented
120  
121 2 require Venus::Yaml;
122  
123 2 ▶ my $value = $method ? scalar($self->$method(@args)) : $self;
Branch: 1 branch
BranchTrueFalseTotal
$method ? : 1 1 2
124  
125 2 require Scalar::Util;
126  
127 2 ▶ if (Scalar::Util::blessed($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value)) 1 1 2
128 1 ▶ $value = $value->value if $value->isa('Venus::Kind');
Branch: 1 branch
BranchTrueFalseTotal
if $value->isa("Venus::Kind") 0 1 1
129 }
130  
131 2 return $self->printer(Venus::Yaml->new($value)->encode, "\n");
132 }
133  
134 # EXPORTS
135  
136 sub EXPORT {
137 [
138 97 ▶ 'print',
Subroutine EXPORT: called CC=1 SCAR=0.0
Pod undocumented
139 'print_json',
140 'print_pretty',
141 'print_string',
142 'print_yaml',
143 'printer',
144 'say',
145 'say_json',
146 'say_pretty',
147 'say_string',
148 'say_yaml',
149 ]
150 }
151  
152 96 1;
153  
154  
155  
156 =head1 NAME
157  
158 Venus::Role::Printable - Printable Role
159  
160 =cut
161  
162 =head1 ABSTRACT
163  
164 Printable Role for Perl 5
165  
166 =cut
167  
168 =head1 SYNOPSIS
169  
170 package Example;
171  
172 use Venus::Class;
173  
174 with 'Venus::Role::Dumpable';
175 with 'Venus::Role::Printable';
176  
177 attr 'test';
178  
179 sub execute {
180 return [@_];
181 }
182  
183 sub printer {
184 return [@_];
185 }
186  
187 package main;
188  
189 my $example = Example->new(test => 123);
190  
191 # $example->say;
192  
193 =cut
194  
195 =head1 DESCRIPTION
196  
197 This package provides a mechanism for outputting (printing) objects or the
198 return value of a dispatched method call to STDOUT.
199  
200 =cut
201  
202 =head1 METHODS
203  
204 This package provides the following methods:
205  
206 =cut
207  
208 =head2 print
209  
210 print(any @data) (any)
211  
212 The print method prints a stringified representation of the underlying data.
213  
214 I<Since C<0.01>>
215  
216 =over 4
217  
218 =item print example 1
219  
220 package main;
221  
222 my $example = Example->new(test => 123);
223  
224 my $print = $example->print;
225  
226 # bless({test => 123}, 'Example')
227  
228 # 1
229  
230 =back
231  
232 =over 4
233  
234 =item print example 2
235  
236 package main;
237  
238 my $example = Example->new(test => 123);
239  
240 my $print = $example->print('execute', 1, 2, 3);
241  
242 # [bless({test => 123}, 'Example'),1,2,3]
243  
244 # 1
245  
246 =back
247  
248 =cut
249  
250 =head2 print_json
251  
252 print_json(string | coderef $method, any @args) (any)
253  
254 The print_json method prints a JSON representation of the underlying data. This
255 method supports dispatching, i.e. providing a method name and arguments whose
256 return value will be acted on by this method.
257  
258 I<Since C<2.91>>
259  
260 =over 4
261  
262 =item print_json example 1
263  
264 package main;
265  
266 my $example = Example->new(test => 123);
267  
268 my $print_json = $example->print_json;
269  
270 # "{\"test\": 123}"
271  
272 =back
273  
274 =over 4
275  
276 =item print_json example 2
277  
278 package main;
279  
280 my $example = Example->new(test => 123);
281  
282 my $print_json = $example->print_json('execute');
283  
284 # "[{\"test\": 123}]"
285  
286 =back
287  
288 =cut
289  
290 =head2 print_pretty
291  
292 print_pretty(any @data) (any)
293  
294 The print_pretty method prints a stringified human-readable representation of
295 the underlying data.
296  
297 I<Since C<0.01>>
298  
299 =over 4
300  
301 =item print_pretty example 1
302  
303 package main;
304  
305 my $example = Example->new(test => 123);
306  
307 my $print_pretty = $example->print_pretty;
308  
309 # bless({ test => 123 }, 'Example')
310  
311 # 1
312  
313 =back
314  
315 =over 4
316  
317 =item print_pretty example 2
318  
319 package main;
320  
321 my $example = Example->new(test => 123);
322  
323 my $print_pretty = $example->print_pretty('execute', 1, 2, 3);
324  
325 # [
326 # bless({ test => 123 }, 'Example'),
327 # 1,
328 # 2,
329 # 3
330 # ]
331  
332 # 1
333  
334 =back
335  
336 =cut
337  
338 =head2 print_string
339  
340 print_string(string | coderef $method, any @args) (any)
341  
342 The print_string method prints a string representation of the underlying data
343 without using a dump. This method supports dispatching, i.e. providing a
344 method name and arguments whose return value will be acted on by this method.
345  
346 I<Since C<0.09>>
347  
348 =over 4
349  
350 =item print_string example 1
351  
352 package main;
353  
354 my $example = Example->new(test => 123);
355  
356 my $print_string = $example->print_string;
357  
358 # 'Example'
359  
360 # 1
361  
362 =back
363  
364 =cut
365  
366 =head2 print_yaml
367  
368 print_yaml(string | coderef $method, any @args) (any)
369  
370 The print_yaml method prints a YAML representation of the underlying data. This
371 method supports dispatching, i.e. providing a method name and arguments whose
372 return value will be acted on by this method.
373  
374 I<Since C<2.91>>
375  
376 =over 4
377  
378 =item print_yaml example 1
379  
380 package main;
381  
382 my $example = Example->new(test => 123);
383  
384 my $print_yaml = $example->print_yaml;
385  
386 # "---\ntest: 123"
387  
388 =back
389  
390 =over 4
391  
392 =item print_yaml example 2
393  
394 package main;
395  
396 my $example = Example->new(test => 123);
397  
398 my $print_yaml = $example->print_yaml('execute');
399  
400 # "---\n- test: 123"
401  
402 =back
403  
404 =cut
405  
406 =head2 say
407  
408 say(any @data) (any)
409  
410 The say method prints a stringified representation of the underlying data, with
411 a trailing newline.
412  
413 I<Since C<0.01>>
414  
415 =over 4
416  
417 =item say example 1
418  
419 package main;
420  
421 my $example = Example->new(test => 123);
422  
423 my $say = $example->say;
424  
425 # bless({test => 123}, 'Example')\n
426  
427 # 1
428  
429 =back
430  
431 =over 4
432  
433 =item say example 2
434  
435 package main;
436  
437 my $example = Example->new(test => 123);
438  
439 my $say = $example->say;
440  
441 # [bless({test => 123}, 'Example'),1,2,3]\n
442  
443 # 1
444  
445 =back
446  
447 =cut
448  
449 =head2 say_json
450  
451 say_json(string | coderef $method, any @args) (any)
452  
453 The say_json method prints a JSON representation of the underlying data. This
454 method supports dispatching, i.e. providing a method name and arguments whose
455 return value will be acted on by this method, with a trailing newline.
456  
457 I<Since C<2.91>>
458  
459 =over 4
460  
461 =item say_json example 1
462  
463 package main;
464  
465 my $example = Example->new(test => 123);
466  
467 my $say_json = $example->say_json;
468  
469 # "{\"test\": 123}\n"
470  
471 =back
472  
473 =over 4
474  
475 =item say_json example 2
476  
477 package main;
478  
479 my $example = Example->new(test => 123);
480  
481 my $say_json = $example->say_json('execute');
482  
483 # "[{\"test\": 123}]\n"
484  
485 =back
486  
487 =cut
488  
489 =head2 say_pretty
490  
491 say_pretty(any @data) (any)
492  
493 The say_pretty method prints a stringified human-readable representation of the
494 underlying data, with a trailing newline.
495  
496 I<Since C<0.01>>
497  
498 =over 4
499  
500 =item say_pretty example 1
501  
502 package main;
503  
504 my $example = Example->new(test => 123);
505  
506 my $say_pretty = $example->say_pretty;
507  
508 # bless({ test => 123 }, 'Example')\n
509  
510 # 1
511  
512 =back
513  
514 =over 4
515  
516 =item say_pretty example 2
517  
518 package main;
519  
520 my $example = Example->new(test => 123);
521  
522 my $say_pretty = $example->say_pretty;
523  
524 # [
525 # bless({ test => 123 }, 'Example'),
526 # 1,
527 # 2,
528 # 3
529 # ]\n
530  
531 # 1
532  
533 =back
534  
535 =cut
536  
537 =head2 say_string
538  
539 say_string(string | coderef $method, any @args) (any)
540  
541 The say_string method prints a string representation of the underlying data
542 without using a dump, with a trailing newline. This method supports
543 dispatching, i.e. providing a method name and arguments whose return value will
544 be acted on by this method.
545  
546 I<Since C<0.09>>
547  
548 =over 4
549  
550 =item say_string example 1
551  
552 package main;
553  
554 my $example = Example->new(test => 123);
555  
556 my $say_string = $example->say_string;
557  
558 # "Example\n"
559  
560 # 1
561  
562 =back
563  
564 =cut
565  
566 =head2 say_yaml
567  
568 say_yaml(string | coderef $method, any @args) (any)
569  
570 The say_yaml method prints a YAML representation of the underlying data. This
571 method supports dispatching, i.e. providing a method name and arguments whose
572 return value will be acted on by this method, with a trailing newline.
573  
574 I<Since C<2.91>>
575  
576 =over 4
577  
578 =item say_yaml example 1
579  
580 package main;
581  
582 my $example = Example->new(test => 123);
583  
584 my $say_yaml = $example->say_yaml;
585  
586 # "---\ntest: 123\n"
587  
588 =back
589  
590 =over 4
591  
592 =item say_yaml example 2
593  
594 package main;
595  
596 my $example = Example->new(test => 123);
597  
598 my $say_yaml = $example->say_yaml('execute');
599  
600 # "---\n- test: 123\n"
601  
602 =back
603  
604 =cut
605  
606 =head1 AUTHORS
607  
608 Awncorp, C<awncorp@cpan.org>
609  
610 =cut
611  
612 =head1 LICENSE
613  
614 Copyright (C) 2000, Awncorp, C<awncorp@cpan.org>.
615  
616 This program is free software, you can redistribute it and/or modify it under
617 the terms of the Apache license version 2.0.
618  
619 =cut
« blib/lib/Venus/Role/Throwable.pm ↑ Summary blib/lib/Venus/Box.pm »