| 1 | package Text::BibTeX::Validate::Warning; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | # ABSTRACT: validaton warning class | ||||||||||
| 7 | 3 | our $VERSION = '0.3.0'; # VERSION | |||||||||
| 8 | |||||||||||
| 9 | 3 | ▶ | use Text::sprintfn; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | =head1 NAME | ||||||||||
| 12 | |||||||||||
| 13 | Text::BibTeX::Validate::Warning - validaton warning class | ||||||||||
| 14 | |||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||
| 16 | |||||||||||
| 17 | use Text::BibTeX::Validate::Warning; | ||||||||||
| 18 | |||||||||||
| 19 | my $warning = Text::BibTeX::Validate::Warning->new( | ||||||||||
| 20 | 'value \'%(value)s\' is better written as \'%(suggestion)s\'', | ||||||||||
| 21 | { | ||||||||||
| 22 | field => 'month', | ||||||||||
| 23 | value => '1', | ||||||||||
| 24 | suggestion => 'Jan', | ||||||||||
| 25 | } | ||||||||||
| 26 | ); | ||||||||||
| 27 | print STDERR "$warning\n"; | ||||||||||
| 28 | |||||||||||
| 29 | =head1 DESCRIPTION | ||||||||||
| 30 | |||||||||||
| 31 | Text::BibTeX::Validate::Warning is used to store the content of | ||||||||||
| 32 | validation warning (as emitted by | ||||||||||
| 33 | L<Text::BibTeX::Validate|Text::BibTeX::Validate>) in a structured way. | ||||||||||
| 34 | Overloads are defined to stringify and to compare instances of the | ||||||||||
| 35 | class. | ||||||||||
| 36 | |||||||||||
| 37 | =head1 METHODS | ||||||||||
| 38 | |||||||||||
| 39 | =cut | ||||||||||
| 40 | |||||||||||
| 41 | use overload | ||||||||||
| 42 | 3 | ▶ | '""' => \&to_string, | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 3 | 'cmp' => \&_cmp; | |||||||||
| 44 | |||||||||||
| 45 | =head2 new( $message, $fields ) | ||||||||||
| 46 | |||||||||||
| 47 | Takes L<Text::sprintfn|Text::sprintfn>-compatible template and a hash | ||||||||||
| 48 | with the values for replacement in the template. Three field names are | ||||||||||
| 49 | reserved and used as prefixes for messages if defined: C<file> for the | ||||||||||
| 50 | name of a file, C<key> for BibTeX key and C<field> for BibTeX field | ||||||||||
| 51 | name. Field C<suggestion> is also somewhat special, as | ||||||||||
| 52 | L<Text::BibTeX::Validate|Text::BibTeX::Validate> may use its value to | ||||||||||
| 53 | replace the original in an attempt to clean up the BibTeX entry. | ||||||||||
| 54 | |||||||||||
| 55 | =cut | ||||||||||
| 56 | |||||||||||
| 57 | sub new | ||||||||||
| 58 | 15 | ▶ | { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 59 | 15 | my( $class, $message, $fields ) = @_; | |||||||||
| 60 | 15 | my $self = { %$fields, message => $message }; | |||||||||
| 61 | 15 | return bless $self, $class; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | =head2 fields() | ||||||||||
| 65 | |||||||||||
| 66 | Returns an array of fields defined in the instance in any order. | ||||||||||
| 67 | |||||||||||
| 68 | =cut | ||||||||||
| 69 | |||||||||||
| 70 | sub fields | ||||||||||
| 71 | 0 | ▶ | { | ||||||||
|
Subroutine
fields: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 72 | 0 | return keys %{$_[0]}; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | =head2 get( $field ) | ||||||||||
| 76 | |||||||||||
| 77 | Returns value of a field. | ||||||||||
| 78 | |||||||||||
| 79 | =cut | ||||||||||
| 80 | |||||||||||
| 81 | sub get | ||||||||||
| 82 | 0 | ▶ | { | ||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 83 | 0 | my( $self, $field ) = @_; | |||||||||
| 84 | 0 | return $self->{$field}; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | =head2 set( $field, $value ) | ||||||||||
| 88 | |||||||||||
| 89 | Sets a new value for a field. Returns the old value. | ||||||||||
| 90 | |||||||||||
| 91 | =cut | ||||||||||
| 92 | |||||||||||
| 93 | sub set | ||||||||||
| 94 | 0 | ▶ | { | ||||||||
|
Subroutine
set: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 95 | 0 | my( $self, $field, $value ) = @_; | |||||||||
| 96 | 0 | ( my $old_value, $self->{$field} ) = ( $self->{$field}, $value ); | |||||||||
| 97 | 0 | return $old_value; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | =head2 delete( $field ) | ||||||||||
| 101 | |||||||||||
| 102 | Unsets value for a field. Returns the old value. | ||||||||||
| 103 | |||||||||||
| 104 | =cut | ||||||||||
| 105 | |||||||||||
| 106 | sub delete | ||||||||||
| 107 | 0 | ▶ | { | ||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 108 | 0 | my( $self, $field ) = @_; | |||||||||
| 109 | |||||||||||
| 110 | 0 | my $old_value = $self->{$field}; | |||||||||
| 111 | 0 | delete $self->{$field}; | |||||||||
| 112 | |||||||||||
| 113 | 0 | return $old_value; | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | =head2 to_string() | ||||||||||
| 117 | |||||||||||
| 118 | Return a string representing the warning. | ||||||||||
| 119 | |||||||||||
| 120 | =cut | ||||||||||
| 121 | |||||||||||
| 122 | sub to_string | ||||||||||
| 123 | 8 | ▶ | { | ||||||||
|
Subroutine
to_string: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||
| 124 | 8 | my( $self ) = @_; | |||||||||
| 125 | |||||||||||
| 126 | 8 | my $message = $self->{message}; | |||||||||
| 127 | 8 | ▶ | $message = '%(field)s: ' . $message if exists $self->{field}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | 8 | ▶ | $message = '%(key)s: ' . $message if exists $self->{key}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | 8 | ▶ | $message = '%(file)s: ' . $message if exists $self->{file}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | |||||||||||
| 131 | 8 | return sprintfn $message, { %$self }; | |||||||||
| 132 | } | ||||||||||
| 133 | |||||||||||
| 134 | sub _cmp | ||||||||||
| 135 | 0 | ▶ | { | ||||||||
|
Subroutine
_cmp: not called CC=2 SCAR=17.9
| |||||||||||
| 136 | 0 | my( $a, $b, $are_swapped ) = @_; | |||||||||
| 137 | 0 | ▶ | return "$a" cmp "$b" * ($are_swapped ? -1 : 1); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | } | ||||||||||
| 139 | |||||||||||
| 140 | =head1 AUTHORS | ||||||||||
| 141 | |||||||||||
| 142 | Andrius Merkys, E<lt>merkys@cpan.orgE<gt> | ||||||||||
| 143 | |||||||||||
| 144 | =cut | ||||||||||
| 145 | |||||||||||
| 146 | 3 | 1; | |||||||||