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 17     17   141986 use Try::Tiny;
  17         45  
  17         978  
8 17     17   2087 use Module::Load;
  17         4720  
  17         126  
9              
10              
11 17     17   779 use Moo::Role;
  17         39  
  17         139  
12             our $VERSION = '0.12';
13              
14             with 'MooX::Attributes::Shadow::Role';
15              
16             requires 'copy_into';
17              
18 17     17   6341 use namespace::clean;
  17         33  
  17         132  
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 1368     1368   2716 my ( $from, $to ) = ( shift, shift );
40              
41 1368         2655 for my $attr ( @_ ) {
42              
43              
44 2736 100       5779 next unless $from->${ \"has_$attr" };
  2736         10981  
45              
46             try {
47 176 50   176   10576 if ( defined( my $value = $from->$attr ) ) {
48              
49 176         3465 $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 176         1179 };
67              
68             }
69              
70 1368         7140 return;
71             }
72              
73              
74              
75              
76              
77              
78              
79              
80              
81              
82              
83             sub copy_from {
84              
85 435     435 1 7038 $_[1]->copy_into( $_[0] );
86              
87 435         888 return;
88             }
89              
90              
91              
92              
93              
94              
95              
96              
97              
98             sub clone {
99              
100 907     907 1 19546 my $class = ref( $_[0] );
101 907         2816 load $class;
102              
103 907         66482 my $clone = $class->new;
104              
105 907         14675 $_[0]->copy_into( $clone );
106              
107 907         3199 return $clone;
108             }
109              
110              
111              
112              
113              
114              
115              
116              
117              
118              
119             sub new_from_attrs {
120              
121 37     37 1 76 my $class = shift;
122 37         147 load $class;
123              
124 37         2613 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 258     258 1 531 my $contained = shift;
142 258         744 my $hash = pop;
143              
144 258   33     1316 my $container = shift || caller();
145              
146 258         1190 load $contained;
147              
148 258         18761 my $shadowed = $contained->shadowed_attrs( $container );
149              
150 258         9187 my %attr;
151 258         450 while ( my ( $alias, $orig ) = each %{$shadowed} ) {
  774         2388  
152              
153 516 50       1344 $attr{$orig} = $hash->{$alias} if exists $hash->{$alias};
154              
155             }
156              
157 258         7095 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__