1 package SL::DB::Helper::Attr;
 
   6   my ($package, %params) = @_;
 
   8   for my $col ($package->meta->columns) {
 
   9     next if $col->primary_key_position; # don't make attr helper for primary keys
 
  10     _make_by_type($package, $col->name, $col->type);
 
  17   my ($package, %params) = @_;
 
  19   for my $name (keys %params) {
 
  20     my @types = ref $params{$name} eq 'ARRAY' ? @{ $params{$name} } : ($params{$name});
 
  21     for my $type (@types) {
 
  22       _make_by_type($package, $name, $type);
 
  31   my ($package, $name, $type) = @_;
 
  32   _as_number     ($package, $name, places => -2) if $type =~ /numeric | real | float/xi;
 
  33   _as_null_number($package, $name, places => -2) if $type =~ /numeric | real | float/xi;
 
  34   _as_percent    ($package, $name, places =>  2) if $type =~ /numeric | real | float/xi;
 
  35   _as_number     ($package, $name, places =>  0) if $type =~ /int/xi;
 
  36   _as_null_number($package, $name, places =>  0) if $type =~ /int/xi;
 
  37   _as_date       ($package, $name)               if $type =~ /date | timestamp/xi;
 
  38   _as_timestamp  ($package, $name)             if $type =~ /timestamp/xi;
 
  39   _as_bool_yn    ($package, $name)               if $type =~ /bool/xi;
 
  44   my $attribute   = shift;
 
  47   $params{places} = 2 if !defined($params{places});
 
  50   *{ $package . '::' . $attribute . '_as_number' } = sub {
 
  51     my ($self, $string) = @_;
 
  53     $self->$attribute($::form->parse_amount(\%::myconfig, $string)) if @_ > 1;
 
  55     return $::form->format_amount(\%::myconfig, $self->$attribute, $params{places});
 
  61   my $attribute   = shift;
 
  64   $params{places} = 2 if !defined($params{places});
 
  67   *{ $package . '::' . $attribute . '_as_null_number' } = sub {
 
  68     my ($self, $string) = @_;
 
  70     $self->$attribute($string eq '' ? undef : $::form->parse_amount(\%::myconfig, $string)) if @_ > 1;
 
  72     return defined $self->$attribute ? $::form->format_amount(\%::myconfig, $self->$attribute, $params{places}) : '';
 
  78   my $attribute   = shift;
 
  81   $params{places} = 2 if !defined($params{places});
 
  84   *{ $package . '::' . $attribute . '_as_percent' } = sub {
 
  85     my ($self, $string) = @_;
 
  87     $self->$attribute($::form->parse_amount(\%::myconfig, $string) / 100) if @_ > 1;
 
  89     return $::form->format_amount(\%::myconfig, 100 * $self->$attribute, $params{places});
 
  97   my $attribute   = shift;
 
 101   *{ $package . '::' . $attribute . '_as_date' } = sub {
 
 102     my ($self, $string) = @_;
 
 106         my ($yy, $mm, $dd) = $::locale->parse_date(\%::myconfig, $string);
 
 107         $self->$attribute(DateTime->new(year => $yy, month => $mm, day => $dd));
 
 109         $self->$attribute(undef);
 
 113     return $self->$attribute
 
 114       ? $::locale->reformat_date(
 
 115           { dateformat => 'yy-mm-dd' },
 
 116           ( ($self->$attribute eq 'now' || $self->$attribute eq 'now()')
 
 120           $::myconfig{dateformat}
 
 130   my $attribute   = shift;
 
 134     my ($precision, $self, $string) = @_;
 
 136     $self->$attribute($string ? $::locale->parse_date_to_object($string) : undef) if @_ > 2;
 
 138     my $dt = $self->$attribute;
 
 139     return undef unless $dt;
 
 141     $dt = DateTime->now if !ref($dt) && ($dt eq 'now');
 
 143     return $::locale->format_date_object($dt, precision => $precision);
 
 147   *{ $package . '::' . $attribute . '_as_timestamp'    } = sub { $accessor->('minute',      @_) };
 
 148   *{ $package . '::' . $attribute . '_as_timestamp_s'  } = sub { $accessor->('second',      @_) };
 
 149   *{ $package . '::' . $attribute . '_as_timestamp_ms' } = sub { $accessor->('millisecond', @_) };
 
 155   my ($package, $attribute, %params) = @_;
 
 158   *{ $package . '::' . $attribute . '_as_bool_yn' } = sub {
 
 162       die 'not an accessor';
 
 165     return !defined $self->$attribute ? ''
 
 166          :          $self->$attribute ? $::locale->text('Yes')
 
 167          :                              $::locale->text('No');
 
 182 SL::DB::Helper::Attr - attribute helpers
 
 186   use SL::DB::Helper::Attr;
 
 187   SL::DB::Helper::Attr::make($class,
 
 188     method_name => 'numeric(15,5)',
 
 191   SL::DB::Helper::Attr::auto_make($class);
 
 195 Makes attribute helpers.
 
 207 Sven Schöling <s.schoeling@linet-services.de>,
 
 208 Moritz Bunkus <m.bunkus@linet-services.de>