[libcatmandu-marc-perl] 13/26: Adding support for variables in marc_each

Jonas Smedegaard dr at jones.dk
Tue Dec 19 12:17:04 UTC 2017


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag upstream/1.23.1
in repository libcatmandu-marc-perl.

commit cf566071f09584136c7a4c23ccb9e86e466c94ad
Author: Patrick Hochstenbach <patrick.hochstenbach at ugent.be>
Date:   Sat Dec 2 10:23:34 2017 +0100

    Adding support for variables in marc_each
---
 Build.PL                           |  4 ++--
 Changes                            |  4 +++-
 lib/Catmandu/Fix/Bind/marc_each.pm | 49 ++++++++++++++++++++++++++++++++------
 lib/Catmandu/Fix/marc_copy.pm      | 20 +++++++---------
 lib/Catmandu/Fix/marc_cut.pm       | 20 +++++++---------
 lib/Catmandu/Fix/marc_paste.pm     | 11 ++++-----
 lib/Catmandu/MARC.pm               |  4 +++-
 7 files changed, 70 insertions(+), 42 deletions(-)

diff --git a/Build.PL b/Build.PL
index a792082..a00114b 100644
--- a/Build.PL
+++ b/Build.PL
@@ -1,5 +1,5 @@
 
-# This file was automatically generated by Dist::Zilla::Plugin::ModuleBuild v6.008.
+# This file was automatically generated by Dist::Zilla::Plugin::ModuleBuild v6.005.
 use strict;
 use warnings;
 
@@ -18,7 +18,7 @@ my %module_build_args = (
     "Patrick Hochstenbach, C<< <patrick.hochstenbach at ugent.be> >>"
   ],
   "dist_name" => "Catmandu-MARC",
-  "dist_version" => "1.21",
+  "dist_version" => "1.22",
   "license" => "perl",
   "module_name" => "Catmandu::MARC",
   "recursive_test_files" => 1,
diff --git a/Changes b/Changes
index ed3e21d..4412bfa 100644
--- a/Changes
+++ b/Changes
@@ -1,7 +1,9 @@
 Revision history for Catmandu-MARC
 
 {{$NEXT}}
-
+  - Add support for passing a loop variable in marc_each
+  - Making the syntax of marc_copy and marc_paste more intuitive for marc_each loops
+  
 1.21  2017-11-13 14:21:16 CET
   - Fixing ^ to space conversion in ALEPHSEQ imports
 
diff --git a/lib/Catmandu/Fix/Bind/marc_each.pm b/lib/Catmandu/Fix/Bind/marc_each.pm
index e726b16..242702b 100644
--- a/lib/Catmandu/Fix/Bind/marc_each.pm
+++ b/lib/Catmandu/Fix/Bind/marc_each.pm
@@ -1,27 +1,32 @@
 package Catmandu::Fix::Bind::marc_each;
 
 use Moo;
+use Catmandu::Sane;
 use Catmandu::Util;
+use Catmandu::MARC;
+use Catmandu::Fix::Has;
+use namespace::clean;
 
 our $VERSION = '1.21';
 
+has var    => (fix_opt => 1);
+has __marc => (is => 'lazy');
+
 with 'Catmandu::Fix::Bind', 'Catmandu::Fix::Bind::Group';
 
-has done => (is => 'ro');
+sub _build___marc {
+    Catmandu::MARC->instance;
+}
 
 sub unit {
     my ($self,$data) = @_;
 
-    $self->{done} = 0;
-
     $data;
 }
 
 sub bind {
     my ($self,$mvar,$code) = @_;
 
-    return $mvar if $self->done;
-
     my $rows = $mvar->{record} // [];
 
     my @new = ();
@@ -30,15 +35,21 @@ sub bind {
 
         $mvar->{record} = [$row];
 
+        if ($self->var) {
+            $mvar->{$self->var} = $self->__marc->marc_copy($mvar,"***")->[0]->[0];
+        }
+
         my $fixed = $code->($mvar);
 
         push @new , @{$fixed->{record}} if defined $fixed && exists $fixed->{record} && defined $fixed->{record};
+
+        if ($self->var) {
+            delete $mvar->{$self->var};
+        }
     }
 
     $mvar->{record} = \@new if exists $mvar->{record};
 
-    $self->{done} = 1;
-
     $mvar;
 }
 
@@ -64,6 +75,14 @@ Catmandu::Fix::Bind::marc_each - a binder that loops over MARC fields
         end
     end
 
+    # Loop over all the fields with a variable (see marc_copy, marc_cut and marc_paste for the content)
+    do marc_each(var:this)
+        if all_match(this.tag,300)
+          # The '***' is short for the current tag in a marc_each loop
+          marc_map(***a,test)
+        end
+    end
+
 =head1 DESCRIPTION
 
 The marc_each binder will iterate over each individual MARC field and execute the fixes only
@@ -95,6 +114,22 @@ you can write:
         end
     end
 
