File Coverage

blib/lib/Debian/Copyright/Stanza.pm
Criterion Covered Total %
statement 53 53 100.0
branch 7 10 70.0
condition 2 4 50.0
subroutine 12 12 100.0
pod 5 5 100.0
total 79 84 94.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Debian::Copyright::Stanza - single stanza of Debian copyright file
4              
5             =head1 VERSION
6              
7             This document describes Debian::Copyright::Stanza version 0.2 .
8              
9             =head1 SYNOPSIS
10              
11             package Header;
12             use base 'Debian::Copyright::Stanza';
13             use constant fields => qw(
14             Format_Specification
15             Name
16             Source
17             Maintainer
18             X_Comment
19             );
20              
21             1;
22              
23             =head1 DESCRIPTION
24              
25             Debian::Copyright::Stanza is the base class for
26             L, L and
27             L classes.
28              
29             =cut
30              
31             package Debian::Copyright::Stanza;
32              
33             require v5.10.1;
34 3     3   20 use strict;
  3         5  
  3         106  
35 3     3   15 use base qw( Class::Accessor Tie::IxHash );
  3         5  
  3         3279  
36 3     3   18322 use Carp qw(croak);
  3         7  
  3         132  
37 3     3   1884 use Debian::Copyright::Stanza::OrSeparated;
  3         10  
  3         149  
38              
39             our $VERSION = '0.2';
40              
41             =head1 FIELDS
42              
43             Stanza fields are to be defined in the class method I. Typically this
44             can be done like:
45              
46             use constant fields => qw( Foo Bar Baz );
47              
48             Fields that are to contain dependency lists (as per L
49             method below) are automatically converted to instances of the
50             L class.
51              
52             =cut
53              
54 3     3   21 use constant fields => ();
  3         7  
  3         397  
55              
56             sub import {
57 13     13   3341 my( $class ) = @_;
58              
59 13         172 $class->mk_accessors( $class->fields );
60             }
61              
62 3     3   18 use overload '""' => \&as_string;
  3         5  
  3         18  
63              
64             =head1 CONSTRUCTOR
65              
66             =head2 new( { field => value, ... } )
67              
68             Creates a new L object and optionally initialises it
69             with the supplied data. The object is hashref based and tied to L.
70              
71             You may use dashes for initial field names, but these will be converted to
72             underscores:
73              
74             my $s = Debian::Copyright::Stanza::Header( {Name => "Blah"} );
75             print $s->Name;
76              
77             =cut
78              
79             sub new {
80 61     61 1 614 my $class = shift;
81 61   50     143 my $init = shift || {};
82              
83 61         149 my $self = Tie::IxHash->new;
84              
85 61         774 bless $self, $class;
86              
87 61         162 while( my($k,$v) = each %$init ) {
88 171         3838 $k =~ s/-/_/g;
89 171 100       629 $self->can($k)
90             or croak "Invalid field given ($k)";
91 170 100       412 if ( $self->is_or_separated($k) ) {
92 47         167 $self->$k( Debian::Copyright::Stanza::OrSeparated->new( $v ) );
93             }
94             else {
95 123         309 $self->$k($v);
96             }
97             }
98              
99 60         1621 return $self;
100             }
101              
102             =head1 METHODS
103              
104             =head2 is_or_separated($field)
105              
106             Returns true if the given field is to contain a 'or'-separated list of values.
107             This is used in stringification, when considering where to wrap long lines.
108              
109             =cut
110              
111             sub is_or_separated {
112 27     27 1 42 my( $self, $field ) = @_;
113 27         61 return 0;
114             }
115              
116             =head2 get($field)
117              
118             Overrides the default get method from L with L's
119             FETCH.
120              
121             =cut
122              
123             sub get {
124 23     23 1 39619 my( $self, $field ) = @_;
125              
126 23         44 $field =~ s/_/-/g;
127              
128 23         81 return $self->FETCH($field);
129             }
130              
131             =head2 set( $field, $value )
132              
133             Overrides the default set method from L with L's
134             STORE.
135              
136             =cut
137              
138             sub set {
139 170     170 1 875 my( $self, $field, $value ) = @_;
140              
141 170         321 chomp($value);
142              
143 170         690 $field =~ s/_/-/g;
144              
145 170         419 return $self->STORE( $field, $value );
146             }
147              
148             =head2 as_string([$width])
149              
150             Returns a string representation of the object. Ready to be printed into a
151             real F file. Used as a stringification operator.
152              
153             =cut
154              
155             sub as_string
156             {
157 19     19 1 4431 my ( $self, $width ) = @_;
158 19   50     118 $width //= 80;
159              
160 19         28 my @lines;
161              
162 19         76 $self->Reorder( map{ ( my $s = $_ ) =~ s/_/-/g; $s } $self->fields );
  78         116  
  78         200  
163              
164 19         574 for my $k ( $self->Keys ) {
165             # We don't' want the internal fields showing in the output
166 44 50       198 next if $k =~ /^-/; # _ in field names is replaced with dashes
167 44         120 my $v = $self->FETCH($k);
168 44 50       279 next unless defined($v);
169              
170 44         96 my $line = "$k: $v";
171 44 50       133 push @lines, $line if $line;
172             }
173              
174 19         140 return join( "\n", @lines ) . "\n";
175             }
176              
177             =head1 COPYRIGHT & LICENSE
178              
179             Copyright (C) 2011 Nicholas Bamber
180              
181             This module is substantially based upon L.
182             Copyright (C) 2009 Damyan Ivanov L [Portions]
183              
184             This program is free software; you can redistribute it and/or modify it under
185             the terms of the GNU General Public License version 2 as published by the Free
186             Software Foundation.
187              
188             This program is distributed in the hope that it will be useful, but WITHOUT ANY
189             WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
190             PARTICULAR PURPOSE.
191              
192             =cut
193              
194             1;