| 1 | 51 | ▶ | use v5.14; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 51 | ▶ | 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.041 | ||||||||||||||||||||||||||||
| 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 | 11 | package Attean::Literal 0.041 { | |||||||||||||||||||||||||||
| 58 | 51 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | 51 | ▶ | use Types::Standard qw(Str Maybe InstanceOf); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | 51 | ▶ | use Attean::API::Term; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 51 | ▶ | use IRI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | 51 | ▶ | use Sub::Install; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | 51 | ▶ | use Sub::Util qw(set_subname); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | 51 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | 51 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 11 | my $XSD_STRING = Attean::IRI->new(value => 'http://www.w3.org/2001/XMLSchema#string'); | |||||||||||||||||||||||||||
| 68 | 11 | my $XSD_LANGSTRING = Attean::IRI->new(value => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString'); | |||||||||||||||||||||||||||
| 69 | 11 | my $XSD_LANGDIRSTRING = Attean::IRI->new(value => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#dirLangString'); | |||||||||||||||||||||||||||
| 70 | 11 | has 'value' => (is => 'ro', isa => Str, required => 1); | |||||||||||||||||||||||||||
| 71 | 11 | has 'language' => (is => 'ro', isa => Maybe[Str], predicate => 'has_language'); | |||||||||||||||||||||||||||
| 72 | 11 | has 'base_dir' => (is => 'ro', isa => Maybe[Str]); | |||||||||||||||||||||||||||
| 73 | has 'datatype' => ( | ||||||||||||||||||||||||||||
| 74 | is => 'ro', | ||||||||||||||||||||||||||||
| 75 | isa => InstanceOf['Attean::IRI'], | ||||||||||||||||||||||||||||
| 76 | required => 1, | ||||||||||||||||||||||||||||
| 77 | 491 | ▶ | coerce => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||
| 78 | 491 | my $dt = shift; | |||||||||||||||||||||||||||
| 79 | 491 | ▶ | if (blessed($dt) and $dt->isa('Attean::IRI')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($dt) and $dt->isa("Attean::IRI")
MC/DC50%1/2
builtin::blessed($dt) and $dt->isa("Attean::IRI")
builtin::blessed($dt)
$dt->isa("Attean::IRI")
Truth table66%2/3
A and B
A= builtin::blessed($dt)B= $dt->isa("Attean::IRI")
| |||||||||||||||||||||||||||||
| 80 | 390 | return $dt; | |||||||||||||||||||||||||||
| 81 | } else { | ||||||||||||||||||||||||||||
| 82 | 101 | ▶ | return blessed($dt) ? Attean::IRI->new($dt->as_string) : Attean::IRI->new($dt) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | }, | ||||||||||||||||||||||||||||
| 85 | 84 | ▶ | default => sub { $XSD_STRING } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 86 | 11 | ); | |||||||||||||||||||||||||||
| 87 | 11 | has 'ntriples_string' => (is => 'ro', isa => Str, lazy => 1, builder => '_ntriples_string'); | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 11 | with 'Attean::API::Literal'; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 502 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 92 | 502 | my $orig = shift; | |||||||||||||||||||||||||||
| 93 | 502 | my $class = shift; | |||||||||||||||||||||||||||
| 94 | 502 | ▶ | return $class->$orig(@_) if (scalar(@_) == 1 and ref($_[0]) eq "HASH"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
scalar @_ == 1 and ref $_[0] eq "HASH"
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"
| |||||||||||||||||||||||||||||
| 95 | 502 | ▶ | if (scalar(@_) == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 223 | return $class->$orig(value => shift, datatype => $XSD_STRING); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 279 | return $class->$orig(@_); | |||||||||||||||||||||||||||
| 99 | 11 | }; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 1400 | ▶ | around 'datatype' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 102 | 1400 | my $orig = shift; | |||||||||||||||||||||||||||
| 103 | 1400 | my $self = shift; | |||||||||||||||||||||||||||
| 104 | 1400 | ▶ | if ($self->has_base_dir) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 105 | 0 | return $XSD_LANGDIRSTRING; | |||||||||||||||||||||||||||
| 106 | } elsif ($self->has_language) { | ||||||||||||||||||||||||||||
| 107 | 58 | return $XSD_LANGSTRING; | |||||||||||||||||||||||||||
| 108 | } else { | ||||||||||||||||||||||||||||
| 109 | 1342 | return $self->$orig(@_); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 11 | }; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 89 | ▶ | sub _ntriples_string { | ||||||||||||||||||||||||||
|
Subroutine
_ntriples_string: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 114 | 89 | my $self = shift; | |||||||||||||||||||||||||||
| 115 | 89 | my $value = $self->value; | |||||||||||||||||||||||||||
| 116 | 89 | $value =~ s/\\/\\\\/g; | |||||||||||||||||||||||||||
| 117 | 89 | $value =~ s/\n/\\n/g; | |||||||||||||||||||||||||||
| 118 | 89 | $value =~ s/\r/\\r/g; | |||||||||||||||||||||||||||
| 119 | 89 | $value =~ s/"/\\"/g; | |||||||||||||||||||||||||||
| 120 | 89 | ▶ | if ($self->has_language) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 16 | ▶ | if (my $d = $self->base_dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | return sprintf('"%s"@%s--%s', $value, $self->language, $d); | |||||||||||||||||||||||||||
| 123 | } else { | ||||||||||||||||||||||||||||
| 124 | 16 | return sprintf('"%s"@%s', $value, $self->language); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } else { | ||||||||||||||||||||||||||||
| 127 | 73 | my $dt = $self->datatype->as_string; | |||||||||||||||||||||||||||
| 128 | 73 | ▶ | if ($dt eq 'http://www.w3.org/2001/XMLSchema#string') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 72 | return sprintf('"%s"', $value); | |||||||||||||||||||||||||||
| 130 | } else { | ||||||||||||||||||||||||||||
| 131 | 1 | return sprintf('"%s"^^<%s>', $value, $dt); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =item C<< true >> | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | The xsd:true term. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =cut | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 1503 | ▶ | sub true { | ||||||||||||||||||||||||||
|
Subroutine
true: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | 1503 | state $v = Attean::Literal->new( value => 'true', datatype => 'http://www.w3.org/2001/XMLSchema#boolean' ); | |||||||||||||||||||||||||||
| 144 | 1503 | return $v; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =item C<< false >> | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | The xsd:false term. | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =cut | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 130 | ▶ | sub false { | ||||||||||||||||||||||||||
|
Subroutine
false: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 154 | 130 | state $v = Attean::Literal->new( value => 'false', datatype => 'http://www.w3.org/2001/XMLSchema#boolean' ); | |||||||||||||||||||||||||||
| 155 | 130 | return $v; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | { | ||||||||||||||||||||||||||||
| 159 | 11 | my %datatypes; | |||||||||||||||||||||||||||
| 160 | 11 | for my $method (qw(integer decimal float double)) { | |||||||||||||||||||||||||||
| 161 | 44 | $datatypes{$method} = Attean::IRI->new( "http://www.w3.org/2001/XMLSchema#$method" ); | |||||||||||||||||||||||||||
| 162 | 4 | ▶ | my $code = sub { | ||||||||||||||||||||||||||
|
Subroutine
float: called CC=1 SCAR=0.0
Subroutine
double: called CC=1 SCAR=0.0
Subroutine
integer: called CC=1 SCAR=0.0
Subroutine
decimal: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 163 | 4 | my $class = shift; | |||||||||||||||||||||||||||
| 164 | 4 | return $class->new( value => shift, datatype => $datatypes{$method} ); | |||||||||||||||||||||||||||
| 165 | 44 | }; | |||||||||||||||||||||||||||
| 166 | 44 | Sub::Install::install_sub({ | |||||||||||||||||||||||||||
| 167 | code => set_subname("${method}", $code), | ||||||||||||||||||||||||||||
| 168 | as => "${method}" | ||||||||||||||||||||||||||||
| 169 | }); | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 11 | 1; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | __END__ | ||||||||||||||||||||||||||||