File: URI.pm

package info (click to toggle)
libtypes-uri-perl 0.007-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 172 kB
  • sloc: perl: 119; sh: 4; makefile: 2
file content (270 lines) | stat: -rw-r--r-- 6,381 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
use 5.008;
use strict;
use warnings;

package Types::URI;

our $AUTHORITY = 'cpan:TOBYINK';
our $VERSION   = '0.007';

use URI;
use URI::file;
use URI::data;
use URI::WithBase;
use URI::FromHash;

use Type::Library -base, -declare => qw( Uri FileUri DataUri Iri );

use Types::Path::Tiny  qw( Path );
use Types::Standard    qw( InstanceOf ScalarRef HashRef Str );
use Types::UUID        qw( Uuid );

my $AtteanIRI = InstanceOf['Attean::IRI'];
my $TrineNode = InstanceOf['RDF::Trine::Node::Resource'];
my $TrineNS   = InstanceOf['RDF::Trine::Namespace'];
my $XmlNS     = InstanceOf['XML::Namespace'];

__PACKAGE__->meta->add_type({
	name        => Iri,
	parent      => InstanceOf['IRI'],
	# Need to define coercions below to break circularity of
	# Uri and Iri.
});

__PACKAGE__->meta->add_type({
	name        => Uri,
	parent      => InstanceOf[qw/ URI URI::WithBase /],
	coercion    => [
		Uuid        ,=> q{ "URI"->new("urn:uuid:$_") },
		Str         ,=> q{ "URI"->new($_) },
		Path        ,=> q{ "URI::file"->new($_) },
		ScalarRef   ,=> q{ do { my $u = "URI"->new("data:"); $u->data($$_); $u } },
		HashRef     ,=> q{ "URI"->new(URI::FromHash::uri(%$_)) },
		$TrineNode  ,=> q{ "URI"->new($_->uri_value) },
		$TrineNS    ,=> q{ "URI"->new($_->uri->uri_value) },
		$XmlNS      ,=> q{ "URI"->new($_->uri) },
		Iri         ,=> q{ "URI"->new($_->as_string) },
		$AtteanIRI  ,=> q{ "URI"->new($_->as_string) },
	],
});

Iri->coercion->add_type_coercions(
	Uuid        ,=> q{ do { require IRI; "IRI"->new("urn:uuid:$_") } },
	Str         ,=> q{ do { require IRI; "IRI"->new($_) } },
	Path        ,=> q{ do { require IRI; my $u = "URI::file"->new($_); "IRI"->new($u->as_string) } },
	ScalarRef   ,=> q{ do { require IRI; my $u = "URI"->new("data:"); $u->data($$_); "IRI"->new($u->as_string) } },
	HashRef     ,=> q{ do { require IRI; "IRI"->new(URI::FromHash::uri(%$_)) } },
	$TrineNode  ,=> q{ do { require IRI; "IRI"->new($_->uri_value) } },
	$TrineNS    ,=> q{ do { require IRI; "IRI"->new($_->uri->uri_value) } },
	$XmlNS      ,=> q{ do { require IRI; "IRI"->new($_->uri) } },
	Uri         ,=> q{ do { require IRI; "IRI"->new($_->as_string) } },
);

__PACKAGE__->meta->add_type({
	name        => FileUri,
	parent      => Uri,
	constraint  => sub { $_->isa('URI::file') },
	inlined     => sub { InstanceOf->parameterize('URI::file')->inline_check($_[1]) },
	coercion    => [
		Str         ,=> q{ "URI::file"->new($_) },
		Path        ,=> q{ "URI::file"->new($_) },
		HashRef     ,=> q{ "URI"->new(URI::FromHash::uri(%$_)) },
		$TrineNode  ,=> q{ "URI"->new($_->uri_value) },
		$TrineNS    ,=> q{ "URI"->new($_->uri->uri_value) },
		$XmlNS      ,=> q{ "URI"->new($_->uri) },
		$AtteanIRI  ,=> q{ "URI"->new($_->as_string) },
		Iri         ,=> q{ "URI"->new($_->as_string) },
	],
});

