File Coverage

blib/lib/MooseX/Types/URI.pm
Criterion Covered Total %
statement 27 27 100.0
branch n/a
condition n/a
subroutine 9 9 100.0
pod n/a
total 36 36 100.0


line stmt bran cond sub pod time code
1 3     3   1094491 use strict;
  3         11  
  3         121  
2 3     3   55 use warnings;
  3         8  
  3         347  
3             package MooseX::Types::URI; # git description: v0.09-3-ge61243d
4             # ABSTRACT: URI related types and coercions for Moose
5             # KEYWORDS: moose types constraints coercions uri path web
6              
7             our $VERSION = '0.10';
8              
9 3     3   23 use Scalar::Util qw(blessed);
  3         6  
  3         213  
10              
11 3     3   1361 use URI;
  3         29604  
  3         188  
12 3     3   1059 use URI::QueryParam;
  3         3034  
  3         125  
13 3     3   991 use URI::WithBase;
  3         4178  
  3         148  
14              
15 3     3   567 use MooseX::Types::Moose qw{Str ScalarRef HashRef};
  3         729643  
  3         44  
16              
17 3     3   22592 use MooseX::Types 0.40 -declare => [qw(Uri _UriWithBase _Uri FileUri DataUri)];
  3         75  
  3         37  
18 3     3   25310 use if MooseX::Types->VERSION >= 0.42, 'namespace::autoclean';
  3         8  
  3         188  
19              
20             my $uri = Moose::Meta::TypeConstraint->new(
21             name => Uri,
22             parent => Moose::Meta::TypeConstraint::Union->new(
23             name => join("|", _Uri, _UriWithBase),
24             type_constraints => [
25             class_type( _Uri, { class => "URI" } ),
26             class_type( _UriWithBase, { class => "URI::WithBase" } ),
27             ],
28             ),
29             inline_as => sub { 'local $@; blessed('.$_[1].') && ( '.$_[1].'->isa("URI") || '.$_[1].'->isa("URI::WithBase") )' },
30             );
31              
32             register_type_constraint($uri);
33              
34             coerce( Uri,
35             from Str , via { URI->new($_) },
36             eval { +require MooseX::Types::Path::Class; 1 }
37             ? (
38             from "Path::Class::File", via { require URI::file; URI::file::->new($_) },
39             from "Path::Class::Dir" , via { require URI::file; URI::file::->new($_) },
40             from MooseX::Types::Path::Class::File() , via { require URI::file; URI::file::->new($_) },
41             from MooseX::Types::Path::Class::Dir() , via { require URI::file; URI::file::->new($_) },
42             ) : (),
43             from ScalarRef , via { my $u = URI->new("data:"); $u->data($$_); $u },
44             from HashRef , via { require URI::FromHash; URI::FromHash::uri_object(%$_) },
45             );
46              
47             class_type FileUri, { class => "URI::file", parent => $uri };
48              
49             coerce( FileUri,
50             from Str , via { require URI::file; URI::file::->new($_) },
51             eval { +require MooseX::Types::Path::Class; 1 }
52             ? (
53             from MooseX::Types::Path::Class::File() , via { require URI::file; URI::file::->new($_) },
54             from MooseX::Types::Path::Class::Dir() , via { require URI::file; URI::file::->new($_) },
55             from "Path::Class::File" , via { require URI::file; URI::file::->new($_) },
56             from "Path::Class::Dir" , via { require URI::file; URI::file::->new($_) },
57             ) : (),
58             );
59              
60             class_type DataUri, { class => "URI::data" };
61              
62             coerce( DataUri,
63             from Str , via { my $u = URI->new("data:"); $u->data($_); $u },
64             from ScalarRef , via { my $u = URI->new("data:"); $u->data($$_); $u },
65             );
66              
67             1;
68              
69             __END__
70              
71             =pod
72              
73             =encoding UTF-8
74              
75             =head1 NAME
76              
77             MooseX::Types::URI - URI related types and coercions for Moose
78              
79             =head1 VERSION
80              
81             version 0.10
82              
83             =head1 SYNOPSIS
84              
85             use MooseX::Types::URI qw(Uri FileUri DataUri);
86              
87             =head1 DESCRIPTION
88              
89             This package provides Moose types for fun with L<URI>s.
90              
91             =head1 TYPES
92              
93             The types are with C<ucfirst> naming convention so that they don't mask the
94             L<URI> class.
95              
96             =head2 C<Uri>
97              
98             Either L<URI> or L<URI::WithBase>
99              
100             Coerces from C<Str> via L<URI/new>.
101              
102             Coerces from L<Path::Class::File> and L<Path::Class::Dir> via L<URI::file/new> (but only if
103             L<MooseX::Types::Path::Class> is installed)
104              
105             Coerces from C<ScalarRef> via L<URI::data/new>.
106              
107             Coerces from C<HashRef> using L<URI::FromHash>.
108              
109             =head2 C<DataUri>
110              
111             A URI whose scheme is C<data>.
112              
113             Coerces from C<Str> and C<ScalarRef> via L<URI::data/new>.
114              
115             =head2 C<FileUri>
116              
117             A L<URI::file> class type.
118              
119             Has coercions from C<Str> (and optionally L<Path::Class::File> and L<Path::Class::Dir>) via L<URI::file/new>
120              
121             =for stopwords DWIMier ducktyping
122              
123             It has slightly DWIMier types than the L<URI> classes have due to
124             implementation details, so the types should be more forgiving when ducktyping
125             will work anyway (e.g. L<URI::WithBase> does not inherit L<URI>).
126              
127             =for stopwords TODO
128              
129             =head1 TODO
130              
131             Think about L<Path::Resource> integration of some sort
132              
133             =head1 SUPPORT
134              
135             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Types-URI>
136             (or L<bug-MooseX-Types-URI@rt.cpan.org|mailto:bug-MooseX-Types-URI@rt.cpan.org>).
137              
138             There is also a mailing list available for users of this distribution, at
139             L<http://lists.perl.org/list/moose.html>.
140              
141             There is also an irc channel available for users of this distribution, at
142             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
143              
144             =head1 AUTHOR
145              
146             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
147              
148             =head1 CONTRIBUTORS
149              
150             =for stopwords Karen Etheridge Florian Ragwitz Olivier Mengué Daniel Pittman MORIYA Masaki (gardejo) Shawn M Moore
151              
152             =over 4
153              
154             =item *
155              
156             Karen Etheridge <ether@cpan.org>
157              
158             =item *
159              
160             Florian Ragwitz <rafl@debian.org>
161              
162             =item *
163              
164             Olivier Mengué <dolmen@cpan.org>
165              
166             =item *
167              
168             Daniel Pittman <daniel@rimspace.net>
169              
170             =item *
171              
172             MORIYA Masaki (gardejo) <moriya@ermitejo.com>
173              
174             =item *
175              
176             Shawn M Moore <sartak@gmail.com>
177              
178             =back
179              
180             =head1 COPYRIGHT AND LICENCE
181              
182             This software is copyright (c) 2008 by יובל קוג'מן (Yuval Kogman).
183              
184             This is free software; you can redistribute it and/or modify it under
185             the same terms as the Perl 5 programming language system itself.
186              
187             =cut