+A variable name can be parsed to the marc_each, in which case an automatic marc_copy will be done
+into the variable name. E.g
+
+    do marc_each()
+       marc_copy(***,this)
+       ...
+    end
+
+and
+
+    do marc_each(var:this)
+       ...
+    end
+
+is similar
+
 =head1 SEE ALSO
 
 L<Catmandu::Fix::Bind>
diff --git a/lib/Catmandu/Fix/marc_copy.pm b/lib/Catmandu/Fix/marc_copy.pm
index 7b8103e..b8df8c9 100644
--- a/lib/Catmandu/Fix/marc_copy.pm
+++ b/lib/Catmandu/Fix/marc_copy.pm
@@ -122,31 +122,27 @@ Copy this MARC fields referred by a MARC_PATH to a JSON_PATH.
     These JSON paths can be used like:
 
         # Set the first indicator of all 300 fields
-        do marc_each()
-          if marc_has(300)
-            marc_copy(300,tmp)
+        do marc_each(var:this)
+          if all_match(this.tag,300)
 
             # Set the first indicator to 1
-            # We only check the first item in tmp because the march_each
-            # binder can contain only one MARC field at a time
-            set_field(tmp.0.ind1,1)
+            set_field(this.ind1,1)
 
-            marc_paste(tmp)
+            marc_paste(this)
           end
         end
 
         # Capitalize all the v subfields of 300
-        do marc_each()
-          if marc_has(300)
-             marc_copy(300,tmp)
+        do marc_each(var:this)
+            if all_match(this.tag,300)
 
-             do list(path:tmp.0.subfields, var:loop)
+             do list(path:this.subfields, var:loop)
                 if (exists(loop.v))
                     upcase(loop.v)
                 end
              end
 
-             marc_paste(tmp)
+             marc_paste(this)
           end
         end
 
diff --git a/lib/Catmandu/Fix/marc_cut.pm b/lib/Catmandu/Fix/marc_cut.pm
index e76d6d5..15f8cb1 100644
--- a/lib/Catmandu/Fix/marc_cut.pm
+++ b/lib/Catmandu/Fix/marc_cut.pm
@@ -118,31 +118,27 @@ E.g.
 These JSON paths can be used like:
 
     # Set the first indicator of all 300 fields
-    do marc_each()
-      if marc_has(300)
-        marc_cut(300,tmp)
+    do marc_each(var:this)
+      if all_match(this.tag,300)
 
         # Set the first indicator to 1
-        # We only check the first item in tmp because the march_each
-        # binder can contain only one MARC field at a time
-        set_field(tmp.0.ind1,1)
+        set_field(this.ind1,1)
 
-        marc_paste(tmp)
+        marc_paste(this)
       end
     end
 
     # Capitalize all the v subfields of 300
-    do marc_each()
-      if marc_has(300)
-         marc_cut(300,tmp)
+    do marc_each(var:this)
+      if all_match(this.tag,300)
 
-         do list(path:tmp.0.subfields, var:loop)
+         do list(path:this.subfields, var:loop)
             if (exists(loop.v))
                 upcase(loop.v)
             end
          end
 
-         marc_paste(tmp)
+         marc_paste(this)
       end
     end
 
diff --git a/lib/Catmandu/Fix/marc_paste.pm b/lib/Catmandu/Fix/marc_paste.pm
index df87875..6758b4a 100644
--- a/lib/Catmandu/Fix/marc_paste.pm
+++ b/lib/Catmandu/Fix/marc_paste.pm
@@ -37,16 +37,13 @@ Catmandu::Fix::marc_paste - paste a MARC structured field back into the MARC rec
     marc_paste(fixed001)
 
     # Copy and paste in place (rename a field)
-    do marc_each()
-      if marc_has(001)
-        # Copy a MARC field
-        marc_copy(001, fixed001)
-
+    do marc_each(var:this)
+      if all_match(this.tag,001)
         # Change it
-        set_fieldfixed001.$first.tag,002)
+        set_field(this.tag,002)
 
         # Paste it back into the record
-        marc_paste(fixed001)
+        marc_paste(this)
       end
     end
 
diff --git a/lib/Catmandu/MARC.pm b/lib/Catmandu/MARC.pm
index 667643d..47ce73d 100644
--- a/lib/Catmandu/MARC.pm
+++ b/lib/Catmandu/MARC.pm
@@ -1167,7 +1167,9 @@ sub marc_paste {
 
     my $value = Catmandu::Util::data_at($json_path,$data);
 
-    return $data unless Catmandu::Util::is_array_ref($value);
+    return $data unless Catmandu::Util::is_array_ref($value) || Catmandu::Util::is_hash_ref($value);
+
+    $value = [$value] unless Catmandu::Util::is_array_ref($value);
 
     my @new_parts;
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libcatmandu-marc-perl.git



More information about the Pkg-perl-cvs-commits mailing list