File Coverage

blib/lib/URI/Namespace.pm
Criterion Covered Total %
statement 45 45 100.0
branch 12 12 100.0
condition 3 3 100.0
subroutine 14 14 100.0
pod 3 7 42.8
total 77 81 95.0


line stmt bran cond sub pod time code
1             package URI::Namespace;
2 6     6   3274 use Moo 1.006000;
  6         34966  
  6         41  
3 6     6   6224 use URI;
  6         4732  
  6         177  
4 6     6   2542 use IRI 0.003;
  6         762765  
  6         1246  
5 6     6   72 use Scalar::Util qw(blessed);
  6         14  
  6         577  
6 6     6   2528 use Types::Namespace 0.004 qw( Iri );
  6         165  
  6         63  
7 6     6   5222 use namespace::autoclean;
  6         31971  
  6         29  
8              
9             our $VERSION = '1.09_03';
10              
11             =head1 NAME
12              
13             URI::Namespace - A namespace URI/IRI class with autoload methods
14              
15             =head1 SYNOPSIS
16              
17             use URI::Namespace;
18             my $foaf = URI::Namespace->new( 'http://xmlns.com/foaf/0.1/' );
19             print $foaf->as_string;
20             print $foaf->name;
21              
22             =head1 DESCRIPTION
23              
24             This module provides an object with a URI/IRI attribute, typically used
25             prefix-namespace pairs, typically used in XML, RDF serializations,
26             etc. The local part can be used as a method, these are autoloaded.
27              
28             =head1 METHODS
29              
30             =over
31              
32             =item C<< new ( $string | URI | IRI ) >>
33              
34             This is the constructor. You may pass a string with a URI or a URI object.
35              
36             =item C<< uri ( [ $local_part ] ) >>
37              
38             Returns a L<URI> object with the namespace IRI. Optionally, the method
39             can take a local part as argument, in which case, it will return the
40             namespace URI with the local part appended.
41              
42             =item C<< iri ( [ $local_part ] ) >>
43              
44             Returns a L<IRI> object with the namespace IRI. Optionally, the method
45             can take a local part as argument, in which case, it will return the
46             namespace IRI with the local part appended.
47              
48             =item C<< local_part ( $uri ) >>
49              
50             Returns the local part string if the given argument URI (which may be
51             a string, L<URI> or L<IRI> object) matches the namespace URI, or
52             C<undef> if not.
53              
54              
55             =back
56              
57             The following methods from L<URI> can be used on an URI::Namespace object: C<as_string>, C<as_iri>, C<canonical>, C<eq>, C<abs>, C<rel>.
58              
59             One important usage for this module is to enable you to create L<URI>s for full URIs, e.g.:
60              
61             print $foaf->Person->as_string;
62              
63             will return
64              
65             http://xmlns.com/foaf/0.1/Person
66              
67             =head1 FURTHER DETAILS
68              
69             See L<URI::NamespaceMap> for further details about authors, license, etc.
70              
71             =cut
72              
73             around BUILDARGS => sub {
74             my ($next, $self, @parameters) = @_;
75             return $self->$next(@_) if ((@parameters > 1) || (ref($parameters[0]) eq 'HASH'));
76             return { _uri => $parameters[0] };
77             };
78              
79             has _uri => (
80             is => "ro",
81             isa => Iri,
82             coerce => 1,
83             required => 1,
84             handles => {
85             'as_string' => 'as_string',
86             'as_iri' => 'as_string',
87             }
88             );
89              
90             sub iri {
91 6     6 1 22232 my ($self, $name) = @_;
92 6 100       26 if (defined($name)) {
93 2         59 my $str = $self->_uri->as_string;
94             # XSD is given without hash in XML world, but hash should be added anyway (see issue #14)
95 2 100       113 $str .= '#' if ($str eq 'http://www.w3.org/2001/XMLSchema');
96 2         42 return IRI->new($str . "$name");
97             } else {
98 4         71 return $self->_uri;
99             }
100             }
101              
102             sub uri {
103 67     67 1 24915 my ($self, $name) = @_;
104 67         1579 my $iri = $self->_uri->as_string;
105 67 100       3725 if (defined($name)) {
106 61 100       170 $iri .= '#' if ($iri eq 'http://www.w3.org/2001/XMLSchema');
107 61         301 return URI->new($iri . "$name");
108             } else {
109 6         28 return URI->new($iri);
110             }
111             }
112              
113             sub local_part {
114 12     12 1 2596 my ($self, $fulluri) = @_;
115 12         22 my $local_part = undef;
116 12 100 100     91 if (blessed($fulluri) && $fulluri->isa('IRI')) {
117 4         83 $fulluri = $fulluri->as_string;
118             }
119 12         463 my $iri = $self->_uri->as_string;
120 12 100       794 if ($fulluri =~ m/^$iri(.+)/) {
121 6         31 $local_part = $1;
122             } else {
123             # TODO: throw error?
124             }
125 12         77 return $local_part;
126             }
127              
128              
129             for my $method (qw/ abs rel eq canonical /) {
130 1     1 0 1670 eval qq[ sub $method { shift->uri->${method}(\@_) } ];
  1     1 0 6  
  1     1 0 817  
  1     1 0 6  
131             }
132              
133             our $AUTOLOAD;
134             sub AUTOLOAD {
135 52     52   41265 my $self = shift;
136 52         305 my ($name) = $AUTOLOAD =~ /::(\w+)$/;
137 52         163 return $self->uri($name);
138             }
139              
140             1;
141             __END__