RDF/NS.pm

filter: Statementstmt 85.1% 132 / 155 Branchbran 62.7% 59 / 94 Conditioncond 70.0% 14 / 20 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 97.2% 35 / 36 Podpod 70.0% 14 / 20 totaltotal 77.3% 260 / 336 CC 60 SCAR 29.2
CC
60
Cov
76%
UPDATE
110.0
new
19.5
SET
12.3
CRAP 18.6
↑ Summary App/rdfns.pm »
1 package RDF::NS;
2 6 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 our $VERSION = '20230619';
7  
8 6 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use File::ShareDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use RDF::SN;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 our $AUTOLOAD;
14 6 our $FORMATS = qr/ttl|n(otation)?3|sparql|xmlns|txt|beacon|json/;
15  
16 6 our $DATE_REGEXP = qr/^([0-9]{4})-?([0-9][0-9])-?([0-9][0-9])$/;
17  
18 28 ▶sub new {
Subroutine new: called CC=19 SCAR=29.7
Pod documented
19 28 ▶ my $class = ref( $_[0] ) ? ref(shift) : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 28 28
20 28 ▶ my $from = @_ % 2 ? shift : 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 18 10 28
21 28 my %options = @_;
22 28 ▶ my $at = $options{at} || 'any';
Condition100%2/2
$options{'at'} || 'any'
l!l
127
MC/DC100%1/1
$options{'at'} || 'any'
$options{'at'}
Truth table100%2/2
A
A= $options{'at'}
Aresult
11
00
23 28 my $warn = $options{'warn'};
24 28 ▶ $from = $options{from} if exists $options{from};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"from"} 0 28 28
25 28 ▶ $from = 'any' if !$from or $from eq 1;
Branch: 1 branch
BranchTrueFalseTotal
if not $from or $from eq 1 10 18 28
Condition66%2/3
not $from or $from eq 1
l!l&&r!l&&!r
01018
MC/DC50%1/2
not $from or $from eq 1
not $from $from eq 1
Truth table66%2/3
not(A) or B
A= not $fromB= $from eq 1
ABresult
1X1
011
000
26  
27 28 ▶ if ( ( ref($from) || '' ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $from || "") eq "HASH") 1 27 28
Condition100%2/2
ref $from || ''
l!l
127
MC/DC100%1/1
ref $from || ''
ref $from
Truth table100%2/2
A
A= ref $from
Aresult
11
00
28 1 my $self = bless $from, $class;
29 1 foreach my $prefix ( keys %$self ) {
30 2 ▶ unless ( $self->SET( $prefix => $self->{$prefix}, $warn ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->SET($prefix, $self->{$prefix}, $warn)) 1 1 2
31 1 delete $self->{$prefix};
32 }
33 }
34 1 return $self;
35 }
36  
37 27 ▶ if ( $from =~ $DATE_REGEXP ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($from =~ /$RDF::NS::DATE_REGEXP/) { } 16 11 27
elsif ($at =~ /$RDF::NS::DATE_REGEXP/) { } 1 10 11
elsif (not $at =~ /any/) { } 0 10 10
38 16 $at = "$1$2$3";
39 16 $from = 'any';
40 }
41 elsif ( $at =~ $DATE_REGEXP ) {
42 1 $at = "$1$2$3";
43 }
44 elsif ( $at !~ 'any' ) {
45 0 croak "RDF::NS expects 'any', '1' or a date as YYYY-MM-DD";
46 }
47  
48 27 my $self = bless {}, $class;
49 27 my $fh = $self->DATA($from);
50 27 foreach (<$fh>) {
51 46930 chomp;
52 46930 ▶ next if /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if /^#/ 0 46930 46930
53 46930 my ( $prefix, $namespace, $date ) = split "\t", $_;
54 46930 ▶ last if $date and $at ne 'any' and $date > $at;
Branch: 1 branch
BranchTrueFalseTotal
if $date and $at ne "any" and $date > $at 17 46913 46930
Condition83%5/6
$date and $at ne "any" and $date > $at
!ll&&!rl&&r
322501466317
$date and $at ne "any"
!ll&&!rl&&r
03225014680
MC/DC66%2/3
$date and $at ne "any" and $date > $at
$date $at ne "any" $date > $at
Truth table75%3/4
A and B and C
A= $dateB= $at ne "any"C= $date > $at
ABCresult
0XX0
10X0
1100
1111
55  
56 46913 $self->SET( $prefix => $namespace, $warn );
57 }
58 27 close($fh);
59  
60 27 $self;
61 }
62  
63 27 ▶sub DATA { # TODO: document
Subroutine DATA: called CC=5 SCAR=17.5
Pod undocumented
64 27 my ( $self, $from ) = @_;
65 27 ▶ $from = File::ShareDir::dist_file( 'RDF-NS', "prefix.cc" )
Branch: 1 branch
BranchTrueFalseTotal
if ($from // "any") eq "any" 27 0 27
Condition50%1/2
$from // "any"
l!l
270
MC/DC0%0/1
$from // "any"
$from
Truth table50%1/2
A
A= $from
Aresult
11
00
66 if ( $from // 'any' ) eq 'any';
67 27 ▶ croak "prefix file or date not found: $from"
Branch: 1 branch
BranchTrueFalseTotal
unless -f $from 0 27 27
68 unless -f $from;
69 27 ▶ open( my $fh, '<', $from ) or croak "failed to open $from";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $from or croak("failed to open $from") 27 0 27
70 27 $fh;
71 }
72  
73 46915 ▶sub SET {
Subroutine SET: called CC=7 SCAR=25.1
Pod documented
74 46915 my ( $self, $prefix, $namespace, $warn ) = @_;
75  
76 46915 ▶ if ( $prefix =~ /^(isa|can|new|uri)$/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($prefix =~ /^(isa|can|new|uri)$/) { } 0 46915 46915
elsif ($prefix =~ /^[a-z][a-z0-9]*$/) { } 46914 1 46915
elsif ($warn) { } 0 1 1
77 0 ▶ carp "Cannot support prefix '$prefix'" if $warn;
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 0 0
78 }
79 elsif ( $prefix =~ /^[a-z][a-z0-9]*$/ ) {
80 46914 ▶ if ( $namespace =~ /^[a-z][a-z0-9]*:[^"<>]*$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($namespace =~ /^[a-z][a-z0-9]*:[^"<>]*$/) { } 46914 0 46914
elsif ($warn) { } 0 0 0
81 46914 $self->{$prefix} = $namespace;
82 46914 return 1;
83 }
84 elsif ($warn) {
85 0 carp "Skipping invalid $prefix namespace $namespace";
86 }
87 }
88 elsif ($warn) {
89 0 carp "Skipping unusual prefix '$prefix'";
90 }
91  
92 1 return;
93 }
94  
95 6 *LOAD = *new;
96  
97 9 ▶sub COUNT {
Subroutine COUNT: called CC=1 SCAR=0.0
Pod undocumented
98 9 scalar keys %{ $_[0] };
99 }
100  
101 13 ▶sub FORMAT {
Subroutine FORMAT: called CC=5 SCAR=16.2
Pod undocumented
102 13 my $self = shift;
103 13 ▶ my $format = shift || "";
Condition100%2/2
shift() || ''
l!l
112
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
104 13 ▶ $format = 'TTL' if $format =~ /^n(otation)?3$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $format =~ /^n(otation)?3$/i 0 13 13
105 13 ▶ if ( lc($format) =~ $FORMATS ) {
Branch: 2 branches
BranchTrueFalseTotal
if (lc($format) =~ /$RDF::NS::FORMATS/) { } 1 12 13
elsif ($format eq '') { } 12 0 12
106 1 $format = uc($format);
107 1 $self->$format(@_);
108 }
109 elsif ( $format eq "" ) {
110 12 ▶ $self->MAP( sub { $self->{$_} }, @_ );
Subroutine __ANON__: called CC=1 SCAR=0.0
111 }
112 }
113  
114 3 ▶sub PREFIX {
Subroutine PREFIX: called CC=3 SCAR=11.0
Pod documented
115 3 my ( $self, $uri ) = @_;
116 3 foreach my $prefix ( sort keys %$self ) {
117 1308 ▶ return $prefix if $uri eq $self->{$prefix};
Branch: 1 branch
BranchTrueFalseTotal
if $uri eq $self->{$prefix} 2 1306 1308
118 }
119 1 return;
120 }
121  
122 1 ▶sub PREFIXES {
Subroutine PREFIXES: called CC=3 SCAR=11.0
Pod documented
123 1 my ( $self, $uri ) = @_;
124 1 my @prefixes;
125 1 ▶ while ( my ( $prefix, $namespace ) = each %$self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$self) 698 1 699
126 698 ▶ push @prefixes, $prefix if $uri eq $namespace;
Branch: 1 branch
BranchTrueFalseTotal
if $uri eq $namespace 2 696 698
127 }
128 1 return ( sort(@prefixes) );
129 }
130  
131 21 ▶sub REVERSE {
Subroutine REVERSE: called CC=1 SCAR=0.0
Pod documented
132 21 RDF::SN->new( $_[0] );
133 }
134  
135 5 ▶sub TTL {
Subroutine TTL: called CC=1 SCAR=0.0
Pod documented
136 5 my $self = shift;
137 5 ▶ $self->MAP( sub { "\@prefix $_: <" . $self->{$_} . "> ." }, @_ );
Subroutine __ANON__: called CC=1 SCAR=0.0
138 }
139  
140 5 ▶sub SPARQL {
Subroutine SPARQL: called CC=1 SCAR=0.0
Pod documented
141 5 my $self = shift;
142 5 ▶ $self->MAP( sub { "PREFIX $_: <" . $self->{$_} . ">" }, @_ );
Subroutine __ANON__: called CC=1 SCAR=0.0
143 }
144  
145 6 ▶sub XMLNS {
Subroutine XMLNS: called CC=1 SCAR=0.0
Pod documented
146 6 my $self = shift;
147 6 ▶ $self->MAP( sub { "xmlns:$_=\"" . $self->{$_} . "\"" }, @_ );
Subroutine __ANON__: called CC=1 SCAR=0.0
148 }
149  
150 4 ▶sub TXT {
Subroutine TXT: called CC=1 SCAR=0.0
Pod documented
151 4 my $self = shift;
152 4 ▶ $self->MAP( sub { "$_\t" . $self->{$_} }, @_ );
Subroutine __ANON__: called CC=1 SCAR=0.0
153 }
154  
155 1 ▶sub JSON {
Subroutine JSON: called CC=1 SCAR=0.0
Pod undocumented
156 1 my $self = shift;
157 1 ▶ $self->MAP( sub { "\"$_\": \"" . $self->{$_} . "\"" }, @_ );
Subroutine __ANON__: called CC=1 SCAR=0.0
158 }
159  
160 4 ▶sub BEACON {
Subroutine BEACON: called CC=1 SCAR=0.0
Pod documented
161 4 my $self = shift;
162 4 ▶ $self->MAP( sub { "#PREFIX: " . $self->{$_} }, @_ );
Subroutine __ANON__: called CC=1 SCAR=0.0
163 }
164  
165 2 ▶sub SELECT {
Subroutine SELECT: called CC=1 SCAR=0.0
Pod documented
166 2 my $self = shift;
167 2 ▶ $self->MAP( sub { $_ => $self->{$_} }, @_ );
Subroutine __ANON__: called CC=1 SCAR=0.0
168 }
169  
170 # functional programming rulez!
171 39 ▶sub MAP {
Subroutine MAP: called CC=3 SCAR=11.0
Pod documented
172 39 my $self = shift;
173 39 my $code = shift;
174 my @ns =
175 @_
176 39 ▶ ? ( grep { $self->{$_} } map { split /[|, ]+/ } @_ )
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 39 0 39
177 : keys %$self;
178 39 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 34 5 39
179 34 return map { $code->() } sort @ns;
180 }
181 else {
182 5 local $_ = $ns[0];
183 5 return $code->();
184 }
185 }
186  
187 21 ▶sub GET {
Subroutine GET: called CC=1 SCAR=0.0
Pod documented
188 21 $_[1];
189 }
190  
191 5 ▶sub BLANK {
Subroutine BLANK: called CC=1 SCAR=0.0
Pod undocumented
192 }
193  
194 6 *URI = *uri;
195  
196 8 ▶sub uri {
Subroutine uri: called CC=7 SCAR=19.7
Pod documented
197 8 my $self = shift;
198 8 ▶ return $1 if $_[0] =~ /^<([a-zA-Z][a-zA-Z+.-]*:.+)>$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^<([a-zA-Z][a-zA-Z+.-]*:.+)>$/ 1 7 8
199 7 ▶ return $self->BLANK( $_[0] ) if $_[0] =~ /^_(:.*)?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^_(:.*)?$/ 3 4 7
200 4 ▶ return unless shift =~ /^([a-z][a-z0-9]*)?([:_]([^:]+))?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless (shift()) =~ /^([a-z][a-z0-9]*)?([:_]([^:]+))?$/ 0 4 4
201 4 ▶ my $ns = $self->{ defined $1 ? $1 : '' };
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 3 1 4
202 4 ▶ return unless defined $ns;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ns 0 4 4
203 4 ▶ return $self->GET($ns) unless $3;
Branch: 1 branch
BranchTrueFalseTotal
unless $3 0 4 4
204 4 return $self->GET( $ns . $3 );
205 }
206  
207 52 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=7 SCAR=19.5
208 52 my $self = shift;
209 52 ▶ return unless $AUTOLOAD =~ /^.*::([a-z][a-z0-9]*)?(_([^:]+)?)?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $RDF::NS::AUTOLOAD =~ /^.*::([a-z][a-z0-9]*)?(_([^:]+)?)?$/ 29 23 52
210 23 ▶ return $self->BLANK( defined $3 ? "_:$3" : '_' ) unless $1;
Branch: 2 branches
BranchTrueFalseTotal
unless $1 2 21 23
defined $3 ? : 1 1 2
211 21 ▶ my $ns = $self->{$1} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $ns = $self->{$1} or return 21 0 21
212 21 ▶ my $local = defined $3 ? $3 : shift;
Branch: 1 branch
BranchTrueFalseTotal
defined $3 ? : 7 14 21
213 21 ▶ return $self->GET($ns) unless defined $local;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $local 12 9 21
214 9 return $self->GET( $ns . $local );
215 }
216  
217 0 ▶sub UPDATE {
Subroutine UPDATE: not called CC=10 SCAR=47.0
Pod undocumented
218 0 my ( $self, $file, $date ) = @_;
219  
220 0 ▶ croak "RDF::NS expects a date as YYYY-MM-DD"
Branch: 1 branch
BranchTrueFalseTotal
unless $date and $date =~ /$RDF::NS::DATE_REGEXP/ 0 0 0
Condition0%0/3
$date and $date =~ /$RDF::NS::DATE_REGEXP/
!ll&&!rl&&r
000
MC/DC0%0/2
$date and $date =~ /$RDF::NS::DATE_REGEXP/
$date $date =~ /$RDF::NS::DATE_REGEXP/
Truth table0%0/3
A and B
A= $dateB= $date =~ /$RDF::NS::DATE_REGEXP/
ABresult
0X0
100
111
221 unless $date and $date =~ $DATE_REGEXP;
222 0 $date = "$1$2$3";
223  
224 0 my $old = RDF::NS->new($file);
225 0 my ( @create, @update, @delete );
226  
227 0 ▶ open( my $fh, '>>', $file ) or croak "failed to open $file";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>", $file or croak("failed to open $file") 0 0 0
228 0 my @lines;
229  
230 0 ▶ while ( my ( $prefix, $namespace ) = each %$self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$self) 0 0 0
231 0 ▶ if ( !exists $old->{$prefix} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $old->{$prefix}) { } 0 0 0
elsif ($old->{$prefix} ne $namespace) { } 0 0 0
232 0 push @create, $prefix;
233 }
234 elsif ( $old->{$prefix} ne $namespace ) {
235 0 push @update, $prefix;
236 }
237 else {
238 0 next;
239 }
240 0 push @lines, "$prefix\t$namespace";
241 }
242 0 ▶ while ( my ( $prefix, $namespace ) = each %$old ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$old) 0 0 0
243 0 ▶ if ( !exists $self->{$prefix} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$prefix}) 0 0 0
244 0 push @delete, $prefix;
245 }
246 }
247  
248 0 print $fh "$_\t$date\n" for sort @lines;
249 0 close $fh;
250  
251 return {
252 0 create => [ sort @create ],
253 update => [ sort @update ],
254 delete => [ sort @delete ],
255 };
256 }
257  
258 6 1;
259 __END__
↑ Summary App/rdfns.pm »