added hash_filter as option when calling to_hash [0.12]
authorDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 22 Aug 2013 11:24:36 +0000 (11:24 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 22 Aug 2013 11:35:44 +0000 (13:35 +0200)
git-svn-id: svn+ssh://llin/home/dpavlin/private/svn/MARC-Fast/trunk@47 49f9634a-d7ec-0310-8e6b-ec35c6cc8804

lib/MARC/Fast.pm
t/004_marc-hash_filter.t [new file with mode: 0755]

index dc2f303..3aa4c66 100644 (file)
@@ -7,7 +7,7 @@ use Data::Dump qw/dump/;
 BEGIN {
        use Exporter ();
        use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
-       $VERSION     = 0.11;
+       $VERSION     = 0.12;
        @ISA         = qw (Exporter);
        #Give a hoot don't pollute, do not export more than needed by default
        @EXPORT      = qw ();
@@ -277,11 +277,14 @@ sub last_leader {
 
 Read record with specified MFN and convert it to hash
 
-  my $hash = $marc->to_hash( $mfn, include_subfields => 1, );
+  my $hash = $marc->to_hash( $mfn, include_subfields => 1,
+       hash_filter => sub { my ($l,$tag) = @_; return $l; }
+  );
 
 It has ability to convert characters (using C<hash_filter>) from MARC
 database before creating structures enabling character re-mapping or quick
-fix-up of data.
+fix-up of data. If you specified C<hash_filter> both in C<new> and C<to_hash>
+only the one from C<to_hash> will be used.
 
 This function returns hash which is like this:
 
@@ -305,6 +308,7 @@ sub to_hash {
        my $mfn = shift || confess "need mfn!";
 
        my $args = {@_};
+       my $filter_coderef = $args->{'hash_filter'} || $self->{'hash_filter'};
 
        # init record to include MFN as field 000
        my $rec = { '000' => [ $mfn ] };
@@ -318,7 +322,7 @@ sub to_hash {
                        $l =~ s/\x1E$//;
 
                        # filter output
-                       $l = $self->{'hash_filter'}->($l, $tag) if ($self->{'hash_filter'});
+                       $l = $filter_coderef->($l, $tag) if $filter_coderef;
 
                        my $val;
 
diff --git a/t/004_marc-hash_filter.t b/t/004_marc-hash_filter.t
new file mode 100755 (executable)
index 0000000..c637662
--- /dev/null
@@ -0,0 +1,46 @@
+#!/usr/bin/perl -w
+
+use strict;
+use blib;
+
+use Test::More tests => 9;
+use Data::Dump qw/dump/;
+
+BEGIN {
+       use_ok( 'MARC::Fast' );
+       use_ok( 'Encode' );
+}
+
+my $debug = shift @ARGV;
+
+my $marc_file = 't/utf8.marc';
+
+ok(my $marc = MARC::Fast->new(
+       marcdb => $marc_file,
+       hash_filter => sub {
+               my ($l, $tag) = @_;
+               $l = Encode::decode( 'utf-8', $l );
+               $l =~ s/knjiga/briga/;
+               return $l;
+       },
+), "new");
+
+cmp_ok($marc->count, '==', 1, 'count' );
+
+ok(my $rec = $marc->fetch(1), "fetch 1");
+diag dump $rec if $debug;
+
+ok(my $hash = $marc->to_hash(1), "to_hash 1");
+diag dump $hash if $debug;
+
+cmp_ok( $hash->{260}->[0]->{'b'}, 'eq', "\x{160}kolska briga,", 'hash_filter from new' );
+
+ok($hash = $marc->to_hash(1, hash_filter => sub {
+               my ($l, $tag) = @_;
+               $l = Encode::decode( 'utf-8', $l );
+               $l =~ s/knjiga/zabava/;
+               return $l;
+}), "to_hash 1 with hash_filter");
+diag dump $hash if $debug;
+
+cmp_ok( $hash->{260}->[0]->{'b'}, 'eq', "\x{160}kolska zabava,", 'hash_filter from to_hash' );