WebFetch/Data/Record.pm

filter: Statementstmt 29.0% 27 / 93 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 53.3% 8 / 15 Podpod 100% 5 / 5 totaltotal 28.5% 40 / 140 CC 16 SCAR 27.3
CC
16
Cov
23%
__ANON__
30.0
AUTOLOAD
30.0
byname
20.0
CRAP 15.3
« WebFetch/Input/PerlStruct.pm ↑ Summary WebFetch/Input/SiteNews.pm »
1 # WebFetch::Data::Record
2 # ABSTRACT: WebFetch Embedding API data record
3 #
4 # Copyright (c) 2009-2022 Ian Kluft. This program is free software; you can
5 # redistribute it and/or modify it under the terms of the GNU General Public
6 # License Version 3. See https://www.gnu.org/licenses/gpl-3.0-standalone.html
7 #
8  
9 # pragmas to silence some warnings from Perl::Critic
10 ## no critic (Modules::RequireExplicitPackage)
11 # This solves a catch-22 where parts of Perl::Critic want both package and use-strict to be first
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 ## use critic (Modules::RequireExplicitPackage)
16  
17 package WebFetch::Data::Record;
18 1 $WebFetch::Data::Record::VERSION = '0.15.9';
19 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use base qw( WebFetch );
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # define exceptions/errors
24 1 ▶use Exception::Class (
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 'WebFetch::Data::Record::Exception::AutoloadFailure' => {
26 isa => 'WebFetch::TracedException',
27 alias => 'throw_autoload_fail',
28 description => "AUTOLOAD failed to handle function call",
29 },
30  
31 1 );
32  
33 # no user-servicable parts beyond this point
34  
35 1 our $AUTOLOAD;
36  
37 # initialization
38 sub init
39 0 ▶{
Subroutine init: not called CC=3 SCAR=24.8
Pod documented
40 0 my ( $self, $obj, $num, @args ) = @_;
41  
42 # save parameters
43 0 $self->{obj} = $obj;
44 0 $self->{num} = $num;
45 0 $self->{recref} = $self->{obj}{records}[ $self->{num} ];
46  
47 # signal WebFetch that Data subclasses do not provide a fetch function
48 0 $self->{no_fetch} = 1;
49 0 $self->SUPER::init(@args);
50  
51 # make accessor functions
52 0 my $class = ref($self);
53 0 foreach my $field ( @{ $self->{obj}{fields} } ) {
54 0 $class->mk_field_accessor($field);
55 }
56 0 foreach my $field ( keys %{ $self->{obj}{wk_names} } ) {
57 0 $class->mk_field_accessor($field);
58 }
59  
60 0 return $self;
61 }
62  
63 # shortcut function to top-level WebFetch object data
64 0 ▶sub data { my @args = @_; return $args[0]->{obj}; }
Subroutine data: not called CC=1 SCAR=6.9
Pod documented
65  
66 # get a field by number
67 sub bynum
68 0 ▶{
Subroutine bynum: not called CC=1 SCAR=6.9
Pod documented
69 0 my $self = shift;
70 0 my $f = shift;
71  
72 0 WebFetch::debug "bynum $f";
73 0 return $self->{recref}[$f];
74 }
75  
76 # get a field by name
77 sub byname
78 0 ▶{
Subroutine byname: not called CC=4 SCAR=30.0
Pod documented
79 0 my $self = shift;
80 0 my $fname = shift;
81 0 my $obj = $self->{obj};
82 0 my $f;
83  
84 0 ▶ WebFetch::debug "byname " . ( ( defined $fname ) ? $fname : "undef" );
Branch: 1 branch
BranchTrueFalseTotal
defined $fname ? : 0 0 0
85 0 ▶ ( defined $fname ) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $fname or return 0 0 0
86 0 ▶ if ( exists $obj->{findex}{$fname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $obj->{"findex"}{$fname}) 0 0 0
87 0 $f = $obj->{findex}{$fname};
88 0 return $self->{recref}[$f];
89 }
90 0 return;
91 }
92  
93 # make field accessor/mutator functions
94 sub mk_field_accessor
95 0 ▶{
Subroutine mk_field_accessor: not called CC=3 SCAR=23.1
Pod documented
96 0 my ( $class, @args ) = @_;
97 0 foreach my $name (@args) {
98 0 ▶ $class->can($name) and next; # skip if function exists!
Branch: 1 branch
BranchTrueFalseTotal
$class->can($name) and next 0 0 0
99  
100 # make a closure which keeps value of $name from this call
101 # keep generic so code can use more than one data type per run
102 ## no critic (TestingAndDebugging::ProhibitNoStrict)
103 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
104 0 ▶ *{ $class . "::" . $name } = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
105 0 my $self = shift;
106 0 my $value = shift;
107 0 my $obj = $self->{obj};
108 0 my $recref = $self->{recref};
109 0 my $f;
110 0 ▶ if ( exists $obj->{findex}{$name} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $obj->{'findex'}{$name}) { } 0 0 0
elsif (exists $obj->{'wk_names'}{$name}) { } 0 0 0
111 0 $f = $obj->{findex}{$name};
112 0 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
113 0 my $tmp = $recref->[$f];
114 0 $recref->[$f] = $value;
115 0 return $tmp;
116 } else {
117 0 return $recref->[$f];
118 }
119 } elsif ( exists $obj->{wk_names}{$name} ) {
120 0 my $wk = $obj->{wk_names}{$name};
121 0 $f = $obj->{findex}{$wk};
122 0 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
123 0 my $tmp = $recref->[$f];
124 0 $recref->[$f] = $value;
125 0 return $tmp;
126 } else {
127 0 return $recref->[$f];
128 }
129 } else {
130 0 return;
131 }
132 0 };
133 }
134 0 return;
135 }
136  
137 # AUTOLOAD function to provide field accessors/mutators
138 ## no critic (ClassHierarchies::ProhibitAutoloading)
139 sub AUTOLOAD
140 0 ▶{
Subroutine AUTOLOAD: not called CC=5 SCAR=34.0
141 0 my ( $self, @args ) = @_;
142 0 ▶ my $type = ref($self) or throw_autoload_fail "self is not an object";
Branch: 1 branch
BranchTrueFalseTotal
my $type = ref $self or throw_autoload_fail("self is not an object") 0 0 0
143  
144 0 my $name = $AUTOLOAD;
145 0 $name =~ s/.*://x; # strip fully-qualified portion, just want function
146  
147 # decline all-caps names - reserved for special Perl functions
148 0 ▶ ( $name =~ /^[A-Z]+$/x ) and return;
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^[A-Z]+$/x and return 0 0 0
149  
150 0 WebFetch::debug __PACKAGE__ . "::AUTOLOAD $name";
151 0 ▶ if ( ( exists $self->{obj}{findex}{$name} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'obj'}{'findex'}{$name} or exists $self->{'obj'}{'wk_names'}{$name}) { } 0 0 0
Condition0%0/3
exists $self->{"obj"}{"findex"}{$name} or exists $self->{"obj"}{"wk_names"}{$name}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $self->{"obj"}{"findex"}{$name} or exists $self->{"obj"}{"wk_names"}{$name}
exists $self->{"obj"}{"findex"}{$name} exists $self->{"obj"}{"wk_names"}{$name}
Truth table0%0/3
A or B
A= exists $self->{"obj"}{"findex"}{$name}B= exists $self->{"obj"}{"wk_names"}{$name}
ABresult
1X1
011
000
152 or ( exists $self->{obj}{wk_names}{$name} ) )
153 {
154 0 $type->mk_field_accessor($name);
155 0 return $self->$name(@args);
156 } else {
157 0 throw_autoload_fail "no such function or field $name";
158 }
159 }
160  
161 1 1;
162  
163 __END__
« WebFetch/Input/PerlStruct.pm ↑ Summary WebFetch/Input/SiteNews.pm »