aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Youri/Check/Input/Updates/Source.pm
blob: e81d4d59fd5dfaff2e89379738cdafe2d7814cbf (plain)
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
# $Id: Source.pm 1179 2006-08-05 08:30:57Z warly $
package Youri::Check::Input::Updates::Source;

=head1 NAME

Youri::Check::Input::Updates::Source - Abstract updates source

=head1 DESCRIPTION

This abstract class defines the updates source interface for
L<Youri::Check::Input::Updates>.

=cut

use warnings;
use strict;
use Carp;

=head1 CLASS METHODS

=head2 new(%args)

Creates and returns a new Youri::Check::Input::Updates object.

Generic parameters (subclasses may define additional ones):

=over

=item aliases $aliases

Hash of package aliases.

=back

Warning: do not call directly, call subclass constructor instead.

=cut

sub new {
    my $class = shift;
    croak "Abstract class" if $class eq __PACKAGE__;

    my %options = (
        id          => '',    # object id
        test        => 0,     # test mode
        verbose     => 0,     # verbose mode
        aliases     => undef, # aliases
        resolver    => undef, # maintainer resolver
        preferences => undef, # maintainer preferences
        check_id    => '',    # parent check id
        @_
    );

    if ($options{aliases}) {
        croak "aliases should be an hashref" unless ref $options{aliases} eq 'HASH';
    }
    if ($options{resolver}) {
        croak "resolver should be a Youri::Check::Maintainer::Resolver object" unless $options{resolver}->isa("Youri::Check::Maintainer::Resolver");
    }
    if ($options{preferences}) {
        croak "preferences should be a Youri::Check::Maintainer::Preferences object" unless $options{preferences}->isa("Youri::Check::Maintainer::Preferences");
    }

    my $self = bless {
        _id          => $options{id},
        _test        => $options{test},
        _verbose     => $options{verbose},
        _aliases     => $options{aliases},
        _resolver    => $options{resolver},
        _preferences => $options{preferences},
        _check_id    => $options{check_id},
    }, $class;

    $self->_init(%options);

    return $self;
}

sub _init {
    # do nothing
}

=head1 INSTANCE METHODS

Excepted explicit statement, package name is expressed with Mandriva naming
conventions.

=head2 get_id()

Returns source identity.

=cut

sub get_id {
    my ($self) = @_;
    croak "Not a class method" unless ref $self;

    return $self->{_id};
}

=head2 get_version($package)

Returns available version for given package, which can be either a full
L<Youri::Package> object or just a package name.

=cut

sub get_version {
    my ($self, $package) = @_;
    croak "Not a class method" unless ref $self;

    my $name = ref $package && $package->isa('Youri::Package') ?
        $package->get_canonical_name() :
        $package;

    # translate in grabber namespace
    $name = $self->get_name($name);

    # return if aliased to null 
    return unless $name;

    # return subclass computation
    return $self->_version($name);
}

=head2 get_url($name)

Returns the URL of information source for package with given name.

=cut

sub get_url {
    my ($self, $name) = @_;

    # retun subclass computation
    return $self->_url($self->get_name($name));
}

=head2 name($name)

Returns name converted to specific source naming conventions for package with given name.

=cut

sub get_name {
    my ($self, $name) = @_;
    croak "Not a class method" unless ref $self;

    # return config aliases if it exists
    if ($self->{_aliases} ) {
        return $self->{_aliases}->{$name} if exists $self->{_aliases}->{$name};
    }

    # return maintainer aliases if it exists
    if ($self->{_resolver} && $self->{_preferences}) {
        my $maintainer = $self->{_resolver}->get_maintainer($name);
        if ($maintainer) {
            my $aliases = $self->{_preferences}->get_preference(
                $maintainer,
                $self->{_check_id},
                'aliases'
            );
            if ($aliases) {
                if ($aliases->{all}) {
                    return $aliases->{all}->{$name} if exists $aliases->{all}->{$name};
                }
                if ($aliases->{$self->{_id}}) {
                    return $aliases->{$self->{_id}}->{$name} if exists $aliases->{$self->{_id}}->{$name};
                }
            }
        }
    }

    # return return subclass computation
    return $self->_name($name);
}

=head2 _version($name)

Hook called by default B<version()> implementation after name translation.

=cut

sub _version {
    my ($self, $name) = @_;
    return $self->{_versions}->{$name};
}

=head2 _url($name)

Hook called by default B<url()> implementation after name translation.

=cut

sub _url {
    my ($self, $name) = @_;
    return undef;
}

=head2 _name($name)

Hook called by default B<name()> implementation if given name was not found in
the aliases.

=cut

sub _name {
    my ($self, $name) = @_;
    return $name;
}

=head1 SUBCLASSING

The following methods have to be implemented:

=over

=item version

As an alternative, the B<_version()> hook can be implemented.

=item url

As an alternative, the <_url()> hook can be implemented.

=item name

As an alternative, the B<_name()> hook can be implemented.

=back

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2002-2006, YOURI project

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

=cut

1;