Revision cbbcefbb
Von Sven Schöling vor etwa 14 Jahren hinzugefügt
SL/DB/Helpers/AttrDate.pm | ||
---|---|---|
10 | 10 |
my $attribute = shift; |
11 | 11 |
my %params = @_; |
12 | 12 |
|
13 |
$params{places} = 2 if !defined($params{places}); |
|
14 |
|
|
15 |
my $code = <<CODE; |
|
16 |
package ${package}; |
|
17 |
|
|
18 |
sub ${attribute}_as_date { |
|
19 |
my \$self = shift; |
|
20 |
|
|
21 |
if (scalar \@_) { |
|
22 |
if (\$_[0]) { |
|
23 |
my (\$yy, \$mm, \$dd) = \$::locale->parse_date(\\\%::myconfig, \@_); |
|
24 |
\$self->${attribute}(DateTime->new(year => \$yy, month => \$mm, day => \$dd)); |
|
25 |
} else { |
|
26 |
\$self->${attribute}(undef); |
|
13 |
no strict 'refs'; |
|
14 |
*{ $package . '::' . $attribute . '_as_date' } = sub { |
|
15 |
my ($self, $string) = @_; |
|
16 |
|
|
17 |
if (@_ > 1) { |
|
18 |
if ($string) { |
|
19 |
my ($yy, $mm, $dd) = $::locale->parse_date(\%::myconfig, $string); |
|
20 |
$self->$attribute(DateTime->new(year => $yy, month => $mm, day => $dd)); |
|
21 |
} else { |
|
22 |
$self->$attribute(undef); |
|
23 |
} |
|
27 | 24 |
} |
28 |
} |
|
29 |
|
|
30 |
return \$self->${attribute} ? \$::locale->reformat_date({ dateformat => 'yy-mm-dd' }, \$self->${attribute}->ymd, \$::myconfig{dateformat}) : undef; |
|
31 |
} |
|
32 |
|
|
33 |
1; |
|
34 |
CODE |
|
35 | 25 |
|
36 |
eval $code; |
|
37 |
croak "Defining '${attribute}_as_number' failed: $EVAL_ERROR" if $EVAL_ERROR; |
|
26 |
return $self->$attribute |
|
27 |
? $::locale->reformat_date( |
|
28 |
{ dateformat => 'yy-mm-dd' }, |
|
29 |
$self->${attribute}->ymd, |
|
30 |
$::myconfig{dateformat} |
|
31 |
) |
|
32 |
: undef; |
|
33 |
}; |
|
38 | 34 |
|
39 | 35 |
return 1; |
40 | 36 |
} |
SL/DB/Helpers/AttrNumber.pm | ||
---|---|---|
12 | 12 |
|
13 | 13 |
$params{places} = 2 if !defined($params{places}); |
14 | 14 |
|
15 |
my $code = <<CODE; |
|
16 |
package ${package}; |
|
15 |
no strict 'refs'; |
|
16 |
*{ $package . '::' . $attribute . '_as_number' } = sub { |
|
17 |
my ($self, $string) = @_; |
|
17 | 18 |
|
18 |
sub ${attribute}_as_number { |
|
19 |
my \$self = shift; |
|
19 |
$self->$attribute($::form->parse_amount(\%::myconfig, $string)) if @_ > 1; |
|
20 | 20 |
|
21 |
if (scalar \@_) { |
|
22 |
\$self->${attribute}(\$::form->parse_amount(\\\%::myconfig, \$_[0])); |
|
23 |
} |
|
24 |
|
|
25 |
return \$::form->format_amount(\\\%::myconfig, \$self->${attribute}, $params{places}); |
|
26 |
} |
|
27 |
|
|
28 |
1; |
|
29 |
CODE |
|
30 |
|
|
31 |
eval $code; |
|
32 |
croak "Defining '${attribute}_as_number' failed: $EVAL_ERROR" if $EVAL_ERROR; |
|
21 |
return $::form->format_amount(\%::myconfig, $self->$attribute, $params{places}); |
|
22 |
}; |
|
33 | 23 |
|
34 | 24 |
return 1; |
35 | 25 |
} |
SL/DB/Helpers/AttrPercent.pm | ||
---|---|---|
12 | 12 |
|
13 | 13 |
$params{places} = 2 if !defined($params{places}); |
14 | 14 |
|
15 |
my $code = <<CODE; |
|
16 |
package ${package}; |
|
15 |
no strict 'refs'; |
|
16 |
*{ $package . '::' . $attribute . '_as_percent' } = sub { |
|
17 |
my ($self, $string) = @_; |
|
17 | 18 |
|
18 |
sub ${attribute}_as_percent { |
|
19 |
my \$self = shift; |
|
19 |
$self->$attribute($::form->parse_amount(\%::myconfig, $string) / 100) if @_ > 1; |
|
20 | 20 |
|
21 |
if (scalar \@_) { |
|
22 |
\$self->${attribute}(\$::form->parse_amount(\\\%::myconfig, \$_[0]) / 100); |
|
23 |
} |
|
24 |
|
|
25 |
return \$::form->format_amount(\\\%::myconfig, 100 * \$self->${attribute}, $params{places}); |
|
26 |
} |
|
27 |
|
|
28 |
1; |
|
29 |
CODE |
|
30 |
|
|
31 |
eval $code; |
|
32 |
croak "Defining '${attribute}_as_number' failed: $EVAL_ERROR" if $EVAL_ERROR; |
|
21 |
return $::form->format_amount(\%::myconfig, 100 * $self->$attribute, $params{places}); |
|
22 |
}; |
|
33 | 23 |
|
34 | 24 |
return 1; |
35 | 25 |
} |
Auch abrufbar als: Unified diff
Attr Helper umgeschrieben auf dnamisch registrierte coderefs.