/[suikacvs]/messaging/manakai/lib/Message/Field/Numval.pm
Suika

Contents of /messaging/manakai/lib/Message/Field/Numval.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (show annotations) (download)
Sun Jun 23 12:10:16 2002 UTC (22 years, 5 months ago) by wakaba
Branch: MAIN
CVS Tags: before-dis2-200411, manakai-release-0-3-2, manakai-release-0-3-1, manakai-release-0-4-0, manakai-200612, msg-0-1, HEAD
Branch point for: branch-suikawiki-1, experimental-xml-parser-200401, stable
Changes since 1.5: +7 -3 lines
2002-06-23  Wakaba <w@suika.fam.cx>

	* AngleQuoted.pm (%REG): Don't define regex locally.
	(Moved to Message::Util).
	* ContentType.pm, Date.pm, UA.pm,
	ValueParams.pm: Fix some codes not to be warned
	as 'Use of uninitialized value'.
	* Structured.pm 
	(header_default_charset, header_default_charset_input):
	New options.

1
2 =head1 NAME
3
4 Message::Field::Numval --- Perl module for
5 Internet message header field body that takes numeric values
6
7 =cut
8
9 package Message::Field::Numval;
10 use strict;
11 use vars qw(@ISA $VERSION);
12 $VERSION=do{my @r=(q$Revision: 1.5 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
13 require Message::Util;
14 require Message::Field::Structured;
15 push @ISA, qw(Message::Field::Structured);
16 use overload '.=' => sub { $_[0]->comment_add ($_[1]); $_[0] },
17 '0+' => sub { $_[0]->{value} || $_[0]->{option}->{value_default} },
18 '+=' => sub {
19 my $n = 0;#$_[0]->{value} + $_[1];
20 $_[0]->{value} = $n if $n <= $_[0]->{option}->{value_max};
21 $_[0]
22 },
23 '-=' => sub {
24 my $n = $_[0]->{value} - $_[1];
25 $_[0]->{value} = $n if $_[0]->{option}->{value_min} <= $n;
26 $_[0]
27 },
28 '*=' => sub {
29 my $n = $_[0]->{value} * $_[1];
30 $_[0]->{value} = $n if $n <= $_[0]->{option}->{value_max};
31 $_[0]
32 },
33 '**=' => sub {
34 my $n = $_[0]->{value} ** $_[1];
35 $_[0]->{value} = $n if $n <= $_[0]->{option}->{value_max};
36 $_[0]
37 },
38 '/=' => sub {
39 my $n = $_[0]->{value} / $_[1];
40 $_[0]->{value} = $n if $_[0]->{option}->{value_min} <= $n;
41 $_[0]
42 },
43 '%=' => sub {
44 my $n = $_[0]->{value} % $_[1];
45 $_[0]->{value} = $n if $_[0]->{option}->{value_min} <= $n;
46 $_[0]
47 },
48 'eq' => sub { $_[0]->stringify eq $_[1] },
49 'ne' => sub { $_[0]->stringify eq $_[1] },
50 fallback => 1;
51
52 =head1 CONSTRUCTORS
53
54 The following methods construct new C<Message::Field::Numval> objects:
55
56 =over 4
57
58 =cut
59
60 ## Initialize of this class -- called by constructors
61 sub _init ($;%) {
62 my $self = shift;
63 my %options = @_;
64 my %DEFAULT = (
65 -check_max => 0,
66 -check_min => 1,
67 #encoding_after_encode ## Inherited
68 #encoding_before_decode ## Inherited
69 -field_name => 'lines',
70 -field_param_name => '',
71 -format_pattern => '%d',
72 #hook_encode_string ## Inherited
73 #hook_decode_string ## Inherited
74 -output_comment => 0,
75 -use_comment => 0,
76 -value_default => 0,
77 -value_if_invalid => '',
78 -value_max => 100,
79 -value_min => 0,
80 );
81 $self->SUPER::_init (%DEFAULT, %options);
82 $self->{value} = $self->{options}->{value_default};
83 $self->{value} = $options{value} if defined $options{value};
84 $self->{comment} = [];
85 if (ref $options{comment} eq 'ARRAY') {
86 push @{$self->{comment}}, @{$options{comment}};
87 } elsif (defined $options{comment}) {
88 push @{$self->{comment}}, $options{comment};
89 }
90
91 my $fname = lc $self->{option}->{field_name};
92 my $pname = lc $self->{option}->{field_param_name};
93 if ($fname eq 'mime-version') {
94 $self->{option}->{output_comment} = 1;
95 $self->{option}->{use_comment} = 1;
96 $self->{option}->{format_pattern} = '%1.1f';
97 $self->{option}->{value_min} = 1;
98 } elsif ($fname eq 'x-priority' || $fname eq 'x-jsmail-priority') {
99 $self->{option}->{output_comment} = 1;
100 $self->{option}->{use_comment} = 1;
101 $self->{option}->{check_max} = 1;
102 $self->{option}->{check_min} = 1;
103 $self->{option}->{value_min} = 1; ## Highest
104 $self->{option}->{value_max} = 5; ## some implemention uses larger number...
105 } elsif ($fname eq 'auto-submitted' && $pname eq 'increment') {
106 $self->{option}->{output_comment} = 0;
107 $self->{option}->{check_min} = 1;
108 $self->{option}->{value_min} = 0;
109 $self->{option}->{value_if_invalid} = undef;
110 }
111 }
112
113 =item Message::Field::Numval->new ([%options])
114
115 Constructs a new C<Message::Field::Numval> object. You might pass some
116 options as parameters to the constructor.
117
118 =cut
119
120 sub new ($;%) {
121 my $class = shift;
122 my $self = bless {}, $class;
123 $self->_init (@_);
124 $self;
125 }
126
127 =item Message::Field::Numval->parse ($field-body, [%options])
128
129 Constructs a new C<Message::Field::Numval> object with
130 given field body. You might pass some options as parameters to the constructor.
131
132 =cut
133
134 sub parse ($$;%) {
135 my $class = shift;
136 my $self = bless {}, $class;
137 my $fb = shift;
138 $self->_init (@_);
139 push @{$self->{comment}}, $self->Message::Util::comment_to_array ($fb)
140 if $self->{option}->{use_comment};
141 $fb =~ s/[^0-9.-]//g;
142 $self->{value} = $& if $fb =~ /-?[0-9]+(\.[0-9]+)?/;
143 $self;
144 }
145
146 =back
147
148 =head1 METHODS FOR FIELD BODY VALUE
149
150 =over 4
151
152 =item $self->value ([$new_value])
153
154 Returns or set value. Note that this method
155 does not check whether value is valid or not.
156
157 =item $self->value_formatted ()
158
159 Returns formatted value string. Note that this method
160 does not check whether value is valid or not.
161 To check min/max value, use C<stringify> with
162 C<output_comment = -1> option (if necessary).
163
164 =cut
165
166 sub value ($;$%) {
167 my $self = shift;
168 my $new_value = shift;
169 if ($new_value) {
170 $self->{value} = $new_value;
171 }
172 $self->{value};
173 }
174
175 sub value_formatted ($;%) {
176 my $self = shift;
177 my %option = @_;
178 $option{format_pattern} = $self->{option}->{format_pattern}
179 unless defined $option{format_pattern};
180 sprintf $option{format_pattern}, $self->{value};
181 }
182
183 =item $self->comment ()
184
185 Returns array reference of comments. You can add/remove/change
186 array values.
187
188 =cut
189
190 sub comment ($) {
191 my $self = shift;
192 $self->{comment};
193 }
194
195 =item $self->comment_add ($comment, [%option]
196
197 Adds a C<comment>. Comments are outputed only when
198 the class option (not an option of this method!)
199 C<output_comment> is enabled (value C<1>).
200
201 On this method, only one option, C<prepend> is available.
202 With this option, additional comment is prepend
203 to current comments. (Default value is C<-1>, append.)
204
205 =cut
206
207 sub comment_add ($$;%) {
208 my $self = shift;
209 my ($value, %option) = (shift, @_);
210 if ($option{prepend}) {
211 unshift @{$self->{comment}}, $value;
212 } else {
213 push @{$self->{comment}}, $value;
214 }
215 $self;
216 }
217
218 =item $self->stringify ()
219
220 Returns C<field-body> as a string.
221
222 =cut
223
224 sub stringify ($;%) {
225 my $self = shift;
226 my %o = @_; my %option = %{$self->{option}};
227 for (grep {/^-/} keys %o) {$option{substr ($_, 1)} = $o{$_}}
228 return $option{value_if_invalid}
229 if $option{check_max} && $option{value_max} < $self->{value};
230 return $option{value_if_invalid}
231 if $option{check_min} && $option{value_min} > $self->{value};
232 my $s = sprintf $option{format_pattern}, $self->{value};
233 if ($option{use_comment} && $option{output_comment}) {
234 for (@{$self->{comment}}) {
235 my $t = $self->Message::Util::encode_ccontent ($_);
236 $s .= ' ('.$t.')' if length $t;
237 }
238 }
239 $s;
240 }
241 *as_string = \&stringify;
242
243 =back
244
245 =over 4
246
247 =item $self->option ( $option-name / $option-name, $option-value, ...)
248
249 Set/gets option value(s). You can pass multiple option name-value pair
250 as parameter when setting.
251
252 =cut
253
254 ## Inherited
255
256 =item $self->clone ()
257
258 Returns a copy of the object.
259
260 =cut
261
262 ## Inherited
263
264 =back
265
266 =head1 LICENSE
267
268 Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
269
270 This program is free software; you can redistribute it and/or modify
271 it under the terms of the GNU General Public License as published by
272 the Free Software Foundation; either version 2 of the License, or
273 (at your option) any later version.
274
275 This program is distributed in the hope that it will be useful,
276 but WITHOUT ANY WARRANTY; without even the implied warranty of
277 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
278 GNU General Public License for more details.
279
280 You should have received a copy of the GNU General Public License
281 along with this program; see the file COPYING. If not, write to
282 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
283 Boston, MA 02111-1307, USA.
284
285 =head1 CHANGE
286
287 See F<ChangeLog>.
288 $Date: 2002/06/09 11:08:28 $
289
290 =cut
291
292 1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24