Fix/Condition/pica_match.pm

filter: Statementstmt 100% 40 / 40 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 1 totaltotal 98.0% 49 / 50 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Exporter/PICA.pm ↑ Summary PICA.pm »
1 package Catmandu::Fix::Condition::pica_match;
2  
3 1 our $VERSION = '1.18';
4  
5 1 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Catmandu::Fix::pica_map;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Catmandu::Fix::Condition::all_match;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Catmandu::Fix::set_field;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Catmandu::Fix::remove_field;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Catmandu::Fix::Has;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 with 'Catmandu::Fix::Condition';
14  
15 1 has pica_path => ( fix_arg => 1 );
16 3 ▶has value => ( fix_arg => 1, default => sub { '.*' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
17  
18 4 ▶sub emit {
Subroutine emit: called CC=1 SCAR=0.0
Pod undocumented
19 4 my ( $self, $fixer, $label ) = @_;
20  
21 4 my $perl;
22  
23 4 my $tmp_var = '_tmp_' . int( rand(9999) );
24 4 my $pica_map =
25 Catmandu::Fix::pica_map->new( $self->pica_path, "$tmp_var.\$append" );
26 4 $perl .= $pica_map->emit( $fixer, $label );
27  
28 4 my $all_match =
29 Catmandu::Fix::Condition::all_match->new( "$tmp_var.*", $self->value );
30 4 my $remove_field = Catmandu::Fix::remove_field->new($tmp_var);
31  
32 4 my $pass_fixes = $self->pass_fixes;
33 4 my $fail_fixes = $self->fail_fixes;
34  
35 4 $all_match->pass_fixes( [ $remove_field, @$pass_fixes ] );
36 4 $all_match->fail_fixes( [ $remove_field, @$fail_fixes ] );
37  
38 4 $perl .= $all_match->emit( $fixer, $label );
39  
40 4 $perl;
41 }
42  
43 =head1 NAME
44  
45 Catmandu::Fix::Condition::pica_match - Conditionals on PICA fields
46  
47 =head1 SYNOPSIS
48
49 # pica_match(PICA_PATH,REGEX)
50
51 if pica_match('021A$a','^My funny title$')
52 add_field('my.funny.title','true')
53 end
54  
55 # pica_match(PICA_PATH) # checks whether a field exists
56
57 # pica_match($9) # checks whether a subfield exists
58  
59 if pica_match('001$U0')
60 add_field('my.encode_info','true')
61 end
62  
63 =head1 DESCRIPTION
64  
65 Check whether at least one PICA field or subfield exists or its value matches a
66 regular expression.
67  
68 =head1 SEE ALSO
69  
70 L<Catmandu::Fix::pica_map>
71  
72 =cut
73  
74 1 1;
« Exporter/PICA.pm ↑ Summary PICA.pm »