r1045@llin: dpavlin | 2006-09-29 21:38:42 +0200
authorDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 29 Sep 2006 19:52:17 +0000 (19:52 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 29 Sep 2006 19:52:17 +0000 (19:52 +0000)
 change low-level API to be OO (and remove various ugly cludges).

git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@726 07558da8-63fa-0310-ba24-9fe276d99e06

lib/WebPAC/Input.pm
lib/WebPAC/Input/ISIS.pm
lib/WebPAC/Input/MARC.pm

index 0ffa04a..7ac1a7c 100644 (file)
@@ -98,28 +98,16 @@ sub new {
        $log->logconfess("lookup argument is not suppored any more. rewrite call to lookup_ref") if ($self->{lookup});
 
        $log->logconfess("specify low-level file format module") unless ($self->{module});
-       my $module = $self->{module};
-       $module =~ s#::#/#g;
-       $module .= '.pm';
-       $log->debug("require low-level module $self->{module} from $module");
+       my $module_path = $self->{module};
+       $module_path =~ s#::#/#g;
+       $module_path .= '.pm';
+       $log->debug("require low-level module $self->{module} from $module_path");
 
-       require $module;
-       #eval $self->{module} .'->import';
+       require $module_path;
 
        # check if required subclasses are implemented
        foreach my $subclass (qw/open_db fetch_rec init dump_rec/) {
-               my $n = $self->{module} . '::' . $subclass;
-               if (! defined &{ $n }) {
-                       my $missing = "missing $subclass in $self->{module}";
-                       $self->{$subclass} = sub { $log->logwarn($missing) };
-               } else {
-                       $self->{$subclass} = \&{ $n };
-               }
-       }
-
-       if ($self->{init}) {
-               $log->debug("calling init");
-               $self->{init}->($self, @_);
+               # FIXME
        }
 
        $self->{'encoding'} ||= 'ISO-8859-2';
@@ -250,7 +238,9 @@ sub open {
        }
        $log->debug("rec_regex: ", Dumper($rec_regex)) if ($rec_regex);
 
-       my ($db, $size) = $self->{open_db}->( $self, 
+       my $class = $self->{module} || $log->logconfess("can't get low-level module name!");
+
+       my $ll_db = $class->new(
                path => $arg->{path},
 #              filter => sub {
 #                      my ($l,$f_nr) = @_;
@@ -262,11 +252,13 @@ sub open {
                %{ $arg },
        );
 
-       unless (defined($db)) {
+       unless (defined($ll_db)) {
                $log->logwarn("can't open database $arg->{path}, skipping...");
                return;
        }
 
+       my $size = $ll_db->size;
+
        unless ($size) {
                $log->logwarn("no records in database $arg->{path}, skipping...");
                return;
@@ -298,7 +290,7 @@ sub open {
 
                $log->debug("position: $pos\n");
 
-               my $rec = $self->{fetch_rec}->($self, $pos, sub {
+               my $rec = $ll_db->fetch_rec($pos, sub {
                                my ($l,$f_nr) = @_;
 #                              return unless defined($l);
 #                              return $l unless ($rec_regex && $f_nr);
@@ -348,7 +340,7 @@ sub open {
                if ($self->{stats}) {
 
                        # fetch clean record with regexpes applied for statistics
-                       my $rec = $self->{fetch_rec}->($self, $pos);
+                       my $rec = $ll_db->fetch_rec($pos);
 
                        foreach my $fld (keys %{ $rec }) {
                                $self->{_stats}->{fld}->{ $fld }++;
index 4b882c5..e8c9e12 100644 (file)
@@ -5,6 +5,7 @@ use strict;
 
 use WebPAC::Input;
 use Biblio::Isis 0.23;
+use base qw/WebPAC::Common/;
 
 =head1 NAME
 
@@ -12,11 +13,11 @@ WebPAC::Input::ISIS - support for CDS/ISIS database files
 
 =head1 VERSION
 
-Version 0.07
+Version 0.08
 
 =cut
 
-our $VERSION = '0.07';
+our $VERSION = '0.08';
 
 
 =head1 SYNOPSIS
@@ -24,16 +25,17 @@ our $VERSION = '0.07';
 Open CDS/ISIS, WinISIS or IsisMarc database using C<Biblio::Isis>
 and read all records to memory.
 
- my $isis = new WebPAC::Input::ISIS();
- $isis->open( path => '/path/to/ISIS/ISIS' );
+ my $isis = new WebPAC::Input::ISIS(
+       path => '/path/to/ISIS/ISIS',
+ );
 
 =head1 FUNCTIONS
 
-=head2 open_db
+=head2 new
 
-Returns handle to database and size in records
+Returns new low-level input API object
 
-  my ($db,$size) = $isis->open_db(
+  my $isis = new WebPAC::Input::ISIS(
        path => '/path/to/LIBRI'
        filter => sub {
                my ($l,$field_nr) = @_;
@@ -54,8 +56,10 @@ path to CDS/ISIS database
 
 =cut
 
-sub open_db {
-       my $self = shift;
+sub new {
+       my $class = shift;
+       my $self = {@_};
+       bless($self, $class);
 
        my $arg = {@_};
 
@@ -70,18 +74,16 @@ sub open_db {
                hash_filter => $arg->{filter} ? sub { return $arg->{filter}->(@_); } : undef,
        ) or $log->logdie("can't find database $arg->{path}");
 
-       my $size = $isis_db->count;
-
        $self->{_isis_db} = $isis_db;
 
-       return ($isis_db, $size);
+       $self ? return $self : return undef;
 }
 
 =head2 fetch_rec
 
 Return record with ID C<$mfn> from database
 
-  my $rec = $self->fetch_rec( $mfn, $filter_coderef);
+  my $rec = $isis->fetch_rec( $mfn, $filter_coderef);
 
 =cut
 
@@ -110,7 +112,7 @@ sub fetch_rec {
 
 Return dump of record ID C<$mfn> from database
 
-  my $rec = $self->dump_rec( $db, $mfn );
+  my $rec = $isis->dump_rec( $db, $mfn );
 
 =cut
 
@@ -122,6 +124,19 @@ sub dump_rec {
        return $self->{_isis_db}->to_ascii( $mfn );
 }
 
+=head2 size
+
+Return number of records in database
+
+  my $size = $isis->size;
+
+=cut
+
+sub size {
+       my $self = shift;
+       return $self->{_isis_db}->count;
+}
+
 =head1 AUTHOR
 
 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
index a5bce1f..987bab1 100644 (file)
@@ -4,6 +4,7 @@ use warnings;
 use strict;
 
 use MARC::Fast 0.03;
+use base qw/WebPAC::Common/;
 
 =head1 NAME
 
@@ -11,11 +12,11 @@ WebPAC::Input::MARC - support for MARC database files
 
 =head1 VERSION
 
-Version 0.05
+Version 0.06
 
 =cut
 
-our $VERSION = '0.05';
+our $VERSION = '0.06';
 
 
 =head1 SYNOPSIS
@@ -23,24 +24,27 @@ our $VERSION = '0.05';
 Open USMARC, Unimarc or any other file format that has same internal
 structure using C<MARC::Fast>.
 
- my $marc = new WebPAC::Input::MARC();
- $marc->open( path => '/path/to/marc.iso' );
+ my $marc = new WebPAC::Input::MARC(
+       path => '/path/to/marc.iso'
+ );
 
 =head1 FUNCTIONS
 
-=head2 open_db
+=head2 new
 
-Returns handle to database and size in records
+Returns new low-level input API object
 
-  my ($db,$size) = $open_db(
+  my $marc = new WebPAC::Input::MARC(
        path => '/path/to/marc.iso',
        filter => \&code_ref,
   }
 
 =cut
 
-sub open_db {
-       my $self = shift;
+sub new {
+       my $class = shift;
+       my $self = {@_};
+       bless($self, $class);
 
        my $arg = {@_};
 
@@ -57,7 +61,7 @@ sub open_db {
        $self->{_marc_size} = $db_size;
        $self->{_marc_db} = $db;
 
-       return ($db, $db_size);
+       $self ? return $self : return undef;
 }
 
 =head2 fetch_rec
@@ -84,14 +88,18 @@ sub fetch_rec {
        }
 }
 
-=head1 PROPERTIES
+=head2 size
 
-=head2 _marc_size
+Return number of records in database
 
-Store size of MARC database
+  my $size = $isis->size;
 
-  print $self->{_marc_size};
+=cut
 
+sub size {
+       my $self = shift;
+       return $self->{_marc_size};
+}
 
 
 =head1 AUTHOR