Attean/Literal.pm

filter: Statementstmt 97.7% 88 / 90 Branchbran 83.3% 15 / 18 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 22 / 22 Podpod 100% 2 / 2 totaltotal 94.3% 134 / 142 CC 12 SCAR 8.3
CC
12
Cov
95%
__ANON__
4.0
_ntriples_string
4.0
__ANON__
4.0
CRAP 2.3
« Test/Attean/MutableQuadStore.pm ↑ Summary Attean/ListIterator.pm »
1 50 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 50 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 Attean::Literal - RDF Literals
7  
8 =head1 VERSION
9  
10 This document describes Attean::Literal version 0.040
11  
12 =head1 SYNOPSIS
13  
14 use v5.14;
15 use Attean;
16 my $langterm = Attean::Literal->new(value => 'foo', language => 'en-US');
17 $langterm->ntriples_string; # "foo"@en-US
18  
19 my $typeterm = Attean::Literal->new(value => '123', datatype => 'http://www.w3.org/2001/XMLSchema#integer');
20 $langterm->ntriples_string; # "123"^^<http://www.w3.org/2001/XMLSchema#integer>
21  
22 =head1 DESCRIPTION
23  
24 The Attean::Literal class represents RDF literals.
25 It conforms to the L<Attean::API::Literal|Attean::API::Term> role.
26  
27 =head1 ATTRIBUTES
28  
29 The following attributes exist:
30  
31 =over 4
32  
33 =item C<< value >>
34  
35 =item C<< language >>
36  
37 =item C<< base_dir >>
38  
39 =item C<< datatype >>
40  
41 =back
42  
43 =head1 METHODS
44  
45 =over 4
46  
47 =item C<< has_language >>
48  
49 Returns true if the literal has a language tag, false otherwise.
50  
51 =item C<< has_base_dir >>
52  
53 Returns true if the literal has a base diredction, false otherwise.
54  
55 =cut
56  
57 5 package Attean::Literal 0.040 {
58 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 50 ▶ use Types::Standard qw(Str Maybe InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
60 50 ▶ use Attean::API::Term;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 50 ▶ use IRI;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 50 ▶ use Sub::Install;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 50 ▶ use Sub::Util qw(set_subname);
Subroutine BEGIN: called CC=1 SCAR=0.0
64 50 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
65 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
66
67 5 my $XSD_STRING = IRI->new(value => 'http://www.w3.org/2001/XMLSchema#string');
68 5 has 'value' => (is => 'ro', isa => Str, required => 1);
69 5 has 'language' => (is => 'ro', isa => Maybe[Str], predicate => 'has_language');
70 5 has 'base_dir' => (is => 'ro', isa => Maybe[Str]);
71 has 'datatype' => (
72 is => 'ro',
73 isa => InstanceOf['Attean::IRI'],
74 required => 1,
75 461 ▶ coerce => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
76 461 my $dt = shift;
77 461 ▶ if (blessed($dt) and $dt->isa('Attean::IRI')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($dt) and $dt->isa('Attean::IRI')) { } 38 423 461
Condition100%3/3
builtin::blessed($dt) and $dt->isa("Attean::IRI")
!ll&&!rl&&r
12829538
MC/DC100%2/2
builtin::blessed($dt) and $dt->isa("Attean::IRI")
builtin::blessed($dt) $dt->isa("Attean::IRI")
Truth table100%3/3
A and B
A= builtin::blessed($dt)B= $dt->isa("Attean::IRI")
ABresult
0X0
100
111
78 38 return $dt;
79 } else {
80 423 ▶ return blessed($dt) ? Attean::IRI->new($dt->as_string) : Attean::IRI->new($dt)
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($dt) ? : 295 128 423
81 }
82 },
83 72 ▶ default => sub { $XSD_STRING }
Subroutine __ANON__: called CC=1 SCAR=0.0
84 5 );
85 5 has 'ntriples_string' => (is => 'ro', isa => Str, lazy => 1, builder => '_ntriples_string');
86  
87 5 with 'Attean::API::Literal';
88  
89 472 ▶ around BUILDARGS => sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
90 472 my $orig = shift;
91 472 my $class = shift;
92 472 ▶ return $class->$orig(@_) if (scalar(@_) == 1 and ref($_[0]) eq "HASH");
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ == 1 and ref $_[0] eq "HASH" 0 472 472
Condition66%2/3
scalar @_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
2492230
MC/DC0%0/2
scalar @_ == 1 and ref $_[0] eq "HASH"
scalar @_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
93 472 ▶ if (scalar(@_) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) 223 249 472
94 223 my $dt = IRI->new('http://www.w3.org/2001/XMLSchema#string');
95 223 return $class->$orig(value => shift, datatype => $dt);
96 }
97 249 return $class->$orig(@_);
98 5 };
99
100 1449 ▶ around 'datatype' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
101 1449 my $orig = shift;
102 1449 my $self = shift;
103 1449 ▶ if ($self->has_base_dir) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_base_dir) { } 0 1449 1449
elsif ($self->has_language) { } 58 1391 1449
104 0 return Attean::IRI->new(value => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#dirLangString');
105 } elsif ($self->has_language) {
106 58 return Attean::IRI->new(value => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString');
107 } else {
108 1391 return $self->$orig(@_);
109 }
110 5 };
111
112 89 ▶ sub _ntriples_string {
Subroutine _ntriples_string: called CC=4 SCAR=13.9
113 89 my $self = shift;
114 89 my $value = $self->value;
115 89 $value =~ s/\\/\\\\/g;
116 89 $value =~ s/\n/\\n/g;
117 89 $value =~ s/\r/\\r/g;
118 89 $value =~ s/"/\\"/g;
119 89 ▶ if ($self->has_language) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_language) { } 16 73 89
120 16 ▶ if (my $d = $self->base_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $d = $self->base_dir) { } 0 16 16
121 0 return sprintf('"%s"@%s--%s', $value, $self->language, $d);
122 } else {
123 16 return sprintf('"%s"@%s', $value, $self->language);
124 }
125 } else {
126 73 my $dt = $self->datatype->as_string;
127 73 ▶ if ($dt eq 'http://www.w3.org/2001/XMLSchema#string') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt eq 'http://www.w3.org/2001/XMLSchema#string') { } 72 1 73
128 72 return sprintf('"%s"', $value);
129 } else {
130 1 return sprintf('"%s"^^<%s>', $value, $dt);
131 }
132 }
133 }
134  
135 =item C<< true >>
136  
137 The xsd:true term.
138  
139 =cut
140  
141 1503 ▶ sub true {
Subroutine true: called CC=1 SCAR=0.0
Pod documented
142 1503 state $v = Attean::Literal->new( value => 'true', datatype => 'http://www.w3.org/2001/XMLSchema#boolean' );
143 1503 return $v;
144 }
145
146 =item C<< false >>
147  
148 The xsd:false term.
149  
150 =cut
151  
152 130 ▶ sub false {
Subroutine false: called CC=1 SCAR=0.0
Pod documented
153 130 state $v = Attean::Literal->new( value => 'false', datatype => 'http://www.w3.org/2001/XMLSchema#boolean' );
154 130 return $v;
155 }
156
157 {
158 5 for my $method (qw(integer decimal float double)) {
159 5 ▶ my $code = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine integer: called CC=1 SCAR=0.0
Subroutine decimal: called CC=1 SCAR=0.0
Subroutine double: called CC=1 SCAR=0.0
Subroutine float: called CC=1 SCAR=0.0
160 5 my $class = shift;
161 5 return $class->new( value => shift, datatype => "http://www.w3.org/2001/XMLSchema#$method" );
162 20 };
163 20 Sub::Install::install_sub({
164 code => set_subname("${method}", $code),
165 as => "${method}"
166 });
167 }
168 }
169  
170 }
171  
172 5 1;
173  
174 __END__
« Test/Attean/MutableQuadStore.pm ↑ Summary Attean/ListIterator.pm »