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
|
package Debian::Copyright::Stanza::OrSeparated;
require v5.10.1;
use strict;
use warnings;
use Array::Unique;
use Text::ParseWords qw(quotewords);
use overload
'""' => \&as_string,
'eq' => \=
our $VERSION = '0.2';
=head1 NAME
Debian::Copyright::Stanza::OrSeparated - 'or' separated field abstraction
=head1 VERSION
This document describes Debian::Copyright::Stanza::OrSeparated version 0.2 .
=cut
=head1 SYNOPSIS
my $f = Debian::Copyright::Stanza::OrSeparated->new('Artistic');
$f->add('GPL-1+ or BSD');
print $f->as_string;
# 'Artistic or GPL-1+ or BSD'
print "$f"; # the same
$f->sort;
=head1 DESCRIPTION
Debian::Copyright::Stanza::OrSeparated abstracts handling of the License
fields in Files blocks, which are lists separated by 'or'. It also supports
a body field representing the optional extended description of a License field.
=head1 CONSTRUCTOR
=head2 new (initial values)
The initial values list is parsed and may contain strings that are in fact
'or'-separated lists. These are split appropriately using L<Text::ParseWords>'
C<quotewords> routine.
=cut
sub new {
my $self = bless {list=>[],body=>""}, shift;
tie @{$self->{list}}, 'Array::Unique';
my $body = exists $self->{body} ? $self->{body} : "";
my @list = ();
foreach my $e (@_) {
if ($e =~ m{\A([^\n]+)\n(.+)\z}xms) {
push @list, $1;
$body .= $2;
}
else {
push @list, $e;
}
}
$self->add(@list) if @list;
$self->{body} = $body if $body;
$self;
}
=head1 METHODS
=head2 as_string
Returns text representation of the list. A simple join of the elements by
C< or >. The same function is used for overloading the stringification
operation.
=cut
sub as_string
{
my $self = shift;
my $body = exists $self->{body} ? "\n$self->{body}" : "";
return join( ' or ', sort @{ $self->{list} } ).$body;
}
=head2 equals
Natural implementation of the equality function.
=cut
sub equals
{
my @args = map { ref $_ ? $_->as_string : $_ } @_;
return $args[0] eq $args[1];
}
sub _parse {
my $self = shift;
my @output;
for (@_) {
my @items = quotewords( qr/\s+or\s+/, 1, $_ );
push @output, @items;
}
return @output;
}
=head2 add I<@items>
Adds the given items to the list. Items that are already present are not added,
keeping the list unique.
=cut
sub add {
my ( $self, @items) = @_;
push @{$self->{list}}, $self->_parse(@items);
}
=head2 sort
A handy method for sorting the list.
=cut
sub sort {
my $self = shift;
@{$self->{list}} = sort @{$self->{list}};
}
=head1 COPYRIGHT & LICENSE
Copyright (C) 2011-12 Nicholas Bamber L<nicholas@periapt.co.uk>
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License version 2 as published by the Free
Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
=cut
1;
1;
|