cleanup WebPAC::Input
authorDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 21 Sep 2009 16:42:25 +0000 (16:42 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 21 Sep 2009 16:42:25 +0000 (16:42 +0000)
git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@1307 07558da8-63fa-0310-ba24-9fe276d99e06

lib/WebPAC/Input.pm
t/2-input.t

index 0eb056f..1cab26a 100644 (file)
@@ -182,9 +182,7 @@ sub open {
        my $input_encoding = $arg->{'input_encoding'} || $self->{'input_encoding'} || 'cp852';
 
        # store data in object
-       foreach my $v (qw/path offset limit/) {
-               $self->{$v} = $arg->{$v} if defined $arg->{$v};
-       }
+       $self->{$_} = $arg->{$_} foreach grep { defined $arg->{$_} } qw(path offset limit);
 
        if ($arg->{load_row} || $arg->{save_row}) {
                $log->logconfess("save_row and load_row must be defined in pair and be CODE") unless (
@@ -276,9 +274,6 @@ sub open {
                $to_rec = $size if ($to_rec > $size);
        }
 
-       # store size for later
-       $self->{size} = $to_rec - $from_rec + 1;
-
        my $strict_encoding = $arg->{strict_encoding} || $self->{strict_encoding}; ## FIXME should be 1 really
 
        $log->info("processing $self->{size}/$size records [$from_rec-$to_rec]",
@@ -286,11 +281,15 @@ sub open {
                $self->{stats} ? ' [stats]' : '',
        );
 
+       $self->{size} = 0;
+
        # read database
        for (my $pos = $from_rec; $pos <= $to_rec; $pos++) {
 
                $log->debug("position: $pos\n");
 
+               $self->{size}++; # XXX I could move this more down if I didn't want empty records...
+
                my $rec = $ll_db->fetch_rec($pos, sub {
                                my ($l,$f_nr,$debug) = @_;
 #                              return unless defined($l);
@@ -473,7 +472,6 @@ because it takes into account C<offset> and C<limit>.
 
 sub size {
        my $self = shift;
-       $self->{ll_db}->size if $self->{ll_db}->can('size');
        return $self->{size}; # FIXME this is buggy if open is called multiple times!
 }
 
index 052b586..e5bfd3b 100755 (executable)
@@ -98,8 +98,8 @@ sub test_start_limit($$$$) {
        cmp_ok($input->size, '==', $e, "input->size = $e");
 }
 
-test_start_limit($input, 1, 3, 3);
-test_start_limit($input, $size, 3, 0);
+test_start_limit($input, 1, 0, $size - 1);
+test_start_limit($input, $size, 0, 0);
 test_start_limit($input, 3, $size, $size - 3);
 test_start_limit($input, 1, $size, $size - 1);