File Coverage

blib/lib/Debian/Copyright/Stanza/OrSeparated.pm
Criterion Covered Total %
statement 41 48 85.4
branch 7 12 58.3
condition n/a
subroutine 9 11 81.8
pod 5 5 100.0
total 62 76 81.5


line stmt bran cond sub pod time code
1             package Debian::Copyright::Stanza::OrSeparated;
2             require v5.10.1;
3 3     3   17 use strict;
  3         6  
  3         107  
4 3     3   17 use warnings;
  3         6  
  3         81  
5              
6 3     3   4619 use Array::Unique;
  3         3120  
  3         91  
7 3     3   2683 use Text::ParseWords qw(quotewords);
  3         15402  
  3         604  
8             use overload
9 3         41 '""' => \&as_string,
10 3     3   30 'eq' => \=
  3         6  
11              
12             our $VERSION = '0.2';
13              
14             =head1 NAME
15              
16             Debian::Copyright::Stanza::OrSeparated - 'or' separated field abstraction
17              
18             =head1 VERSION
19              
20             This document describes Debian::Copyright::Stanza::OrSeparated version 0.2 .
21              
22             =cut
23              
24             =head1 SYNOPSIS
25              
26             my $f = Debian::Copyright::Stanza::OrSeparated->new('Artistic');
27             $f->add('GPL-1+ or BSD');
28             print $f->as_string;
29             # 'Artistic or GPL-1+ or BSD'
30             print "$f"; # the same
31             $f->sort;
32              
33             =head1 DESCRIPTION
34              
35             Debian::Copyright::Stanza::OrSeparated abstracts handling of the License
36             fields in Files blocks, which are lists separated by 'or'. It also supports
37             a body field representing the optional extended description of a License field.
38              
39             =head1 CONSTRUCTOR
40              
41             =head2 new (initial values)
42              
43             The initial values list is parsed and may contain strings that are in fact
44             'or'-separated lists. These are split appropriately using L'
45             C routine.
46              
47             =cut
48              
49             sub new {
50 47     47 1 214 my $self = bless {list=>[],body=>""}, shift;
51              
52 47         55 tie @{$self->{list}}, 'Array::Unique';
  47         249  
53              
54 47 50       417 my $body = exists $self->{body} ? $self->{body} : "";
55 47         61 my @list = ();
56 47         68 foreach my $e (@_) {
57 47 100       122 if ($e =~ m{\A([^\n]+)\n(.+)\z}xms) {
58 15         66 push @list, $1;
59 15         57 $body .= $2;
60             }
61             else {
62 32         85 push @list, $e;
63             }
64             }
65 47 50       161 $self->add(@list) if @list;
66 47 100       1668 $self->{body} = $body if $body;
67              
68 47         159 $self;
69             }
70              
71             =head1 METHODS
72              
73             =head2 as_string
74              
75             Returns text representation of the list. A simple join of the elements by
76             C< or >. The same function is used for overloading the stringification
77             operation.
78              
79             =cut
80              
81             sub as_string
82             {
83 94     94 1 551 my $self = shift;
84 94 50       244 my $body = exists $self->{body} ? "\n$self->{body}" : "";
85 94         97 return join( ' or ', @{ $self->{list} } ).$body;
  94         234  
86             }
87              
88             =head2 equals
89              
90             Natural implementation of the equality function.
91              
92             =cut
93              
94             sub equals
95             {
96 0 0   0 1 0 my @args = map { ref $_ ? $_->as_string : $_ } @_;
  0         0  
97 0         0 return $args[0] eq $args[1];
98             }
99              
100             sub _parse {
101 47     47   74 my $self = shift;
102              
103 47         39 my @output;
104              
105 47         74 for (@_) {
106 47         207 my @items = quotewords( qr/\s+or\s+/, 1, $_ );
107 47         2139 push @output, @items;
108             }
109              
110 47         157 return @output;
111             }
112              
113             =head2 add I<@items>
114              
115             Adds the given items to the list. Items that are already present are not added,
116             keeping the list unique.
117              
118             =cut
119              
120             sub add {
121 47     47 1 83 my ( $self, @items) = @_;
122              
123 47         46 push @{$self->{list}}, $self->_parse(@items);
  47         109  
124             }
125              
126             =head2 sort
127              
128             A handy method for sorting the list.
129              
130             =cut
131              
132             sub sort {
133 0     0 1   my $self = shift;
134              
135 0           @{$self->{list}} = sort @{$self->{list}};
  0            
  0            
136             }
137              
138             =head1 COPYRIGHT & LICENSE
139              
140             Copyright (C) 2011-12 Nicholas Bamber L
141              
142             This program is free software; you can redistribute it and/or modify it under
143             the terms of the GNU General Public License version 2 as published by the Free
144             Software Foundation.
145              
146             This program is distributed in the hope that it will be useful, but WITHOUT ANY
147             WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
148             PARTICULAR PURPOSE.
149              
150             =cut
151              
152             1;
153              
154             1;