File Coverage

blib/lib/IPC/PrettyPipe/Format.pm
Criterion Covered Total %
statement 40 43 93.0
branch 4 6 66.6
condition 1 3 33.3
subroutine 10 11 90.9
pod 4 4 100.0
total 59 67 88.0


line stmt bran cond sub pod time code
1             package IPC::PrettyPipe::Format;
2              
3             # ABSTRACT: Format role
4              
5             ## no critic (ProhibitAccessOfPrivateData)
6              
7 20     20   198897 use Try::Tiny;
  20         63  
  20         1269  
8 20     20   2839 use Module::Load;
  20         5822  
  20         178  
9              
10              
11 20     20   1062 use Moo::Role;
  20         57  
  20         143  
12             our $VERSION = '0.13';
13              
14             with 'MooX::Attributes::Shadow::Role';
15              
16             requires 'copy_into';
17              
18 20     20   8575 use namespace::clean;
  20         49  
  20         165  
19              
20             # IS THIS REALLY NEEDED????? this will convert an attribute with a
21             # an undef value into a switch.
22             #
23             # undefined values are the same as not specifying a value at all
24             if ( 0 ) {
25             around BUILDARGS => sub {
26              
27             my ( $orig, $class ) = ( shift, shift );
28              
29             my $attrs = $class->$orig( @_ );
30              
31             delete @{$attrs}{ grep { !defined $attrs->{$_} } keys %$attrs };
32              
33             return $attrs;
34             };
35             }
36              
37             sub _copy_attrs {
38              
39 1779     1779   3851 my ( $from, $to ) = ( shift, shift );
40              
41 1779         3698 for my $attr ( @_ ) {
42              
43              
44 3558 100       8021 next unless $from->${ \"has_$attr" };
  3558         15080  
45              
46             try {
47 198 50   198   12380 if ( defined( my $value = $from->$attr ) ) {
48              
49 198         4305 $to->$attr( $value );
50              
51             }
52              
53             else {
54              
55 0         0 $to->${ \"clear_$attr" };
  0         0  
56              
57             }
58             }
59             catch {
60              
61 0     0   0 croak(
62             "unable to copy into or clear attribute $attr in object of type ",
63             ref $to,
64             ": $_\n"
65             );
66 198         1420 };
67              
68             }
69              
70 1779         9629 return;
71             }
72              
73              
74              
75              
76              
77              
78              
79              
80              
81              
82              
83             sub copy_from {
84              
85 548     548 1 9169 $_[1]->copy_into( $_[0] );
86              
87 548         1255 return;
88             }
89              
90              
91              
92              
93              
94              
95              
96              
97              
98             sub clone {
99              
100 1201     1201 1 25971 my $class = ref( $_[0] );
101 1201         3872 load $class;
102              
103 1201         94314 my $clone = $class->new;
104              
105 1201         20664 $_[0]->copy_into( $clone );
106              
107 1201         4683 return $clone;
108             }
109              
110              
111              
112              
113              
114              
115              
116              
117              
118              
119             sub new_from_attrs {
120              
121 45     45 1 102 my $class = shift;
122 45         216 load $class;
123              
124 45         3516 return $class->new( $class->xtract_attrs( @_ ) );
125             }
126              
127              
128              
129              
130              
131              
132              
133              
134              
135              
136              
137              
138              
139             sub new_from_hash {
140              
141 344     344 1 973 my $contained = shift;
142 344         708 my $hash = pop;
143              
144 344   33     1731 my $container = shift || caller();
145              
146 344         1422 load $contained;
147              
148 344         25370 my $shadowed = $contained->shadowed_attrs( $container );
149              
150 344         12124 my %attr;
151 344         678 while ( my ( $alias, $orig ) = each %{$shadowed} ) {
  1032         3406  
152              
153 688 50       1930 $attr{$orig} = $hash->{$alias} if exists $hash->{$alias};
154              
155             }
156              
157 344         9913 return $contained->new( \%attr );
158             }
159              
160             1;
161              
162             #
163             # This file is part of IPC-PrettyPipe
164             #
165             # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
166             #
167             # This is free software, licensed under:
168             #
169             # The GNU General Public License, Version 3, June 2007
170             #
171              
172             __END__