[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
SF.net SVN: ledger-smb:[6514] trunk
- Subject: SF.net SVN: ledger-smb:[6514] trunk
- From: ..hidden..
- Date: Fri, 17 Jan 2014 14:04:39 +0000
Revision: 6514
http://sourceforge.net/p/ledger-smb/code/6514
Author: einhverfr
Date: 2014-01-17 14:04:38 +0000 (Fri, 17 Jan 2014)
Log Message:
-----------
Initial commit of LedgerSMB::Request for declarative requirements of inputs. This should make for better error handling. I would like to backport a stripped down version to 1.3 so we can avoid cryptic 'required input not provided' errors to users.
Modified Paths:
--------------
trunk/LedgerSMB/Form.pm
trunk/LedgerSMB.pm
Added Paths:
-----------
trunk/LedgerSMB/Request.pm
Modified: trunk/LedgerSMB/Form.pm
===================================================================
--- trunk/LedgerSMB/Form.pm 2014-01-17 13:33:26 UTC (rev 6513)
+++ trunk/LedgerSMB/Form.pm 2014-01-17 14:04:38 UTC (rev 6514)
@@ -72,6 +72,7 @@
use charnames qw(:full);
use open ':utf8';
package Form;
+use base qw(LedgerSMB::Request);
use utf8;
our $logger = Log::Log4perl->get_logger('LedgerSMB::Form');
Added: trunk/LedgerSMB/Request.pm
===================================================================
--- trunk/LedgerSMB/Request.pm (rev 0)
+++ trunk/LedgerSMB/Request.pm 2014-01-17 14:04:38 UTC (rev 6514)
@@ -0,0 +1,138 @@
+=head1 NAME
+
+LedgerSMB::Request - Basic request handling routines for LedgerSMB
+
+=head1 SYNPOSIS
+
+ $request->requires('myattribute1', 'myattribute2');
+
+Error: Attribute 'myattribute2' is not provided.
+
+ $request->requires_series(1, 12, 'myattribute1', 'myattribute2');
+
+Error: Attribute 'myattribute2_10' is not provided.
+
+ $request->numbers('debits', 'credits');
+ $request->numbers_series(1, 10, 'amount');
+ $request->dates('date_from', 'date_to');
+ $request->dates_series(1, $end, 'shipdate');
+
+=cut
+
+package LedgerSMB::Request;
+use LedgerSMB::App_State;
+use LedgerSMB::PGNumber;
+use LedgerSMB::PGDate;
+
+=head1 DESCRIPTION
+
+This package provides methods (as an interface package, as of 1.4) for both new
+and old code to use for declarative handling of required inputs, dates, and
+amounts.
+
+In future versions, this may take on more of the role found in LedgerSMB.pm
+today, but hopefully with a lot less cruft. It isn't clear we will use
+CGI::Simple or rely on a specific interface and so some portability in request
+handling will be required. That's where this module comes in.
+
+=head1 METHODS
+
+=head2 requires(@attribute_names)
+
+Raises an exception if any member of the argument list corresponds to a non-key
+in the current hash or an empty string. '0' does pass however.
+
+=cut
+
+sub requires {
+ my $self = shift @_;
+ for (@_){
+ die "Required attribute not provided: %1", $_
+ unless $self->{$_} or $self->{$_} eq '0';
+ }
+}
+
+=head2 requries_series($start, $stop, @attnames)
+
+Generates and checks a series of attributes in the form of ${attname}_$count
+from $start to $stop, for each in @attnames
+
+=cut
+
+sub requires_series {
+ my $self = shift @_;
+ my $start = shift @_;
+ my $end = shift @_;
+ for my $att (@_){
+ $self->requires("${att}_$_") for ($start .. $stop);
+ }
+}
+
+=head2 numbers(@attnames)
+
+Transforms every $self->{$attname} into a LedgerSMB::PGNumber instance based on
+from_input. This is mostly of interest for old pre-1.3 code in place of
+parse_amount, or for add-ons written with Moo instead of Moose.
+
+=cut
+
+sub numbers {
+ my $self = shift @_;
+ $self->{$_} = LedgerSMB::PGNumber->from_input($self->{$_}) for @_;
+}
+
+=head2 numbers_series($start, $stop, @attnames)
+
+Like numbers() above, except uses start and stop to generate attribute lists.
+This can be useful for larger series of numbers where line items are not
+directly handled by Moose (yet) or where old code is concerned.
+
+=cut
+
+sub numbers_series {
+ my $self = shift @_;
+ my $start = shift @_;
+ my $end = shift @_;
+ for my $att (@_){
+ $self->numbers("${att}_$_") for ($start .. $stop);
+ }
+}
+
+=head2 dates (@attnames)
+
+Like numbers() above, but converts to LedgerSMB::PGDate objects instead,.
+
+=cut
+
+
+sub dates {
+ my $self = shift @_;
+ $self->{$_} = LedgerSMB::PGDate->from_input($self->{$_}) for @_;
+}
+
+=head2 dates_series ($start, $stop, @attnames)
+
+Like numbers_series above but with PGDate objects instead.
+
+=cut
+
+sub dates_series {
+ my $self = shift @_;
+ my $start = shift @_;
+ my $end = shift @_;
+ for my $att (@_){
+ $self->dates("${att}_$_") for ($start .. $stop);
+ }
+}
+
+=head1 COPYRIGHT
+
+Copyright (C) 2014 The LedgerSMB Core Team.
+
+This file may be re-used under the terms of the GNU General Public License
+version 2 or at your option any later version. Please see the LICENSE.txt for
+details.
+
+=cut
+
+1;
Modified: trunk/LedgerSMB.pm
===================================================================
--- trunk/LedgerSMB.pm 2014-01-17 13:33:26 UTC (rev 6513)
+++ trunk/LedgerSMB.pm 2014-01-17 14:04:38 UTC (rev 6514)
@@ -214,6 +214,7 @@
$CGI::Simple::POST_MAX = -1;
package LedgerSMB;
+use base qw(LedgerSMB::Request);
our $VERSION = '1.4.0';
my $logger = Log::Log4perl->get_logger('LedgerSMB');
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
------------------------------------------------------------------------------
CenturyLink Cloud: The Leader in Enterprise Cloud Services.
Learn Why More Businesses Are Choosing CenturyLink Cloud For
Critical Workloads, Development Environments & Everything In Between.
Get a Quote or Start a Free Trial Today.
http://pubads.g.doubleclick.net/gampad/clk?id=119420431&iu=/4140/ostg.clktrk
_______________________________________________
Ledger-smb-commits mailing list
..hidden..
https://lists.sourceforge.net/lists/listinfo/ledger-smb-commits