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_percent($package, $name, places =>  2) if $type =~ /numeric | real | float/xi;
 
  34   _as_number ($package, $name, places =>  0) if $type =~ /int/xi;
 
  35   _as_date   ($package, $name)               if $type =~ /date | timestamp/xi;
 
  36   _as_timestamp($package, $name)             if $type =~ /timestamp/xi;
 
  37   _as_bool_yn($package, $name)               if $type =~ /bool/xi;
 
  42   my $attribute   = shift;
 
  45   $params{places} = 2 if !defined($params{places});
 
  48   *{ $package . '::' . $attribute . '_as_number' } = sub {
 
  49     my ($self, $string) = @_;
 
  51     $self->$attribute($::form->parse_amount(\%::myconfig, $string)) if @_ > 1;
 
  53     return $::form->format_amount(\%::myconfig, $self->$attribute, $params{places});
 
  59   my $attribute   = shift;
 
  62   $params{places} = 2 if !defined($params{places});
 
  65   *{ $package . '::' . $attribute . '_as_percent' } = sub {
 
  66     my ($self, $string) = @_;
 
  68     $self->$attribute($::form->parse_amount(\%::myconfig, $string) / 100) if @_ > 1;
 
  70     return $::form->format_amount(\%::myconfig, 100 * $self->$attribute, $params{places});
 
  78   my $attribute   = shift;
 
  82   *{ $package . '::' . $attribute . '_as_date' } = sub {
 
  83     my ($self, $string) = @_;
 
  87         my ($yy, $mm, $dd) = $::locale->parse_date(\%::myconfig, $string);
 
  88         $self->$attribute(DateTime->new(year => $yy, month => $mm, day => $dd));
 
  90         $self->$attribute(undef);
 
  94     return $self->$attribute
 
  95       ? $::locale->reformat_date(
 
  96           { dateformat => 'yy-mm-dd' },
 
  97           ( ($self->$attribute eq 'now' || $self->$attribute eq 'now()')
 
 101           $::myconfig{dateformat}
 
 111   my $attribute   = shift;
 
 115     my ($precision, $self, $string) = @_;
 
 117     $self->$attribute($string ? $::locale->parse_date_to_object($string) : undef) if @_ > 2;
 
 119     my $dt = $self->$attribute;
 
 120     return undef unless $dt;
 
 122     $dt = DateTime->now if !ref($dt) && ($dt eq 'now');
 
 124     return $::locale->format_date_object($dt, precision => $precision);
 
 128   *{ $package . '::' . $attribute . '_as_timestamp'    } = sub { $accessor->('minute',      @_) };
 
 129   *{ $package . '::' . $attribute . '_as_timestamp_s'  } = sub { $accessor->('second',      @_) };
 
 130   *{ $package . '::' . $attribute . '_as_timestamp_ms' } = sub { $accessor->('millisecond', @_) };
 
 136   my ($package, $attribute, %params) = @_;
 
 139   *{ $package . '::' . $attribute . '_as_bool_yn' } = sub {
 
 143       die 'not an accessor';
 
 146     return !defined $self->$attribute ? ''
 
 147          :          $self->$attribute ? $::locale->text('Yes')
 
 148          :                              $::locale->text('No');
 
 163 SL::DB::Helper::Attr - attribute helpers
 
 167   use SL::DB::Helper::Attr;
 
 168   SL::DB::Helper::Attr::make($class,
 
 169     method_name => 'numeric(15,5)',
 
 172   SL::DB::Helper::Attr::auto_make($class);
 
 176 Makes attribute helpers.
 
 188 Sven Schöling <s.schoeling@linet-services.de>,
 
 189 Moritz Bunkus <m.bunkus@linet-services.de>