From 8944e41547df93c8b25943938bb42ddec101fc16 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Thu, 6 Oct 2011 12:20:43 +0200 Subject: [PATCH] _bool_yn accessor --- SL/DB/Helper/Attr.pm | 18 ++++++++++++++++++ t/helper/attr.t | 12 +++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/SL/DB/Helper/Attr.pm b/SL/DB/Helper/Attr.pm index 94b53dd7f..f7fabfe89 100644 --- a/SL/DB/Helper/Attr.pm +++ b/SL/DB/Helper/Attr.pm @@ -33,6 +33,7 @@ sub _make_by_type { _as_percent($package, $name, places => 2) if $type =~ /numeric | real | float/xi; _as_number ($package, $name, places => 0) if $type =~ /int/xi; _as_date ($package, $name) if $type =~ /date | timestamp/xi; + _as_bool_yn($package, $name) if $type =~ /bool/xi; } sub _as_number { @@ -104,6 +105,23 @@ sub _as_date { return 1; } +sub _as_bool_yn { + my ($package, $attribute, %params) = @_; + + no strict 'refs'; + *{ $package . '::' . $attribute . '_as_bool_yn' } = sub { + my ($self) = @_; + + if (@_ > 1) { + die 'not an accessor'; + } + + return !defined $self->$attribute ? '' + : $self->$attribute ? $::locale->text('Yes') + : $::locale->text('No'); + } +} + 1; diff --git a/t/helper/attr.t b/t/helper/attr.t index 3fc0607d5..5d6238ed9 100644 --- a/t/helper/attr.t +++ b/t/helper/attr.t @@ -1,4 +1,4 @@ -use Test::More tests => 29; +use Test::More tests => 32; use lib 't'; @@ -50,3 +50,13 @@ $i->amount(12); $i->netamount(10.34); is($i->taxamount_as_number, '1,66'); +$o->closed(1); +is $o->closed_as_bool_yn, 'Ja', 'bool 1'; +$o->closed(0); +is $o->closed_as_bool_yn, 'Nein', 'bool 2'; + +# undef test: this only works for columns without default, rose will set +# defaults according to the database +$i->taxincluded(undef); +is $i->taxincluded_as_bool_yn, '', 'bool 3'; + -- 2.20.1