XLSX/Backend.pm

filter: Statementstmt 98.8% 86 / 87 Branchbran 68.7% 11 / 16 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 94.4% 17 / 18 Podpod 0.0% 0 / 4 totaltotal 88.4% 115 / 130 CC 12 SCAR 9.0
CC
12
Cov
92%
_zip_member_name_for_sheet
5.5
_zip
3.0
_table_info
3.0
CRAP 2.5
« XLSX.pm ↑ Summary
1 package Excel::ValueReader::XLSX::Backend;
2 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use 5.12.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Archive::Zip 1.61 qw(AZ_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Scalar::Util qw/openhandle/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Encode qw/decode/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 #======================================================================
12 # ATTRIBUTES
13 #======================================================================
14 2 has 'frontend' => (is => 'ro', isa => 'Excel::ValueReader::XLSX',
15 required => 1, weak_ref => 1, handles => [qw/formatted_date/]);
16  
17 2 my %lazy_attrs = ( zip => 'Archive::Zip',
18 date_styles => 'ArrayRef',
19 strings => 'ArrayRef',
20 workbook_data => 'HashRef',
21 table_info => 'HashRef',
22 sheet_for_table => 'ArrayRef',
23 );
24  
25 2 ▶while (my ($name, $type) = each %lazy_attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %lazy_attrs) 12 2 14
26 12 has $name => (is => 'ro', isa => $type, builder => "_$name", init_arg => undef, lazy => 1);
27 }
28  
29  
30  
31  
32 #======================================================================
33 # ATTRIBUTE CONSTRUCTORS
34 #======================================================================
35  
36 30 ▶sub _zip {
Subroutine _zip: called CC=3 SCAR=11.0
37 30 my $self = shift;
38  
39 30 my $zip = Archive::Zip->new;
40 30 my $xlsx_source = $self->frontend->xlsx;
41 30 ▶ my ($meth, $source_name) = openhandle($xlsx_source) ? (readFromFileHandle => 'filehandle')
Branch: 1 branch
BranchTrueFalseTotal
&openhandle($xlsx_source) ? : 2 28 30
42 : (read => $xlsx_source);
43 30 my $result = $zip->$meth($xlsx_source);
44 30 ▶ $result == AZ_OK or die "cannot unzip from $source_name";
Branch: 1 branch
BranchTrueFalseTotal
$result == 0 or die "cannot unzip from $source_name" 30 0 30
45  
46 30 return $zip;
47 }
48  
49  
50 4 ▶sub _table_info {
Subroutine _table_info: called CC=3 SCAR=11.0
51 4 my ($self) = @_;
52  
53 4 my %table_info;
54 4 my @table_members = $self->zip->membersMatching(qr[^xl/tables/table\d+\.xml$]);
55 4 foreach my $table_member (map {$_->fileName} @table_members) {
56 24 my ($table_id) = $table_member =~ /table(\d+)\.xml/;
57 24 my $table_xml = $self->_zip_member_contents($table_member);
58 24 my $this_table_info = $self->_parse_table_xml($table_xml); # defined in subclass
59 24 $this_table_info->{id} = $table_id;
60 24 ▶ $this_table_info->{sheet} = $self->sheet_for_table->[$table_id]
Branch: 1 branch
BranchTrueFalseTotal
$this_table_info->{"sheet"} = $self->sheet_for_table->[$table_id] or croak("could not find sheet id for table $table_id") 24 0 24
61 or croak "could not find sheet id for table $table_id";
62  
63 24 $table_info{$this_table_info->{name}} = $this_table_info;
64 }
65  
66 4 return \%table_info;
67 }
68  
69  
70 4 ▶sub _sheet_for_table {
Subroutine _sheet_for_table: called CC=3 SCAR=11.0
71 4 my ($self) = @_;
72  
73 4 my @sheet_for_table;
74 4 my @rel_members = $self->zip->membersMatching(qr[^xl/worksheets/_rels/sheet\d+\.xml\.rels$]);
75 4 foreach my $rel_member (map {$_->fileName} @rel_members) {
76 20 my ($sheet_id) = $rel_member =~ /sheet(\d+)\.xml/;
77 20 my $rel_xml = $self->_zip_member_contents($rel_member);
78 20 my @table_ids = $self->_table_targets($rel_xml); # defined in subclass
79 20 $sheet_for_table[$_] = $sheet_id foreach @table_ids;
80 }
81  
82 4 return \@sheet_for_table;
83 }
84  
85 2 for my $abstract_meth (qw/_date_styles _strings _workbook_data/) {
86 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
87 0 ▶ *{$abstract_meth} = sub {die "->$abstract_meth() should be implemented in subclass"}
Subroutine __ANON__: not called CC=1 SCAR=1.2
88 6 }
89  
90  
91 #======================================================================
92 # METHODS
93 #======================================================================
94  
95  
96 # accessors to workbook data
97 312 ▶sub base_year {shift->workbook_data->{base_year} }
Subroutine base_year: called CC=1 SCAR=0.0
Pod undocumented
98 94 ▶sub sheets {shift->workbook_data->{sheets} }
Subroutine sheets: called CC=1 SCAR=0.0
Pod undocumented
99 8 ▶sub active_sheet {shift->workbook_data->{active_sheet}}
Subroutine active_sheet: called CC=1 SCAR=0.0
Pod undocumented
100  
101  
102 20 ▶sub Excel_builtin_date_formats {
Subroutine Excel_builtin_date_formats: called CC=1 SCAR=0.0
Pod undocumented
103 20 my @numFmt;
104  
105 # source : section 18.8.30 numFmt (Number Format) in ECMA-376-1:2016
106 # Office Open XML File Formats - Fundamentals and Markup Language Reference
107 20 $numFmt[14] = 'mm-dd-yy';
108 20 $numFmt[15] = 'd-mmm-yy';
109 20 $numFmt[16] = 'd-mmm';
110 20 $numFmt[17] = 'mmm-yy';
111 20 $numFmt[18] = 'h:mm AM/PM';
112 20 $numFmt[19] = 'h:mm:ss AM/PM';
113 20 $numFmt[20] = 'h:mm';
114 20 $numFmt[21] = 'h:mm:ss';
115 20 $numFmt[22] = 'm/d/yy h:mm';
116 20 $numFmt[45] = 'mm:ss';
117 20 $numFmt[46] = '[h]:mm:ss';
118 20 $numFmt[47] = 'mmss.0';
119  
120 20 return @numFmt;
121 }
122  
123 198 ▶sub _zip_member_contents {
Subroutine _zip_member_contents: called CC=2 SCAR=7.1
124 198 my ($self, $member) = @_;
125  
126 198 ▶ my $contents = $self->zip->contents($member)
Branch: 1 branch
BranchTrueFalseTotal
my $contents = $self->zip->contents($member) or die "no contents for member $member" 198 0 198
127 or die "no contents for member $member";
128  
129 198 return decode('UTF-8', $contents);
130 }
131  
132 88 ▶sub _zip_member_name_for_sheet {
Subroutine _zip_member_name_for_sheet: called CC=5 SCAR=17.0
133 88 my ($self, $sheet) = @_;
134  
135 # check that sheet name was given
136 88 ▶ $sheet or die "->values(): missing sheet name";
Branch: 1 branch
BranchTrueFalseTotal
$sheet or die "->values(): missing sheet name" 88 0 88
137  
138 # get sheet id
139 88 my $id = $self->sheets->{$sheet};
140 88 ▶ $id //= $sheet if $sheet =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $sheet =~ /^\d+$/u 40 48 88
Condition33%1/3
$id //= $sheet
l!l&&r!l&&!r
0400
MC/DC0%0/2
$id //= $sheet
$id $sheet
Truth table33%1/3
A //= B
A= $idB= $sheet
ABresult
1X1
011
000
141 88 ▶ $id or die "no such sheet: $sheet";
Branch: 1 branch
BranchTrueFalseTotal
$id or die "no such sheet: $sheet" 88 0 88
142  
143 # construct member name for that sheet
144 88 return "xl/worksheets/sheet$id.xml";
145 }
146  
147  
148 2 1;
149  
150 __END__
« XLSX.pm ↑ Summary