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 |
} |
Auch abrufbar als: Unified diff
Attr Helper umgeschrieben auf dnamisch registrierte coderefs.