File Coverage

lib/Data/URIID/Base.pm
Criterion Covered Total %
statement 24 56 42.8
branch 6 32 18.7
condition 6 11 54.5
subroutine 8 11 72.7
pod 4 4 100.0
total 48 114 42.1


line stmt bran cond sub pod time code
1             # Copyright (c) 2023-2024 Philipp Schafft
2              
3             # licensed under Artistic License 2.0 (see LICENSE file)
4              
5             # ABSTRACT: Extractor for identifiers from URIs
6              
7             package Data::URIID::Base;
8              
9 6     6   2745 use v5.10;
  6         24  
10 6     6   36 use strict;
  6         15  
  6         170  
11 6     6   24 use warnings;
  6         8  
  6         350  
12              
13 6     6   32 use Carp;
  6         7  
  6         527  
14              
15 6     6   1264 use Data::Identifier v0.25;
  6         363989  
  6         69  
16              
17             our $VERSION = v0.20;
18              
19              
20              
21              
22             #@returns Data::URIID
23             sub extractor {
24 295     295 1 536 my ($self, %opts) = @_;
25 295 50       1355 return $self->{extractor} if defined $self->{extractor};
26 0 0       0 return $opts{default} if exists $opts{default};
27 0         0 croak 'Invalid access: No extractor (instance of Data::URIID) known';
28             }
29              
30              
31             sub ise {
32 523     523 1 957 my ($self, %opts) = @_;
33 523 50       1676 return $self->_cast_ise($self->{ise}, 'ise', %opts) if defined $self->{ise};
34 0 0       0 return $opts{default} if exists $opts{default};
35 0         0 croak 'No ISE known';
36             }
37              
38              
39             sub displayname {
40 0     0 1 0 my ($self, %opts) = @_;
41              
42 0 0       0 unless ($opts{no_defaults}) {
43 0 0       0 return $opts{_fallback} if defined $opts{_fallback}; # fallback defined by overriding method.
44              
45             {
46 0         0 my $v = $self->ise(default => undef, no_defaults => 1);
  0         0  
47 0 0       0 return $v if defined $v;
48             }
49             }
50              
51 0 0       0 return $opts{default} if exists $opts{default};
52              
53 0         0 croak 'No displayname known';
54             }
55              
56              
57             sub as {
58 0     0 1 0 my ($self, $as, %opts) = @_;
59 0   0     0 $opts{extractor} //= $self->{extractor};
60 0         0 return $self->Data::Identifier::as($as, %opts);
61             }
62              
63             # ---- Private helpers ----
64              
65             sub _as_lookup {
66 0     0   0 my ($self, $lookup_args, %opts) = @_;
67 0         0 my Data::URIID $extractor = $self->extractor;
68 0         0 my $res;
69             my $old_online;
70              
71 0 0       0 if (exists $opts{online}) {
72 0         0 $old_online = $extractor->online;
73 0         0 $extractor->online($opts{online});
74             }
75              
76 0         0 $res = $extractor->lookup(@{$lookup_args});
  0         0  
77              
78 0 0       0 if (exists $opts{online}) {
79 0         0 $extractor->online($old_online);
80             }
81              
82 0         0 return $res;
83             }
84              
85             sub _cast_ise {
86 541     541   14918 my ($self, $src, $src_type, %opts) = @_;
87 541   100     1663 my $as = $opts{as} // 'raw';
88              
89 541 50       1060 $as = 'raw' if $as eq 'string'; # compatibility with <= v0.09
90              
91 541 100 66     1426 if ($as eq 'raw' || $as eq 'ise' || $as eq $src_type) {
    50 66        
    0          
    0          
92 539         2820 return $src;
93             } elsif ($as eq 'Data::Identifier') {
94 2         9 return Data::Identifier->new($src_type => $src);
95             } elsif ($as eq 'Data::URIID::Result') {
96 0           return $self->_as_lookup([$src_type => $src], %opts);
97             } elsif ($as eq 'Data::URIID::Service') {
98 0           return $self->extractor->service($src);
99             }
100              
101             {
102 0           my $val = Data::Identifier->new($src_type => $src)->as($as, %opts{'no_defaults'}, default => undef);
  0            
103 0 0         return $val if defined $val;
104             }
105              
106 0           croak sprintf('Cannot convert identifier to type "%s"', $as);
107             }
108              
109             1;
110              
111             __END__
112              
113             =pod
114              
115             =encoding UTF-8
116              
117             =head1 NAME
118              
119             Data::URIID::Base - Extractor for identifiers from URIs
120              
121             =head1 VERSION
122              
123             version v0.20
124              
125             =head1 SYNOPSIS
126              
127             use Data::URIID;
128              
129             use parent 'Data::URIID::Base';
130              
131             This module is the base package for a number of other packages.
132             Common methods are defined in here.
133              
134             B<Note:>
135             Functionality marked with B<Experimental> may or may not work as expected
136             (e.g. may contain bugs or may change behaviour in future versions without warning).
137              
138             =head1 METHODS
139              
140             =head2 extractor
141              
142             my Data::URIID $extractor = $object->extractor( [ %opts ] );
143              
144             Returns the L<Data::URIID> object used to create this object (if any).
145             If the extractor is not/no longer available this method C<die>s.
146              
147             The following options are defined:
148              
149             =over
150              
151             =item C<default>
152              
153             Returns the given value if no value is found.
154             This can also be set to C<undef> to allow returning C<undef> in case of no value found instead of C<die>-ing.
155              
156             =back
157              
158             =head2 ise
159              
160             my $ise = $object->ise( [ %opts ] );
161              
162             Returns the ISE of this object. If no ISE is known
163             this method will C<die>.
164              
165             The following options are defined:
166              
167             =over
168              
169             =item C<as>
170              
171             Return the value as the given type.
172             This is the package name of the type, C<ise> for pain ISE perl string.
173             If the given type is not supported or cannot be constructed the method C<die>s.
174              
175             At least the following types are supported:
176             L<Data::URIID::Result>,
177             L<Data::URIID::Service>,
178             L<Data::Identifier>.
179              
180             =item C<default>
181              
182             Returns the given value if no value is found.
183             This can also be set to C<undef> to allow returning C<undef> in case of no value found instead of C<die>-ing.
184              
185             =item C<no_defaults>
186              
187             B<Experimental:>
188             If set to true this will avoid calculating identifiers from others if C<as> does not match what is available.
189              
190             =item C<online>
191              
192             Overrides the L<Data::URIID/"online"> flag used for the lookup if C<as> is set to L<Data::URIID::Result>.
193             This is very useful to prevent network traffic for auxiliary lookups.
194              
195             =back
196              
197             =head2 displayname
198              
199             my $displayname = $object->displayname( [ %opts ] );
200              
201             This method is for compatibility with other moduls such as L<Data::Identifier> and L<Data::TagDB::Tag>.
202             This methods C<die>s if no value can be found.
203              
204             The following options are supported:
205              
206             =over
207              
208             =item C<default>
209              
210             B<Experimental:>
211             Returns the given value if no value is found.
212             This can also be set to C<undef> to allow returning C<undef> in case of no value found instead of C<die>-ing.
213              
214             =item C<no_defaults>
215              
216             B<Experimental:>
217             If set to true this will avoid returning an identifier or any other default value.
218              
219             =back
220              
221             =head2 as
222              
223             my $xxx = $base->as($as, [ %opts ] );
224              
225             Proxy for L<Data::Identifier/as>.
226              
227             Automatically adds C<extractor> to C<%opts> if any is known (see L</extractor>).
228              
229             =head1 AUTHOR
230              
231             Philipp Schafft <lion@cpan.org>
232              
233             =head1 COPYRIGHT AND LICENSE
234              
235             This software is Copyright (c) 2023-2025 by Philipp Schafft <lion@cpan.org>.
236              
237             This is free software, licensed under:
238              
239             The Artistic License 2.0 (GPL Compatible)
240              
241             =cut