summaryrefslogtreecommitdiff
path: root/lib/SlackBuild/Registry/Record.pm
blob: 22c6a3ca4cfa965e80740db6f5d73f20cf505ecc (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
package SlackBuild::Registry::Record;
use strict;
use warnings;
use Carp;
use SlackBuild::Base qw(package arch build date filename);
use SlackBuild::Registry::Version;
use SlackBuild::match;
use Scalar::Util qw(blessed);

=head2 new

    $x = new SlackBuild::Registry::Record(PACKAGE,
                     arch=>X, version=>Y, build=>Z, date=>D, filename=>F)
    
=cut

sub new {
    my $class = shift;
    my $self = bless {}, $class;
    $self->build(1);
    if (my $v = shift) {
	$self->package($v);
	if (@_) {
	    croak "bad number of arguments" if (@_ % 2);
	    local %_ = @_;
	    while (my ($k,$v) = each %_) {
		$self->${\$k}($v);
	    }
	}
    }
    return $self;
}

sub version {
    my $self = shift;
    if (@_) {
	croak "too many arguments" if @_ > 1;
	$self->{version} = new SlackBuild::Registry::Version(shift);
    }
    return $self->{version};
}

sub store {
    my $self = shift;
    croak "store not implemented";
}

sub as_string {
    my $self = shift;
    return $self->package . '-'
	   . ($self->version || '*') . '-'
	   . ($self->arch || '*') . '-'
	   . ($self->build || '1');
}

sub cmp {
    my ($self, $other) = @_;

    my $v;
    if ($v = ($self->package || '') cmp ($other->package || '')) {
	return $v;
    }
    if ($self->version <=> $other->version) {
	return $v;
    }
    if ($self->arch && $other->arch
	&& ($v = $self->arch cmp $other->arch)) {
	return $v;
    }
    return ($self->build || 1) <=> ($other->build || 1);
}   

sub match_attr {
    my ($self, $pred) = @_;
    my ($meth, $arg);
    if (ref($pred) eq 'ARRAY') {
	croak "array must have 2 elements" unless @$pred == 2;
        ($meth, $arg) = @$pred;
    } elsif (ref($pred) eq 'HASH') {
	croak "array must have 1 key" unless keys(%$pred) == 1;
	($meth, $arg) = each %$pred;
    } else {
	return $self == $pred;
    }
    return match($self->${ \$meth }, $arg);
}

sub matches {
    my $self = shift;
    my $args;
    if (@_ == 1) {
	if (ref($_[0]) eq 'HASH') {
	    $args = $_[0];
	} else {
	    croak "bad argument";
	}
    } elsif (@_ % 2 == 0) {
	local %_ = @_;
	$args = \%_;
    } else {
	croak "odd number of arguments";
    }
    while (my ($meth, $pred) = each %$args) {
	return 0 unless match($self->${ \$meth }, $pred);
    }
    return 1;
}

use overload
    '""' => sub { shift->as_string },
    'cmp' => sub {
	my ($self, $other, $swap) = @_;
	my $res = $self->cmp($other);
	return $swap ? -$res : $res;
    },
    '<=>' => sub {
	my ($self, $other, $swap) = @_;
	my $res = $self->cmp($other);
	return $swap ? -$res : $res;
    },
    '==' => sub {
	my ($self, $other) = @_;
	my $res = $self->cmp($other) == 0;
    },
    '!=' => sub {
	my ($self, $other) = @_;
	my $res = $self->cmp($other) != 0;
    },
    '<' => sub {
	my ($self, $other, $swap) = @_;
	my $res = $self->cmp($other) < 0;
	return $swap ? !$res : $res;
    },
    '<=' => sub {
	my ($self, $other, $swap) = @_;
	my $res = $self->cmp($other) <= 0;
	return $swap ? !$res : $res;
    },
    '>' => sub {
	my ($self, $other, $swap) = @_;
	my $res = $self->cmp($other) > 0;
	return $swap ? !$res : $res;
    },
    '>=' => sub {
	my ($self, $other, $swap) = @_;
	my $res = $self->cmp($other) >= 0;
	return $swap ? !$res : $res;
    };

1;

Return to:

Send suggestions and report system problems to the System administrator.