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_bool_yn($package, $name) if $type =~ /bool/xi;
41 my $attribute = shift;
44 $params{places} = 2 if !defined($params{places});
47 *{ $package . '::' . $attribute . '_as_number' } = sub {
48 my ($self, $string) = @_;
50 $self->$attribute($::form->parse_amount(\%::myconfig, $string)) if @_ > 1;
52 return $::form->format_amount(\%::myconfig, $self->$attribute, $params{places});
58 my $attribute = shift;
61 $params{places} = 2 if !defined($params{places});
64 *{ $package . '::' . $attribute . '_as_percent' } = sub {
65 my ($self, $string) = @_;
67 $self->$attribute($::form->parse_amount(\%::myconfig, $string) / 100) if @_ > 1;
69 return $::form->format_amount(\%::myconfig, 100 * $self->$attribute, $params{places});
77 my $attribute = shift;
81 *{ $package . '::' . $attribute . '_as_date' } = sub {
82 my ($self, $string) = @_;
86 my ($yy, $mm, $dd) = $::locale->parse_date(\%::myconfig, $string);
87 $self->$attribute(DateTime->new(year => $yy, month => $mm, day => $dd));
89 $self->$attribute(undef);
93 return $self->$attribute
94 ? $::locale->reformat_date(
95 { dateformat => 'yy-mm-dd' },
96 ( $self->$attribute eq 'now'
100 $::myconfig{dateformat}
109 my ($package, $attribute, %params) = @_;
112 *{ $package . '::' . $attribute . '_as_bool_yn' } = sub {
116 die 'not an accessor';
119 return !defined $self->$attribute ? ''
120 : $self->$attribute ? $::locale->text('Yes')
121 : $::locale->text('No');
136 SL::DB::Helper::Attr - attribute helpers
140 use SL::DB::Helper::Attr;
141 SL::DB::Helper::Attr::make($class,
142 method_name => 'numeric(15,5)',
145 SL::DB::Helper::Attr::auto_make($class);
149 Makes attribute helpers.
161 Sven Schöling <s.schoeling@linet-services.de>