File: URI.pm

package info (click to toggle)
libdata-hal-perl 1.001-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 220 kB
  • sloc: perl: 315; makefile: 2
file content (109 lines) | stat: -r--r--r-- 2,266 bytes parent folder | download | duplicates (2)
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
package Data::HAL::URI;
use strictures;
use Moo; # has
use Types::Standard qw(InstanceOf Str);
use URI qw();

our $VERSION = '1.001';

has('_original', is => 'rw', isa => Str);
# just records what was passed to the constructor, this is a work-around for
# URI->new being a lossy operation
has(
    'uri',
    is      => 'ro',
    isa     => InstanceOf['URI'],
    default => sub {
        my ($self) = @_;
        return URI->new($self->_original);
    },
    handles => [qw(abs as_iri canonical clone eq fragment implementor new_abs opaque path rel scheme secure
                STORABLE_freeze STORABLE_thaw)],
    lazy    => 1,
    required => 1,
);

sub BUILDARGS {
    my (undef, @arg) = @_;
    return 1 == @arg ? {_original => $arg[0]} : {@arg};
}

sub as_string {
    my ($self, $root) = @_;
    if (
        $self->eq($self->_original)
        ||
        $root && $root->_nsmap && $self->uri->eq($root->_nsmap->uri($self->_original)->as_string)
    ) {
        return $self->_original;
    } else {
        return $self->uri->as_string;
    }
}

1;

__END__

=encoding UTF-8

=head1 NAME

Data::HAL::URI - URI wrapper

=head1 VERSION

This document describes Data::HAL::URI version 1.001

=head1 SYNOPSIS

    my $relation = $resource->relation->as_string;

=head1 DESCRIPTION

This is a wrapper for L<URI> objects.

=head1 INTERFACE

=head2 Composition

None, but L<URI> methods are delegated through the L</uri> attribute.

=head2 Constructors

=head3 C<new>

    my $u = Data::HAL::URI->new('http://example.com/something');

Takes a string argument, returns a C<Data::HAL::URI> object.

=head2 Attributes

=head3 C<uri>

Type C<URI>, B<required>, B<readonly>, can only be set from the L</new> constructor.

This attribute delegates all methods to L<URI> except L</as_string>.

=head2 Methods

=head3 C<as_string>

Returns the original argument to the constructor if still equal to the L</uri>, where equality also takes CURIE
expansion into account, or otherwise the L</uri> as string.

The unaltered behaviour is still available through the L</uri> accessor, e.g.:

    $resource->relation->uri->as_string

=head2 Exports

None.

=head1 DIAGNOSTICS

None.

=head1 CONFIGURATION AND ENVIRONMENT

Requires no configuration files or environment variables.