lib/BoutrosLab/TSVStream/IO/Role/Fixed.pm

filter: Statementstmt 96.6% 58 / 60 Branchbran 64.2% 9 / 14 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 3 totaltotal 82.6% 86 / 104 CC 12 SCAR 11.3
CC
12
Cov
86%
_check_dups
7.5
__ANON__
5.0
reader
2.1
CRAP 3.1
« lib/BoutrosLab/TSVStream/IO/Role/Base/Fixed.pm ↑ Summary lib/BoutrosLab/TSVStream/Format/VCF/Types.pm »
1 package BoutrosLab::TSVStream::IO::Role::Fixed;
2  
3 # safe Perl
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶use Moose::Util qw(find_meta);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 ▶use BoutrosLab::TSVStream::IO::Reader::Fixed;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use BoutrosLab::TSVStream::IO::Writer::Fixed;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 BoutrosLab::TSVStream::IO::Role::Fixed
19  
20 =head1 SYNOPSIS
21  
22 # in a Moose class definition...
23 use MooseX::ClassAttribute
24  
25 class_has '_fields' => (
26 is => 'ro',
27 isa => 'ArrayRef',
28 default => sub { [qw(foo bar)] }
29 );
30  
31 # # or, without using MooseX::ClassAttribute
32 # my $_fields = [ qw(foo bar) ];
33 # sub _fields { return $_fields }
34  
35 with 'BoutrosLab::TSVStream::IO::Role::Fixed';
36  
37 has 'foo' => ( ... );
38 has 'bar' => ( ... );
39 ...
40  
41 =head1 DESCRIPTION
42  
43 This role provides methods to create a file/iostream reader/writer
44 for a class, using a stream of lines with tab separated fields for
45 each record, converting to/from an object of the class. Usually,
46 the stream will start with an initial line that has the field names
47 as a tab separated record.
48  
49 This role is also provided a BUILDARGS wrapper that alows the
50 constructor to be given one element pair in the parameter list:
51 (field_values=>[val1,val2,...]) instead of providing each field
52 explicitly by name as (fld1=>val1, fld2=>val2, ...). In such a
53 case, the values in the B<field_values> array must be in the same
54 order as they are listed in the B<_fields> class attribute.
55  
56 =cut
57  
58 has [qw(_tsvinternal_pre_header _tsvinternal_pre_comments _tsvinternal_post_comments)] => (
59 is => 'ro',
60 isa => 'ArrayRef[Str]',
61 468 ▶ default => sub { [] }
Subroutine __ANON__: called CC=1 SCAR=0.0
62 8 );
63  
64 32 ▶sub _reader_class {
Subroutine _reader_class: called CC=1 SCAR=0.0
65 32 return 'BoutrosLab::TSVStream::IO::Reader::Fixed';
66 }
67  
68 10 ▶sub _writer_class {
Subroutine _writer_class: called CC=1 SCAR=0.0
69 10 return 'BoutrosLab::TSVStream::IO::Writer::Fixed';
70 }
71  
72 153 ▶sub _hashlist_opt_attr {
Subroutine _hashlist_opt_attr: called CC=2 SCAR=6.9
73 153 my $self = shift;
74 153 my $attr = shift;
75 153 my $can = $self->can($attr);
76 153 ▶ return $can ? %{ $self->$attr } : ();
Branch: 1 branch
BranchTrueFalseTotal
$can ? : 16 137 153
77 }
78  
79 143 ▶sub reader {
Subroutine reader: called CC=2 SCAR=7.6
Pod undocumented
80 143 my $self = shift;
81 143 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
01430
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
82 143 return $self->_reader_class()
83 ->new( { $self->_hashlist_opt_attr('_reader_args'), @_, class => $class } );
84 }
85  
86 10 ▶sub writer {
Subroutine writer: called CC=2 SCAR=7.6
Pod undocumented
87 10 my $self = shift;
88 10 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
0100
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
89 10 return $self->_writer_class()
90 ->new( { $self->_hashlist_opt_attr('_writer_args'), @_, class => $class } );
91 }
92  
93 156 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
94 156 my $orig = shift;
95 156 my $class = shift;
96 156 ▶ my $arg = ref($_[0]) ? $_[0] : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 84 72 156
97  
98 156 ▶ if (my $field_values = delete $arg->{field_values}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $field_values = delete $arg->{"field_values"}) 53 103 156
99 53 my @v = @$field_values;
100 53 $arg->{$_} = shift @v for @{ $class->_fields };
101 53 ▶ $arg->{dyn_values} = \@v if scalar(@v);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @v 0 53 53
102 }
103 156 $class->$orig( $arg );
104 8 };
105  
106 55 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
107 55 my $self = shift;
108 55 $self->_check_dups( @{ $self->_fields } );
109 }
110  
111 139 ▶sub _check_dups {
Subroutine _check_dups: called CC=5 SCAR=20.1
112 139 my $self = shift;
113 139 my %seen;
114 my @dups;
115 139 for my $hdr (@_) {
116 305 ▶ push @dups, $hdr if $seen{$hdr}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$hdr}++ 0 305 305
117 }
118 139 ▶ if (@dups) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dups) 0 139 139
119 0 ▶ my $s = (@dups == 1) ? '' : 's';
Branch: 1 branch
BranchTrueFalseTotal
@dups == 1 ? : 0 0 0
120 0 croak "field name$s ("
121 . join( ', ', @dups)
122 . ") seen multiple times in headers ("
123 . join( ', ', @_)
124 . ")";
125 }
126 }
127  
128  
129 =head1 AUTHOR
130  
131 John Macdonald - Boutros Lab
132  
133 =head1 ACKNOWLEDGEMENTS
134  
135 Paul Boutros, Phd, PI - Boutros Lab
136  
137 The Ontario Institute for Cancer Research
138  
139 =cut
140  
141 8 1;
142  
« lib/BoutrosLab/TSVStream/IO/Role/Base/Fixed.pm ↑ Summary lib/BoutrosLab/TSVStream/Format/VCF/Types.pm »