| 1 | package Text::SuDocs; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | $Text::SuDocs::VERSION = '0.014'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | # ABSTRACT: parse and normalize SuDocs numbers | ||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use 5.10.0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Any::Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 5 | our @subfields = qw{agency subagency committee series relatedseries document}; | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 5 | has [qw(original), @subfields] => ( | |||||||||||||||||||||||||||||||||||||||||
| 17 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 18 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 1112 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 1112 | my $orig = shift; | |||||||||||||||||||||||||||||||||||||||||
| 23 | 1112 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 1112 | ▶ | if (@_ == 1 && !ref $_[0]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ == 1 and not ref $_[0]
MC/DC100%2/2
@_ == 1 and not ref $_[0]
@_ == 1
not ref $_[0]
Truth table100%3/3
A and not(B)
A= @_ == 1B= not ref $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 1105 | return $class->$orig(original => $_[0]); | |||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||
| 28 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 7 | return $class->$orig(@_); | |||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 5 | }; | |||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 1112 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 1112 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 1112 | ▶ | if($self->original) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 1105 | $self->parse; | |||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 2219 | ▶ | around 'original' => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 2219 | my $orig = shift; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 2219 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 2219 | ▶ | if (scalar @_) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | $self->$orig(@_); | |||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | $self->parse(); | |||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 2219 | return $self->$orig(); | |||||||||||||||||||||||||||||||||||||||||
| 50 | 5 | }; | |||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 1106 | ▶ | sub parse { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=10 SCAR=23.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 1106 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 1106 | ▶ | my $original = shift // $self->original; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
shift() // $self->original
MC/DC0%0/2
shift() // $self->original
shift()
$self->original
Truth table33%1/3
A // B
A= shift()B= $self->original
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 1106 | ▶ | return if ! defined $original; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 1106 | chomp($original); | |||||||||||||||||||||||||||||||||||||||||
| 58 | 1106 | ▶ | croak 'Invalid characters' if $original =~ qr{[^\p{IsAlnum}\s:/\-.<>()]}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 1104 | $original = uc $original; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 1104 | $original =~ s{^\s+|\s+$}{}g; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 1104 | $original =~ s{\s+}{ }g; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 1104 | $original =~ s{:$}{}; | |||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 1104 | ▶ | if ($original =~ /^(XJH|XJS)$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 5 | $self->agency($1); | |||||||||||||||||||||||||||||||||||||||||
| 66 | 5 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 1099 | $original =~ qr{ | |||||||||||||||||||||||||||||||||||||||||
| 70 | ^(\p{IsAlpha}+)\s* #Agency | ||||||||||||||||||||||||||||||||||||||||||
| 71 | (\p{IsDigit}+)\s*\.\s* #Subagency | ||||||||||||||||||||||||||||||||||||||||||
| 72 | (?:(\p{IsAlpha}+)\s+)? #Committee | ||||||||||||||||||||||||||||||||||||||||||
| 73 | (\p{IsDigit}+) #Series | ||||||||||||||||||||||||||||||||||||||||||
| 74 | (?:/(\p{IsAlnum}+)(-\p{IsAlnum}+)?)?\s* #RelSeries | ||||||||||||||||||||||||||||||||||||||||||
| 75 | (?::\s*(.*))?$ #Document | ||||||||||||||||||||||||||||||||||||||||||
| 76 | }x; | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 1099 | ▶ | croak 'Unable to determine stem' if (!($1 && $2 && $4)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$1 and $2 and $4
$1 and $2
MC/DC33%1/3
$1 and $2 and $4
$1
$2
$4
Truth table50%2/4
A and B and C
A= $1B= $2C= $4
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 1089 | $self->agency($1); | |||||||||||||||||||||||||||||||||||||||||
| 80 | 1089 | $self->subagency($2); | |||||||||||||||||||||||||||||||||||||||||
| 81 | 1089 | $self->committee($3); | |||||||||||||||||||||||||||||||||||||||||
| 82 | 1089 | $self->series($4); | |||||||||||||||||||||||||||||||||||||||||
| 83 | 1089 | ▶ | my $relseries = | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | (!$5) ? undef : | ||||||||||||||||||||||||||||||||||||||||||
| 85 | ($6) ? $5.$6 : $5; | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 1089 | $self->relatedseries($relseries); | |||||||||||||||||||||||||||||||||||||||||
| 87 | 1089 | $self->document($7); | |||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 1089 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 1137 | ▶ | sub normal_string { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
normal_string: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 1137 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | my %args = (ref $_[0]) ? %{$_[0]} : @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 1137 | ▶ | return $self->agency if ($self->agency =~ /^(?:XJH|XJS)$/); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 1122 | ▶ | my $sudocs = sprintf( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | '%s %d.%s%s%s', | ||||||||||||||||||||||||||||||||||||||||||
| 100 | $self->agency, | ||||||||||||||||||||||||||||||||||||||||||
| 101 | $self->subagency, | ||||||||||||||||||||||||||||||||||||||||||
| 102 | ($self->committee) ? $self->committee . q{ } : '', | ||||||||||||||||||||||||||||||||||||||||||
| 103 | $self->series, | ||||||||||||||||||||||||||||||||||||||||||
| 104 | ($self->relatedseries) ? '/'.$self->relatedseries : '', | ||||||||||||||||||||||||||||||||||||||||||
| 105 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 1122 | ▶ | unless ($args{class_stem} || !$self->document) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args{"class_stem"} or not $self->document
MC/DC100%2/2
$args{"class_stem"} or not $self->document
$args{"class_stem"}
not $self->document
Truth table100%3/3
A or not(B)
A= $args{"class_stem"}B= not $self->document
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 1100 | $sudocs .= ':'.$self->document; | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 1122 | return $sudocs; | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 22 | ▶ | sub sortable_string { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sortable_string: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 22 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 22 | ▶ | my $pad = shift // 8; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() // 8
MC/DC0%0/1
shift() // 8
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 22 | my $s = $self->normal_string; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 22 | my $format = sprintf '%%0%dd', $pad; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 22 | $s =~ s/\b(\d+)\b/sprintf $format, $1/xge; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 22 | $s =~ s/\s/_/g; | |||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 22 | return $s; | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 5 | __PACKAGE__->meta()->make_immutable(); | |||||||||||||||||||||||||||||||||||||||||
| 126 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | __END__ | ||||||||||||||||||||||||||||||||||||||||||