File Coverage

blib/lib/Data/Record/Serialize/Role/Sink/Stream.pm
Criterion Covered Total %
statement 40 41 97.5
branch 14 18 77.7
condition 9 18 50.0
subroutine 9 9 100.0
pod 1 1 100.0
total 73 87 83.9


line stmt bran cond sub pod time code
1             package Data::Record::Serialize::Role::Sink::Stream;
2              
3             # ABSTRACT: output encoded data to a stream.
4              
5 9     9   200760 use v5.12;
  9         56  
6 9     9   64 use Scalar::Util;
  9         20  
  9         578  
7 9     9   465 use Moo::Role;
  9         13237  
  9         604  
8              
9 9     9   6522 use Data::Record::Serialize::Error { errors => [ '::create', '::parameter', '::internal' ] }, -all;
  9         24  
  9         147  
10 9     9   3188 use Types::Standard qw[ Bool ];
  9         250631  
  9         91  
11              
12             our $VERSION = '2.02';
13              
14 9     9   34301 use IO::File;
  9         90415  
  9         1782  
15              
16 9     9   482 use namespace::clean;
  9         13882  
  9         110  
17              
18             ## no critic( NamingConventions::ProhibitAmbiguousNames )
19             ## no critic( Subroutines::ProhibitBuiltinHomonyms )
20              
21              
22              
23              
24              
25              
26              
27              
28              
29              
30              
31              
32              
33              
34              
35              
36              
37              
38              
39              
40              
41              
42              
43              
44              
45              
46              
47              
48              
49              
50             has output => (
51             is => 'ro',
52             default => q{-},
53             isa => sub {
54             defined $_[0]
55             or error( '::parameter', q{'output' parameter must be defined} );
56             my $ref = ref $_[0];
57             return if $ref eq 'GLOB' or $ref eq 'SCALAR';
58              
59             if ( $ref eq q{} ) {
60             $ref = ref( \$_[0] ); # turn plain *STDOUT into \*STDOUT
61             return if $ref eq 'GLOB';
62             return if length $_[0];
63             error( '::parameter', q{string 'output' parameter must not be empty} );
64             }
65              
66             return
67             if Scalar::Util::blessed $_[0]
68             and $_[0]->isa( 'IO::Handle' ) || $_[0]->isa( 'FileHandle' );
69             error( '::parameter', q{illegal value for 'output' parameter} );
70             },
71             );
72              
73              
74              
75              
76              
77              
78              
79             has fh => (
80             is => 'lazy',
81             init_arg => undef,
82             clearer => 1,
83             predicate => 1,
84             );
85              
86              
87              
88              
89              
90              
91              
92             has _passed_fh => (
93             is => 'rwp',
94             init_arg => undef,
95             default => 1,
96             );
97              
98             sub _build_fh {
99 18     18   1211 my $self = shift;
100              
101 18         99 my $output = $self->output;
102 18         58 my $ref = ref $output;
103              
104             # filename
105 18 100       80 if ( $ref eq q{} ) {
106 3 100       25 return $output if ref( \$output ) eq 'GLOB';
107 2 50       8 return \*STDOUT if $output eq q{-};
108              
109 2         9 $self->_set__passed_fh( 0 );
110 2 100       15 if ( $self->create_output_dir ) {
111 1         1422 require Path::Tiny;
112 1         18912 my $dir = Path::Tiny::path( $output )->parent;
113 1 50       201 eval { $dir->mkdir; } or error( '::create', "unable to create output directory '$dir': $@" );
  1         6  
114             }
115             return (
116 2   33     679 IO::File->new( $output, 'w' )
117             or error( '::create', "unable to create output file: '$output'" ) );
118             }
119              
120 15 50 33     156 return $output
      66        
      66        
121             if $ref eq 'GLOB'
122             or Scalar::Util::blessed( $output )
123             && ( $output->isa( 'IO::Handle' ) || $output->isa( 'FileHandle' ) );
124              
125 12         97 $self->_set__passed_fh( 0 );
126             return (
127 12 50 33     157 IO::File->new( $output, 'w' )
128             or error( '::create', q{unable to open scalar for output} ) ) if $ref eq 'SCALAR';
129              
130 0         0 error( '::internal', q{can't get here} );
131             }
132              
133              
134              
135              
136              
137              
138              
139              
140             has create_output_dir => (
141             is => 'ro',
142             isa => Bool,
143             default => 0,
144             );
145              
146              
147              
148              
149              
150              
151              
152              
153              
154              
155              
156              
157              
158              
159              
160              
161              
162              
163              
164              
165              
166              
167              
168              
169              
170             sub close {
171 22     22 1 177 my ( $self, $in_global_destruction ) = @_;
172              
173             # don't bother closing the FH in global destruction (it'll be done
174             # on its own) or if we were passed a file handle in the output
175             # attribute.
176 22 100 66     263 return if $in_global_destruction or $self->_passed_fh;
177              
178             # fh is lazy, so the object may close without every using it, so
179             # don't inadvertently create it.
180 12 100       301 $self->fh->close if $self->has_fh;
181 12         855 $self->clear_fh;
182             }
183              
184             1;
185              
186             #
187             # This file is part of Data-Record-Serialize
188             #
189             # This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.
190             #
191             # This is free software, licensed under:
192             #
193             # The GNU General Public License, Version 3, June 2007
194             #
195              
196             __END__