Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision b9cd11d7

Von Moritz Bunkus vor mehr als 3 Jahren hinzugefügt

  • ID b9cd11d78faef7815e435c11aa6a12793e9cb9c9
  • Vorgänger a206eca6
  • Nachfolger 9b16f8f8

AttrDuration für minutes: _in_hours und _in_hours_as_number

Generierte Helfer-Methoden für Attribute, die normalerweise Minuten
speichern. Die Funktion `attribute_in_hours` rechnet die Minuten in
Stunden um (beim Lesen) und umgekehrt (beim Schreiben).

`attribute_in_hours_as_number` formatiert zusätzlich den Wert in
Stunden zusätzlich in das Zahlenformat der Anwender*in bzw. parst
dieses Format beim Schreiben.

Unterschiede anzeigen:

SL/DB/Helper/AttrDuration.pm
116 116
    my $as_minutes = "${attribute}_as_minutes";
117 117
    return defined($self->$attribute) ? sprintf('%d:%02d', $self->$as_hours, $self->$as_minutes) : undef;
118 118
  };
119

  
120
  *{ $package . '::' . $attribute . '_in_hours' } = sub {
121
    my ($self, $value) = @_;
122

  
123
    $self->$attribute(int($value * 60 + 0.5)) if @_ > 1;
124
    return $self->$attribute / 60.0;
125
  };
126

  
127
  *{ $package . '::' . $attribute . '_in_hours_as_number' } = sub {
128
    my ($self, $value) = @_;
129

  
130
    my $sub = "${attribute}_in_hours";
131

  
132
    $self->$sub($::form->parse_amount(\%::myconfig, $value)) if @_ > 1;
133
    return $::form->format_amount(\%::myconfig, $self->$sub, 2);
134
  };
119 135
}
120 136

  
121 137
1;
......
234 250
e.g. C<1:30> for the value 90 minutes. Parsing such a string is
235 251
supported, too.
236 252

  
253
=item C<attribute_in_hours [$new_value]>
254

  
255
Access the full value but convert to and from hours when
256
reading/writing the value.
257

  
258
=item C<attribute_in_hours_as_number [$new_value]>
259

  
260
Access the full value but convert to and from hours when
261
reading/writing the value. The value is formatted to/parsed from the
262
user's number format.
263

  
237 264
=back
238 265

  
239 266
=head1 FUNCTIONS
t/db_helper/attr_duration.t
7 7
  columns => [
8 8
    dummy => { type => 'numeric', precision => 2, scale => 12 },
9 9
    inty  => { type => 'integer' },
10
    miny  => { type => 'integer' },
10 11
  ]
11 12
);
12 13

  
13 14
use SL::DB::Helper::AttrDuration;
14 15

  
15 16
__PACKAGE__->attr_duration('dummy');
16
__PACKAGE__->attr_duration_minutes('inty');
17
__PACKAGE__->attr_duration_minutes('inty', 'miny');
17 18

  
18 19
package main;
19 20

  
20
use Test::More tests => 120;
21
use Test::More tests => 130;
21 22
use Test::Exception;
22 23

  
23 24
use strict;
......
218 219
is($item->inty_as_hours,           3,      'write as_duration_string 03:1 read as_hours');
219 220
is($item->inty_as_duration_string, "3:01", 'write as_duration_string 03:1 read as_duration_string');
220 221

  
222
local %::myconfig = (numberformat => "1.000,00");
223

  
224
$item = new_item(miny_in_hours => 2.5);
225
is($item->miny,                    150,    'write in_hours 2.5 read raw');
226
is($item->miny_as_minutes,         30,     'write in_hours 2.5 read as_minutes');
227
is($item->miny_as_hours,           2,      'write in_hours 2.5 read as_hours');
228
is($item->miny_in_hours,           2.5,    'write in_hours 2.5 read in_hours');
229
is($item->miny_in_hours_as_number, '2,50', 'write in_hours 2.5 read in_hours_as_number');
230

  
231
$item = new_item(miny_in_hours_as_number => '4,25');
232
is($item->miny,                    255,    'write in_hours_as_number 4,25 read raw');
233
is($item->miny_as_minutes,         15,     'write in_hours_as_number 4,25 read as_minutes');
234
is($item->miny_as_hours,           4,      'write in_hours_as_number 4,25 read as_hours');
235
is($item->miny_in_hours,           4.25,   'write in_hours_as_number 4,25 read in_hours');
236
is($item->miny_in_hours_as_number, '4,25', 'write in_hours_as_number 4,25 read in_hours_as_number');
237

  
221 238
# Parametervalidierung
222 239
throws_ok { new_item()->inty_as_duration_string('invalid') } qr/invalid.*format/i, 'invalid duration format';
223 240

  

Auch abrufbar als: Unified diff