| 1 | package Dallycot::Value::URI; | ||||||||||||||||||||
| 2 | 30 | our $AUTHORITY = 'cpan:JSMITH'; | |||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | # ABSTRACT: A URI value that can be dereferenced | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 30 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 30 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 30 | ▶ | use utf8; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 30 | ▶ | use parent 'Dallycot::Value::Any'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 30 | ▶ | use Dallycot::Registry; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 30 | ▶ | use Promises qw(deferred); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 30 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 30 | ▶ | use URI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 30 | ▶ | use experimental qw(switch); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | sub new { | ||||||||||||||||||||
| 20 | 784 | ▶ | my ( $class, $uri ) = @_; | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 784 | ▶ | $class = ref $class || $class; | ||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 784 | $uri = URI->new($uri)->canonical; | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 784 | return bless [$uri] => $class; | |||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | sub to_rdf { | ||||||||||||||||||||
| 30 | 0 | ▶ | my( $self, $model ) = @_; | ||||||||||||||||||
|
Subroutine
to_rdf: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 0 | return RDF::Trine::Node::Resource->new($self->[0]->as_string); | |||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | sub calculate_length { | ||||||||||||||||||||
| 36 | 0 | ▶ | my ( $self, $engine ) = @_; | ||||||||||||||||||
|
Subroutine
calculate_length: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 0 | return Dallycot::Value::Numeric->new( length $self->[0]->as_string ); | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | sub value_at { | ||||||||||||||||||||
| 42 | 0 | ▶ | my ( $self, $engine, $index ) = @_; | ||||||||||||||||||
|
Subroutine
value_at: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 0 | my $d = deferred; | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 0 | ▶ | if($index > length($self -> [0] -> as_string)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | 0 | $d -> resolve($engine -> UNDEFINED); | |||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | else { | ||||||||||||||||||||
| 50 | 0 | $d->resolve( | |||||||||||||||||||
| 51 | bless [ substr( $self->[0]->as_string, $index - 1, 1 ), 'en' ] => 'Dallycot::Value::String' ); | ||||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 0 | return $d->promise; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | sub id { | ||||||||||||||||||||
| 58 | 171 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
id: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 171 | return "<" . $self->[0]->as_string . ">"; | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | sub as_text { | ||||||||||||||||||||
| 64 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
as_text: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 0 | return $self->id; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | sub is_lambda { | ||||||||||||||||||||
| 70 | 8 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
is_lambda: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 8 | my ( $lib, $method ) = $self->_get_library_and_method; | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 8 | ▶ | return unless defined $lib; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | return $lib->get_assignment($method)->then( | ||||||||||||||||||||
| 76 | sub { | ||||||||||||||||||||
| 77 | 8 | ▶ | my ($def) = @_; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.5
| |||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 8 | ▶ | return unless blessed($def); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | 8 | ▶ | return 1 if $def->isa(__PACKAGE__); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 0 | return $def->is_lambda; | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | 8 | ); | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 0 | ▶ | sub is_defined { return 1 } | ||||||||||||||||||
|
Subroutine
is_defined: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 0 | ▶ | sub is_empty {return} | ||||||||||||||||||
|
Subroutine
is_empty: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | sub min_arity { | ||||||||||||||||||||
| 91 | 8 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
min_arity: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 8 | my ( $lib, $method ) = $self->_get_library_and_method; | |||||||||||||||||||
| 94 | 8 | ▶ | if ($lib) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | 8 | return $lib->min_arity($method); | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | else { | ||||||||||||||||||||
| 98 | 0 | return 0; # TODO: fix once we fetch remote libraries | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 30 | my $registry = Dallycot::Registry->instance; | |||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | sub _get_library_and_method { | ||||||||||||||||||||
| 105 | 205 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
_get_library_and_method: called CC=4 SCAR=18.5
| |||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 205 | my $uri = $self->[0]->as_string; | |||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 205 | my ( $namespace, $method ) = split( /#/, $uri, 2 ); | |||||||||||||||||||
| 110 | 205 | ▶ | if ( !defined $method ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | 0 | ▶ | if ( $self->[0] =~ m{^(.*/)(.+?)$}x ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | 0 | $namespace = $1; | |||||||||||||||||||
| 113 | 0 | $method = $2; | |||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | else { | ||||||||||||||||||||
| 116 | 0 | $namespace = $self->[0]; | |||||||||||||||||||
| 117 | 0 | $method = ''; | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | else { | ||||||||||||||||||||
| 121 | 205 | $namespace .= '#'; | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 205 | ▶ | if ( $registry->has_namespace($namespace) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | 205 | return ( $registry->namespaces->{$namespace}, $method ); | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | 0 | return; | |||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | sub apply { | ||||||||||||||||||||
| 131 | 189 | ▶ | my ( $self, $engine, $options, @bindings ) = @_; | ||||||||||||||||||
|
Subroutine
apply: called CC=2 SCAR=8.5
Pod
undocumented
| |||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 189 | my ( $lib, $method ) = $self->_get_library_and_method; | |||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 189 | ▶ | if ($lib) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 136 | 189 | return $lib->apply( $method, $engine, $options, @bindings ); | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | else { # TODO: fetch resource and see if it's a lambda | ||||||||||||||||||||
| 139 | 0 | my $d = deferred; | |||||||||||||||||||
| 140 | 0 | $d->reject( $self->[0] . " is not a lambda" ); | |||||||||||||||||||
| 141 | 0 | return $d->promise; | |||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | sub resolve { | ||||||||||||||||||||
| 146 | 0 | ▶ | my ( $self, $engine ) = @_; | ||||||||||||||||||
|
Subroutine
resolve: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 0 | my $d = deferred; | |||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 0 | my $url = $self->[0]; | |||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | 0 | my $resolver = Dallycot::Resolver->instance; | |||||||||||||||||||
| 153 | $resolver->get($url->as_string)->done( | ||||||||||||||||||||
| 154 | sub { | ||||||||||||||||||||
| 155 | 0 | ▶ | $d->resolve(@_); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 156 | }, | ||||||||||||||||||||
| 157 | sub { | ||||||||||||||||||||
| 158 | 0 | ▶ | $d->reject(@_); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 159 | } | ||||||||||||||||||||
| 160 | 0 | ); | |||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 0 | return $d->promise; | |||||||||||||||||||
| 163 | } | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | sub resolve_content { | ||||||||||||||||||||
| 166 | 0 | ▶ | my ( $self, $engine ) = @_; | ||||||||||||||||||
|
Subroutine
resolve_content: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 0 | my $d = deferred; | |||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | 0 | my $url = $self->[0]; | |||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | 0 | my $resolver = Dallycot::TextResolver->instance; | |||||||||||||||||||
| 173 | $resolver->get($url->as_string)->done( | ||||||||||||||||||||
| 174 | sub { | ||||||||||||||||||||
| 175 | 0 | ▶ | $d->resolve(@_); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 176 | }, | ||||||||||||||||||||
| 177 | sub { | ||||||||||||||||||||
| 178 | 0 | ▶ | $d->reject(@_); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 179 | } | ||||||||||||||||||||
| 180 | 0 | ); | |||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | 0 | return $d->promise; | |||||||||||||||||||
| 183 | } | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | sub fetch_property { | ||||||||||||||||||||
| 186 | 0 | ▶ | my ( $self, $engine, $prop ) = @_; | ||||||||||||||||||
|
Subroutine
fetch_property: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 0 | ▶ | if ( @$self < 2 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 189 | 0 | print STDERR "Getting " . ($self->[0])."\n"; | |||||||||||||||||||
| 190 | 0 | push @$self, Dallycot::Resolver->instance->get( | |||||||||||||||||||
| 191 | "".($self -> [0]) | ||||||||||||||||||||
| 192 | ); | ||||||||||||||||||||
| 193 | } | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | $self -> [1] -> then(sub { | ||||||||||||||||||||
| 196 | 0 | ▶ | my($tstore) = @_; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 197 | 0 | $tstore -> fetch_property( $engine, $prop ); | |||||||||||||||||||
| 198 | 0 | }); | |||||||||||||||||||
| 199 | } | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | 30 | 1; | |||||||||||||||||||