Skip to content

Commit

Permalink
atmandu::Importer::MARC cannot import from string reference, or IO::S…
Browse files Browse the repository at this point in the history
…tring #23
  • Loading branch information
phochste committed Jun 23, 2016
1 parent c865010 commit 5ce1898
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/Catmandu/Importer/MARC/Decoder.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ use Moo;

our $VERSION = '0.218';

sub fake_marc_file {
my ($self,$fh,$class) = @_;

my $obj = {
filename => scalar($fh),
fh => $fh,
recnum => 0,
warnings => [],
};

return( bless $obj , $class );
}

sub decode {
my ($self, $record, $id) = @_;
return unless eval { $record->isa('MARC::Record') };
Expand Down Expand Up @@ -46,4 +59,4 @@ sub decode {
return { _id => $sysid , record => \@result };
}

1;
1;
2 changes: 2 additions & 0 deletions lib/Catmandu/Importer/MARC/Lint.pm
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ sub generator {
my ($self) = @_;
my $lint = MARC::Lint->new;
my $file = MARC::File::USMARC->in($self->fh);
# MARC::File doesn't provide support for inline files
$file = $self->decoder->fake_marc_file($self->fh,'MARC::File::USMARC') unless $file;
sub {
my $marc = $file->next();
my $doc = $self->decoder->decode($marc,$self->id);
Expand Down
2 changes: 2 additions & 0 deletions lib/Catmandu/Importer/MARC/MARCMaker.pm
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ has decoder => (
sub generator {
my ($self) = @_;
my $file = MARC::File::MARCMaker->in($self->fh);
# MARC::File doesn't provide support for inline files
$file = $self->decoder->fake_marc_file($self->fh,'MARC::File::MARCMaker') unless $file;
sub {
$self->decoder->decode($file->next(),$self->id);
}
Expand Down
3 changes: 3 additions & 0 deletions lib/Catmandu/Importer/MARC/MiJ.pm
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ has decoder => (
sub generator {
my ($self) = @_;
my $file = MARC::File::MiJ->in($self->file);

# MARC::File doesn't provide support for inline files
$file = $self->decoder->fake_marc_file($self->fh,'MARC::File::MiJ') unless $file;
sub {
$self->decoder->decode($file->next(),$self->id);
}
Expand Down
2 changes: 2 additions & 0 deletions lib/Catmandu/Importer/MARC/MicroLIF.pm
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ has decoder => (
sub generator {
my ($self) = @_;
my $file = MARC::File::MicroLIF->in($self->fh);
# MARC::File doesn't provide support for inline files
$file = $self->decoder->fake_marc_file($self->fh,'MARC::File::MicroLIF') unless $file;
sub {
$self->decoder->decode($file->next(),$self->id);
}
Expand Down
10 changes: 10 additions & 0 deletions lib/Catmandu/Importer/MARC/USMARC.pm
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,27 @@ package Catmandu::Importer::MARC::USMARC;
use Catmandu::Sane;
use Moo;
use MARC::File::USMARC;
use Catmandu::Importer::MARC::Decoder;

our $VERSION = '0.218';

with 'Catmandu::Importer';

has id => (is => 'ro' , default => sub { '001' });
has records => (is => 'rw');
has decoder => (
is => 'ro',
lazy => 1 ,
builder => sub {
Catmandu::Importer::MARC::Decoder->new;
} );

sub generator {
my ($self) = @_;
my $file = MARC::File::USMARC->in($self->fh);

# MARC::File doesn't provide support for inline files
$file = $self->decoder->fake_marc_file($self->fh,'MARC::File::USMARC') unless $file;
sub {
$self->decode_marc($file->next());
}
Expand Down
4 changes: 4 additions & 0 deletions lib/Catmandu/Importer/MARC/XML.pm
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ has decoder => (
sub generator {
my ($self) = @_;
my $file = MARC::File::XML->in($self->fh);

# MARC::File doesn't provide support for inline files
$file = $self->decoder->fake_marc_file($self->fh,'MARC::File::XML') unless $file;

sub {
$self->decoder->decode($file->next(),$self->id);
}
Expand Down
25 changes: 25 additions & 0 deletions t/19-io-string.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/perl

use strict;
use warnings;

use Catmandu::Importer::MARC;
use MARC::File::USMARC;
use Test::Simple tests => 4;

my $data = join("",<DATA>);

my $importer = Catmandu::Importer::MARC->new(
file => \$data,
type => "USMARC"
);
my $records = $importer->to_array();

ok( @$records == 10, 'got all records' );
ok( $records->[0]->{'_id'} eq 'fol05731351 ', 'got _id' );
ok( $records->[0]->{'record'}->[1][-1] eq 'fol05731351 ', 'got subfield' );
ok( $records->[0]->{'_id'} eq $records->[0]->{'record'}->[1][-1],
'_id matches record id' );

__DATA__
00755cam 22002414a 4500001001300000003000600013005001700019008004100036010001700077020004300094040001800137042000800155050002600163082001700189100003100206245005400237260004200291300007200333500003300405650003700438630002500475630001300500fol05731351 IMchF20000613133448.0000107s2000 nyua 001 0 eng  a 00020737  a0471383147 (paper/cd-rom : alk. paper) aDLCcDLCdDLC apcc00aQA76.73.P22bM33 200000a005.13/32211 aMartinsson, Tobias,d1976-10aActivePerl with ASP and ADO /cTobias Martinsson. aNew York :bJohn Wiley & Sons,c2000. axxi, 289 p. :bill. ;c23 cm. +e1 computer laser disc (4 3/4 in.) a"Wiley Computer Publishing." 0aPerl (Computer program language)00aActive server pages.00aActiveX.00647pam 2200241 a 4500001001300000003000600013005001700019008004100036010001700077020001500094040001800109042000800127050002600135082001500161100002600176245006700202260003800269263000900307300001100316650003700327650002500364700001600389fol05754809 IMchF20000601115601.0000203s2000 mau 001 0 eng  a 00022023  a1565926994 aDLCcDLCdDLC apcc00aQA76.73.P22bD47 200000a005.742211 aDescartes, Alligator.10aProgramming the Perl DBI /cAlligator Descartes and Tim Bunce. aCmabridge, MA :bO'Reilly,c2000. a1111 ap. cm. 0aPerl (Computer program language) 0aDatabase management.1 aBunce, Tim.00605cam 22002054a 4500001001300000003000600013005001700019008004100036010001700077040001800094042000800112050002700120082001700147100002100164245005500185260004500240300002600285504005100311650003700362fol05843555 IMchF20000525142739.0000318s1999 cau b 001 0 eng  a 00501349  aDLCcDLCdDLC apcc00aQA76.73.P22bB763 199900a005.13/32211 aBrown, Martin C.10aPerl :bprogrammer's reference /cMartin C. Brown. aBerkeley :bOsborne/McGraw-Hill,cc1999. axix, 380 p. ;c22 cm. aIncludes bibliographical references and index. 0aPerl (Computer program language)00579cam 22002054a 4500001001300000003000600013005001700019008004100036010001700077020001500094040001800109042000800127050002700135082001700162100002100179245005500200260004500255300003600300650003700336fol05843579 IMchF20000525142716.0000318s1999 caua 001 0 eng  a 00502116  a0072120002 aDLCcDLCdDLC apcc00aQA76.73.P22bB762 199900a005.13/32211 aBrown, Martin C.10aPerl :bthe complete reference /cMartin C. Brown. aBerkeley :bOsborne/McGraw-Hill,cc1999. axxxv, 1179 p. :bill. ;c24 cm. 0aPerl (Computer program language)00801nam 22002778a 4500001001300000003000600013005001700019008004100036010001700077020001500094040001300109042000800122050002600130082001800156100002000174245008800194250003200282260004100314263000900355300001100364650003700375650003600412650002600448700002500474700002400499fol05848297 IMchF20000524125727.0000518s2000 mau 001 0 eng  a 00041664  a1565924193 aDLCcDLC apcc00aQA76.73.P22bG84 200000a005.2/7622211 aGuelich, Scott.10aCGI programming with Perl /cScott Guelich, Shishir Gundavaram & Gunther Birznieks. a2nd ed., expanded & updated aCambridge, Mass. :bO'Reilly,c2000. a0006 ap. cm. 0aPerl (Computer program language) 0aCGI (Computer network protocol) 0aInternet programming.1 aGundavaram, Shishir.1 aBirznieks, Gunther.00665nam 22002298a 4500001001300000003000600013005001700019008004100036010001700077020001500094040001300109042000800122050002700130082001700157111005200174245008600226250001200312260004100324263000900365300001100374650005000385fol05865950 IMchF20000615103017.0000612s2000 mau 100 0 eng  a 00055759  a0596000138 aDLCcDLC apcc00aQA76.73.P22bP475 200000a005.13/32212 aPerl Conference 4.0d(2000 :cMonterey, Calif.)10aProceedings of the Perl Conference 4.0 :bJuly 17-20, 2000, Monterey, California. a1st ed. aCambridge, Mass. :bO'Reilly,c2000. a0006 ap. cm. 0aPerl (Computer program language)vCongresses.00579nam 22002178a 4500001001300000003000600013005001700019008004100036010001700077020001500094040001300109042000800122050002600130082001700156100002800173245006200201260004100263263000900304300001100313650003700324fol05865956 IMchF20000615102948.0000612s2000 mau 000 0 eng  a 00055770  a1565926099 aDLCcDLC apcc00aQA76.73.P22bB43 200000a005.13/32211 aBlank-Edelman, David N.10aPerl for system administration /cDavid N. Blank-Edelman. aCambridge, Mass. :bO'Reilly,c2000. a0006 ap. cm. 0aPerl (Computer program language)00661nam 22002538a 4500001001300000003000600013005001700019008004100036010001700077020001500094040001300109042000800122050002600130082001700156100001700173245006700190250001200257260004100269263000900310300001100319650003700330700002300367700001700390fol05865967 IMchF20000615102611.0000614s2000 mau 000 0 eng  a 00055799  a0596000278 aDLCcDLC apcc00aQA76.73.P22bW35 200000a005.13/32211 aWall, Larry.10aProgramming Perl /cLarry Wall, Tom Christiansen & Jon Orwant. a3rd ed. aCambridge, Mass. :bO'Reilly,c2000. a0007 ap. cm. 0aPerl (Computer program language)1 aChristiansen, Tom.1 aOrwant, Jon.00603cam 22002054a 4500001001300000003000600013005001700019008004100036010001700077020001500094040001800109042000800127050002600135082001700161100003200178245006000210260005700270300003300327650003700360fol05872355 IMchF20000706095105.0000315s1999 njua 001 0 eng  a 00500678  a013020868X aDLCcDLCdDLC apcc00aQA76.73.P22bL69 199900a005.13/32211 aLowe, Vincentq(Vincent D.)10aPerl programmer's interactive workbook /cVincent Lowe. aUpper Saddle River, NJ :bPrentice Hall PTP,cc1999. axx, 633 p. :bill. ;c23 cm. 0aPerl (Computer program language)00696nam 22002538a 4500001001300000003000600013005001700019008004100036010001700077020002800094040001300122042000800135050002600143082001700169100002600186245004400212260005100256263000900307300001100316500002000327650003700347650001700384650004100401fol05882032 IMchF20000707091904.0000630s2000 cau 001 0 eng  a 00058174  a0764547291 (alk. paper) aDLCcDLC apcc00aQA76.73.P22bF64 200000a005.13/32212 aFoster-Johnson, Eric.10aCross-platform Perl /cEric F. Johnson. aFoster City, CA :bIDG Books Worldwide,c2000. a0009 ap. cm. aIncludes index. 0aPerl (Computer program language) 0aWeb servers. 0aCross-platform software development.

0 comments on commit 5ce1898

Please sign in to comment.