From cbbcefbbb1b2b58752bd9dd9d374c6092323ef27 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Thu, 2 Sep 2010 11:27:16 +0200 Subject: [PATCH] Attr Helper umgeschrieben auf dnamisch registrierte coderefs. --- SL/DB/Helpers/AttrDate.pm | 42 ++++++++++++++++-------------------- SL/DB/Helpers/AttrNumber.pm | 22 ++++++------------- SL/DB/Helpers/AttrPercent.pm | 22 ++++++------------- 3 files changed, 31 insertions(+), 55 deletions(-) diff --git a/SL/DB/Helpers/AttrDate.pm b/SL/DB/Helpers/AttrDate.pm index fa6338870..38d5696fd 100644 --- a/SL/DB/Helpers/AttrDate.pm +++ b/SL/DB/Helpers/AttrDate.pm @@ -10,31 +10,27 @@ sub define { my $attribute = shift; my %params = @_; - $params{places} = 2 if !defined($params{places}); - - my $code = <parse_date(\\\%::myconfig, \@_); - \$self->${attribute}(DateTime->new(year => \$yy, month => \$mm, day => \$dd)); - } else { - \$self->${attribute}(undef); + no strict 'refs'; + *{ $package . '::' . $attribute . '_as_date' } = sub { + my ($self, $string) = @_; + + if (@_ > 1) { + if ($string) { + my ($yy, $mm, $dd) = $::locale->parse_date(\%::myconfig, $string); + $self->$attribute(DateTime->new(year => $yy, month => $mm, day => $dd)); + } else { + $self->$attribute(undef); + } } - } - - return \$self->${attribute} ? \$::locale->reformat_date({ dateformat => 'yy-mm-dd' }, \$self->${attribute}->ymd, \$::myconfig{dateformat}) : undef; -} - -1; -CODE - eval $code; - croak "Defining '${attribute}_as_number' failed: $EVAL_ERROR" if $EVAL_ERROR; + return $self->$attribute + ? $::locale->reformat_date( + { dateformat => 'yy-mm-dd' }, + $self->${attribute}->ymd, + $::myconfig{dateformat} + ) + : undef; + }; return 1; } diff --git a/SL/DB/Helpers/AttrNumber.pm b/SL/DB/Helpers/AttrNumber.pm index 98acf192c..6593fb708 100644 --- a/SL/DB/Helpers/AttrNumber.pm +++ b/SL/DB/Helpers/AttrNumber.pm @@ -12,24 +12,14 @@ sub define { $params{places} = 2 if !defined($params{places}); - my $code = <$attribute($::form->parse_amount(\%::myconfig, $string)) if @_ > 1; - if (scalar \@_) { - \$self->${attribute}(\$::form->parse_amount(\\\%::myconfig, \$_[0])); - } - - return \$::form->format_amount(\\\%::myconfig, \$self->${attribute}, $params{places}); -} - -1; -CODE - - eval $code; - croak "Defining '${attribute}_as_number' failed: $EVAL_ERROR" if $EVAL_ERROR; + return $::form->format_amount(\%::myconfig, $self->$attribute, $params{places}); + }; return 1; } diff --git a/SL/DB/Helpers/AttrPercent.pm b/SL/DB/Helpers/AttrPercent.pm index 24f816404..e4c44ae90 100644 --- a/SL/DB/Helpers/AttrPercent.pm +++ b/SL/DB/Helpers/AttrPercent.pm @@ -12,24 +12,14 @@ sub define { $params{places} = 2 if !defined($params{places}); - my $code = <$attribute($::form->parse_amount(\%::myconfig, $string) / 100) if @_ > 1; - if (scalar \@_) { - \$self->${attribute}(\$::form->parse_amount(\\\%::myconfig, \$_[0]) / 100); - } - - return \$::form->format_amount(\\\%::myconfig, 100 * \$self->${attribute}, $params{places}); -} - -1; -CODE - - eval $code; - croak "Defining '${attribute}_as_number' failed: $EVAL_ERROR" if $EVAL_ERROR; + return $::form->format_amount(\%::myconfig, 100 * $self->$attribute, $params{places}); + }; return 1; } -- 2.20.1