Moose/Record.pm

filter: Statementstmt 86.7% 111 / 128 Branchbran 63.3% 19 / 30 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 84.0% 21 / 25 Podpod 75.0% 6 / 8 totaltotal 82.6% 162 / 196 CC 22 SCAR 14.9
CC
22
Cov
83%
append
8.2
field
8.0
as
6.0
CRAP 4.4
« Moose/Parser/MarcxmlSaxHandler.pm ↑ Summary Moose/Field/Control.pm »
1 package MARC::Moose::Record;
2 # ABSTRACT: MARC::Moose bibliographic record
3 4 $MARC::Moose::Record::VERSION = '1.0.49';
4 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use MARC::Moose::Formater::Iso2709;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use MARC::Moose::Formater::Json;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use MARC::Moose::Formater::Legacy;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use MARC::Moose::Formater::Marcxml;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use MARC::Moose::Formater::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use MARC::Moose::Formater::Yaml;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use MARC::Moose::Formater::UnimarcToMarc21;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use MARC::Moose::Formater::AuthorityUnimarcToMarc21;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use MARC::Moose::Parser::Iso2709;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use MARC::Moose::Parser::MarcxmlSax;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use MARC::Moose::Parser::Legacy;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use MARC::Moose::Parser::Yaml;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use MARC::Moose::Parser::Json;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 4 with 'MARC::Moose::Lint::Checker';
23  
24 4 has lint => (
25 is => 'rw',
26 );
27  
28 4 has leader => (
29 is => 'ro',
30 isa => 'Str',
31 writer => '_leader',
32 default => ' ' x 24,
33 );
34  
35 has fields => (
36 is => 'rw',
37 isa => 'ArrayRef',
38 10 ▶ default => sub { [] }
Subroutine __ANON__: called CC=1 SCAR=0.0
39 4 );
40  
41  
42 # Global
43  
44 # Les formater standards
45 4 my $formater = {
46 iso2709 => MARC::Moose::Formater::Iso2709->new(),
47 json => MARC::Moose::Formater::Json->new(),
48 legacy => MARC::Moose::Formater::Legacy->new(),
49 marcxml => MARC::Moose::Formater::Marcxml->new(),
50 text => MARC::Moose::Formater::Text->new(),
51 yaml => MARC::Moose::Formater::Yaml->new(),
52 unimarctomarc21 => MARC::Moose::Formater::UnimarcToMarc21->new(),
53 authorityunimarctomarc21 => MARC::Moose::Formater::AuthorityUnimarcToMarc21->new(),
54 };
55  
56 # Les parser standards
57 4 my $parser = {
58 iso2709 => MARC::Moose::Parser::Iso2709->new(),
59 marcxml => MARC::Moose::Parser::MarcxmlSax->new(),
60 legacy => MARC::Moose::Parser::Legacy->new(),
61 yaml => MARC::Moose::Parser::Yaml->new(),
62 json => MARC::Moose::Parser::Json->new(),
63 };
64  
65  
66 {
67 4 $MARC::Moose::Record::formater = $formater;
68 4 $MARC::Moose::Record::parser = $parser;
69 }
70  
71  
72  
73 0 ▶sub clone {
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
74 0 my $self = shift;
75  
76 0 my $record = MARC::Moose::Record->new();
77 0 $record->_leader( $self->leader );
78 0 $record->fields( [ map { $_->clone(); } @{$self->fields} ] );
79 0 return $record;
80 }
81  
82  
83 1 ▶sub set_leader_length {
Subroutine set_leader_length: called CC=2 SCAR=7.0
Pod documented
84 1 my ($self, $length, $offset) = @_;
85  
86 1 ▶ carp "Record length of $length is larger than the MARC spec allows 99999"
Branch: 1 branch
BranchTrueFalseTotal
if $length > 99999 0 1 1
87 if $length > 99999;
88  
89 1 my $leader = $self->leader;
90 1 substr($leader, 0, 5) = sprintf("%05d", $length);
91 1 substr($leader, 12, 5) = sprintf("%05d", $offset);
92  
93 # Default leader various pseudo variable fields
94 # Force UNICODE MARC21: substr($leader, 9, 1) = 'a';
95 1 substr($leader, 10, 2) = '22';
96 1 substr($leader, 20, 4) = '4500';
97  
98 1 $self->_leader( $leader );
99 }
100  
101  
102 4 ▶sub append {
Subroutine append: called CC=8 SCAR=21.0
Pod documented
103 4 my $self = shift;
104  
105 4 my @fields = @_;
106 4 ▶ return unless @fields;
Branch: 1 branch
BranchTrueFalseTotal
unless @fields 0 4 4
107
108 # Control field correctness
109 4 for my $field (@fields) {
110 4 ▶ unless ( ref($field) =~ /^MARC::Moose::Field/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($field) =~ /^MARC::Moose::Field/u) 0 4 4
111 0 carp "Append a non MARC::Moose::Field";
112 0 return;
113 }
114 }
115  
116 4 my $tag = $fields[0]->tag;
117 4 my @sf;
118 4 my $notdone = 1;
119 4 for my $field ( @{$self->fields} ) {
120 6 ▶ if ( $notdone && $field->tag gt $tag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($notdone and $field->tag gt $tag) 2 4 6
Condition100%3/3
$notdone and $field->tag gt $tag
!ll&&!rl&&r
132
MC/DC100%2/2
$notdone and $field->tag gt $tag
$notdone $field->tag gt $tag
Truth table100%3/3
A and B
A= $notdoneB= $field->tag gt $tag
ABresult
0X0
100
111
121 2 push @sf, @fields;
122 2 $notdone = 0;
123 }
124 6 push @sf, $field;
125 }
126 4 ▶ push @sf, @fields if $notdone;
Branch: 1 branch
BranchTrueFalseTotal
if $notdone 2 2 4
127 4 $self->fields( \@sf );
128 }
129  
130  
131 4 my %_field_regex;
132  
133  
134 13 ▶sub field {
Subroutine field: called CC=8 SCAR=20.8
Pod documented
135 13 my $self = shift;
136 13 my @specs = @_;
137  
138 13 my @list;
139 13 for my $tag ( @specs ) {
140 13 my $regex = $_field_regex{ $tag };
141 # Compile & stash it if necessary
142 13 ▶ unless ( $regex ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($regex) 10 3 13
143 10 $regex = qr/^$tag$/;
144 10 $_field_regex{ $tag } = $regex;
145 }
146 13 for my $field ( @{$self->fields} ) {
147 65 ▶ if ( $field->tag =~ $regex ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->tag =~ /$regex/u) 15 50 65
148 15 ▶ return $field unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 8 7 15
149 7 push @list, $field;
150 }
151 }
152 }
153 wantarray
154 ? @list
155 5 ▶ : @list ? $list[0] : undef;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 3 2 5
@list ? : 0 2 2
156 }
157  
158  
159 1 ▶sub delete {
Subroutine delete: called CC=4 SCAR=13.9
Pod documented
160 1 my $self = shift;
161  
162 1 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 1 1
163  
164 $self->fields( [ grep {
165 4 my $tag = $_->tag;
166 4 my $keep = 1;
167 4 for my $tag_spec ( @_ ) {
168 4 ▶ if ( $tag =~ $tag_spec ) { $keep = 0; last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /$tag_spec/u) 3 1 4
169 }
170 4 $keep;
171 1 } @{$self->fields} ] );
172 }
173  
174  
175 0 ▶sub as {
Subroutine as: not called CC=2 SCAR=17.9
Pod documented
176 0 my ($self, $format) = @_;
177 0 my $f = $formater->{ lc($format) };
178 0 ▶ return $f ? $f->format($self) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$f ? : 0 0 0
179 }
180  
181  
182 0 ▶sub new_from {
Subroutine new_from: not called CC=2 SCAR=17.9
Pod undocumented
183 0 my ($record, $type) = @_;
184 0 my $p = $parser->{ lc($type) };
185 0 ▶ $p ? $p->parse($record) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$p ? : 0 0 0
186 }
187  
188  
189 0 ▶sub check {
Subroutine check: not called CC=2 SCAR=17.9
Pod undocumented
190 0 my $self = shift;
191 0 ▶ $self->lint ? $self->lint->check($self) : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->lint ? : 0 0 0
192 }
193  
194  
195 4 __PACKAGE__->meta->make_immutable;
196  
197 4 1;
198  
199 __END__
« Moose/Parser/MarcxmlSaxHandler.pm ↑ Summary Moose/Field/Control.pm »