__PACKAGE__->meta->add_type({
	name        => DataUri,
	parent      => Uri,
	constraint  => sub { $_->isa('URI::data') },
	inlined     => sub { InstanceOf->parameterize('URI::data')->inline_check($_[1]) },
	coercion    => [
		Str         ,=> q{ do { my $u = "URI"->new("data:"); $u->data($_); $u } },
		ScalarRef   ,=> q{ do { my $u = "URI"->new("data:"); $u->data($$_); $u } },
		HashRef     ,=> q{ "URI"->new(URI::FromHash::uri(%$_)) },
		$TrineNode  ,=> q{ "URI"->new($_->uri_value) },
		$TrineNS    ,=> q{ "URI"->new($_->uri->uri_value) },
		$XmlNS      ,=> q{ "URI"->new($_->uri) },
		$AtteanIRI  ,=> q{ "URI"->new($_->as_string) },
		Iri         ,=> q{ "URI"->new($_->as_string) },
	],
});

__PACKAGE__->meta->make_immutable; # returns true

__END__

=pod

=encoding utf-8

=head1 NAME

Types::URI - type constraints and coercions for URIs

=head1 SYNOPSIS

   package FroobleDocument;
   
   use Moose;
   use Types::URI -all;
   
   has source => (
      is      => 'ro',
      isa     => Uri,
      coerce  => 1,
   );

=head1 DESCRIPTION

L<Types::URI> is a type constraint library suitable for use with
L<Moo>/L<Moose> attributes, L<Kavorka> sub signatures, and so forth.

=head2 Types

This module provides some type constraints broadly compatible with
those provided by L<MooseX::Types::URI>, plus a couple of extra type
constraints.

=over

=item C<Uri>

A class type for L<URI>/L<URI::WithBase>. Coercions from:

=over

=item from C<Uuid>

Coerces to a URI in the C<< urn:uuid: >> schema. (See L<Types::UUID>.)

=item from C<Str>

Uses L<URI/new>.

=item from C<Path>

Uses L<URI::file/new>. (See L<Types::Path::Tiny>.)

=item from C<ScalarRef>

Uses L<URI::data/new>.

=item from C<HashRef>

Coerces using L<URI::FromHash>.

=item from C<Iri>

Uses L<URI/new>.

=item from L<RDF::Trine::Node::Resource>, L<RDF::Trine::Namespace>, L<XML::Namespace>

Uses L<URI/new>.

=back

=item C<FileUri>

A subtype of C<Uri> covering L<URI::file>. Coercions from:

=over

=item from C<Str>

Uses L<URI::file/new>.

=item from C<Path>

Uses L<URI::file/new>. (See L<Types::Path::Tiny>.)

=item from C<HashRef>

Coerces using L<URI::FromHash>.

=item from C<Iri>

Uses L<URI/new>.

=item from L<RDF::Trine::Node::Resource>, L<RDF::Trine::Namespace>, L<XML::Namespace>

Uses L<URI/new>.

=back

=item C<DataUri>

A subtype of C<Uri> covering L<URI::data>. Coercions from:

=over

=item from C<Str>

Uses L<URI::data/new>.

=item from C<ScalarRef>

Uses L<URI::data/new>.

=item from C<HashRef>

Coerces using L<URI::FromHash>.

=item from C<Iri>

Uses L<URI/new>.

=item from L<RDF::Trine::Node::Resource>, L<RDF::Trine::Namespace>, L<XML::Namespace>

Uses L<URI/new>.

=back

=item C<Iri>

A class type for L<IRI>. Coercions as per C<Uri> above, plus can coerce
from C<Uri>.

=back

=head1 BUGS

Please report any bugs to
L<http://rt.cpan.org/Dist/Display.html?Queue=Types-URI>.

=head1 SEE ALSO

L<MooseX::Types::URI>,
L<Type::Tiny::Manual>,
L<URI>,
L<URI::file>,
L<URI::data>,
L<URI::FromHash>,
L<RDF::Trine::Node::Resource>,
L<IRI>.

L<Types::UUID>,
L<Types::Path::Tiny>,
L<Types::Standard>.

=head1 AUTHOR

Toby Inkster E<lt>tobyink@cpan.orgE<gt>.

=head1 COPYRIGHT AND LICENCE

This software is copyright (c) 2014 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=head1 DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.