my $as_minutes = "${attribute}_as_minutes";
return defined($self->$attribute) ? sprintf('%d:%02d', $self->$as_hours, $self->$as_minutes) : undef;
};
+
+ *{ $package . '::' . $attribute . '_in_hours' } = sub {
+ my ($self, $value) = @_;
+
+ $self->$attribute(int($value * 60 + 0.5)) if @_ > 1;
+ return $self->$attribute / 60.0;
+ };
+
+ *{ $package . '::' . $attribute . '_in_hours_as_number' } = sub {
+ my ($self, $value) = @_;
+
+ my $sub = "${attribute}_in_hours";
+
+ $self->$sub($::form->parse_amount(\%::myconfig, $value)) if @_ > 1;
+ return $::form->format_amount(\%::myconfig, $self->$sub, 2);
+ };
}
1;
e.g. C<1:30> for the value 90 minutes. Parsing such a string is
supported, too.
+=item C<attribute_in_hours [$new_value]>
+
+Access the full value but convert to and from hours when
+reading/writing the value.
+
+=item C<attribute_in_hours_as_number [$new_value]>
+
+Access the full value but convert to and from hours when
+reading/writing the value. The value is formatted to/parsed from the
+user's number format.
+
=back
=head1 FUNCTIONS
columns => [
dummy => { type => 'numeric', precision => 2, scale => 12 },
inty => { type => 'integer' },
+ miny => { type => 'integer' },
]
);
use SL::DB::Helper::AttrDuration;
__PACKAGE__->attr_duration('dummy');
-__PACKAGE__->attr_duration_minutes('inty');
+__PACKAGE__->attr_duration_minutes('inty', 'miny');
package main;
-use Test::More tests => 120;
+use Test::More tests => 130;
use Test::Exception;
use strict;
is($item->inty_as_hours, 3, 'write as_duration_string 03:1 read as_hours');
is($item->inty_as_duration_string, "3:01", 'write as_duration_string 03:1 read as_duration_string');
+local %::myconfig = (numberformat => "1.000,00");
+
+$item = new_item(miny_in_hours => 2.5);
+is($item->miny, 150, 'write in_hours 2.5 read raw');
+is($item->miny_as_minutes, 30, 'write in_hours 2.5 read as_minutes');
+is($item->miny_as_hours, 2, 'write in_hours 2.5 read as_hours');
+is($item->miny_in_hours, 2.5, 'write in_hours 2.5 read in_hours');
+is($item->miny_in_hours_as_number, '2,50', 'write in_hours 2.5 read in_hours_as_number');
+
+$item = new_item(miny_in_hours_as_number => '4,25');
+is($item->miny, 255, 'write in_hours_as_number 4,25 read raw');
+is($item->miny_as_minutes, 15, 'write in_hours_as_number 4,25 read as_minutes');
+is($item->miny_as_hours, 4, 'write in_hours_as_number 4,25 read as_hours');
+is($item->miny_in_hours, 4.25, 'write in_hours_as_number 4,25 read in_hours');
+is($item->miny_in_hours_as_number, '4,25', 'write in_hours_as_number 4,25 read in_hours_as_number');
+
# Parametervalidierung
throws_ok { new_item()->inty_as_duration_string('invalid') } qr/invalid.*format/i, 'invalid duration format';