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 => 0) 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;
40 my $attribute = shift;
43 $params{places} = 2 if !defined($params{places});
46 *{ $package . '::' . $attribute . '_as_number' } = sub {
47 my ($self, $string) = @_;
49 $self->$attribute($::form->parse_amount(\%::myconfig, $string)) if @_ > 1;
51 return $::form->format_amount(\%::myconfig, $self->$attribute, $params{places});
57 my $attribute = shift;
60 $params{places} = 2 if !defined($params{places});
63 *{ $package . '::' . $attribute . '_as_percent' } = sub {
64 my ($self, $string) = @_;
66 $self->$attribute($::form->parse_amount(\%::myconfig, $string) / 100) if @_ > 1;
68 return $::form->format_amount(\%::myconfig, 100 * $self->$attribute, $params{places});
76 my $attribute = shift;
80 *{ $package . '::' . $attribute . '_as_date' } = sub {
81 my ($self, $string) = @_;
85 my ($yy, $mm, $dd) = $::locale->parse_date(\%::myconfig, $string);
86 $self->$attribute(DateTime->new(year => $yy, month => $mm, day => $dd));
88 $self->$attribute(undef);
92 return $self->$attribute
93 ? $::locale->reformat_date(
94 { dateformat => 'yy-mm-dd' },
95 ( $self->$attribute eq 'now'
99 $::myconfig{dateformat}
116 SL::DB::Helpers::Attr - attribute helpers
120 use SL::DB::Helpers::Attr;
121 SL::DB::Helpers::Attr::make($class,
122 method_name => 'numeric(15,5)',
125 SL::DB::Helpers::Attr::auto_make($class);