Reference.pm

filter: Statementstmt 98.2% 110 / 112 Branchbran 77.0% 37 / 48 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 20 / 20 Podpod 100% 7 / 7 totaltotal 91.6% 176 / 192 CC 32 SCAR 14.2
CC
32
Cov
91%
canonicalize_book
8.4
_parse_ranges
6.0
next
5.0
CRAP 4.1
↑ Summary Reference/Standard.pm »
1 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Religion::Bible::Reference;
4 # ABSTRACT: canonicalize shorthand bible references
5 2 $Religion::Bible::Reference::VERSION = '0.016';
6 2 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
7 exports => [ qw(bibref) ],
8 groups => { default => [ qw(bibref) ] },
9 2 };
10  
11 2 my %book_chapters;
12 my %book_abbrev;
13 2 my %book_short;
14  
15 2 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.1
16 2 for my $attr (qw(book chapter ranges)) {
17 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶ *$attr = sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
19 9 ▶ return $_[0]->{$attr} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 9 0 9
20 0 return $_[0]->{$attr} = $_[1];
21 6 };
22 }
23 }
24  
25 2 ▶use Religion::Bible::Reference::Standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 #pod =head1 SYNOPSIS
28 #pod
29 #pod use Religion::Bible::Reference;
30 #pod
31 #pod my $quote = bibref("jn8:32");
32 #pod
33 #pod print "($quote)"; # (John 8:32)
34 #pod print $quote->book; # John
35 #pod
36 #pod =head1 DESCRIPTION
37 #pod
38 #pod This module converts simple text descriptions of bible references and ranges
39 #pod into objects that stringify into a canonical form.
40 #pod
41 #pod B<WARNING!> This module is mostly an idea and not so much a guaranteed
42 #pod interface or well-tested implementation. If you're interested in either of
43 #pod those existing, you should let me know.
44 #pod
45 #pod =func bibref
46 #pod
47 #pod my $ref = bibref($ref_string)
48 #pod
49 #pod This function is exported by default, and constructs a new
50 #pod Religion::Bible::Reference
51 #pod
52 #pod Reference strings must be a book followed by a list of chapters, verses, or
53 #pod ranges. The following are all valid ranges:
54 #pod
55 #pod Pro 23:12, 23:15-17
56 #pod st.jn8:32
57 #pod Song of Solomon 8:7-8
58 #pod 2 John 1
59 #pod
60 #pod =cut
61  
62 9 ▶sub bibref { __PACKAGE__->new(@_); }
Subroutine bibref: called CC=1 SCAR=0.0
Pod documented
63  
64 #pod =method new
65 #pod
66 #pod my $ref = Religion::Bible::Reference->new($ref_string)
67 #pod
68 #pod This method acts just like the exported C<bibref> function.
69 #pod
70 #pod =cut
71  
72 # ok:
73 # jn8
74 # jn8:32
75 # jn8:30-32
76 # jn8:25-28,30-32
77 # jn8:1,3-4,6
78  
79 9 ▶sub _parse_ref {
Subroutine _parse_ref: called CC=1 SCAR=0.0
80 9 my ($class, $ref_string) = @_;
81 9 my $range_regex = qr/\d+(?::(?:\d[-,]?)+)?/;
82  
83 9 (my $book = $ref_string) =~ s/\s*($range_regex)\z//;
84 9 my $ranges = $1;
85  
86 9 return (book => $book, ranges => $ranges);
87 }
88  
89 9 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
90 9 my ($class, $ref_string) = @_;
91  
92 9 my %bibref = $class->_parse_ref($ref_string);
93  
94 9 my $self;
95  
96 9 ▶ return unless $self->{book} = $class->canonicalize_book($bibref{book});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"book"} = $class->canonicalize_book($bibref{"book"}) 0 9 9
97  
98 9 bless $self => $class;
99  
100 9 ▶ return unless my $range = $self->_parse_ranges($bibref{ranges});
Branch: 1 branch
BranchTrueFalseTotal
unless my $range = $self->_parse_ranges($bibref{"ranges"}) 0 9 9
101  
102 9 $self->{chapter} = $range->{chapter};
103 9 $self->{ranges} = $range->{ranges};
104  
105 return unless $class->_validate_ranges(
106 $self->{book},
107 $self->{chapter},
108 $self->{ranges},
109 9 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_validate_ranges($self->{"book"}, $self->{"chapter"}, $self->{"ranges"}) 2 7 9
110  
111 7 return $self;
112 }
113  
114 9 ▶sub _validate_ranges {
Subroutine _validate_ranges: called CC=4 SCAR=13.9
115 9 my ($class, $book, $chapter, $ranges) = @_;
116  
117 9 foreach my $range (@$ranges) {
118 14 ▶ return unless $class->validate_verse($book, $chapter, $range->[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->validate_verse($book, $chapter, $range->[0]) 1 13 14
119 13 ▶ return unless $class->validate_verse($book, $chapter, $range->[1]);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->validate_verse($book, $chapter, $range->[1]) 1 12 13
120 }
121 7 return 1;
122 }
123  
124 9 ▶sub _parse_ranges {
Subroutine _parse_ranges: called CC=6 SCAR=18.0
125 9 my ($self, $string) = @_;
126  
127 9 my ($chapter, $rest) = $string =~ /\A(\d+)(?::(.+))?\z/;
128  
129 9 ▶ return unless $chapter;
Branch: 1 branch
BranchTrueFalseTotal
unless $chapter 0 9 9
130 return { chapter => $string,
131 9 ▶ ranges => [[ 1, $book_chapters{$self->{book}}[$chapter - 1] ]] }
Branch: 1 branch
BranchTrueFalseTotal
unless $rest 1 8 9
132 unless $rest;
133  
134 8 my @range_strings = split /,\s?/, $rest;
135  
136 8 my @range;
137  
138 8 for my $rs (@range_strings) {
139 13 my ($start, $end) = $rs =~ /\A(\d+)(?:-(\d+))?\z/;
140 13 ▶ return unless $start;
Branch: 1 branch
BranchTrueFalseTotal
unless $start 0 13 13
141 13 ▶ push @range, [ $start, (defined $end ? $end : $start) ];
Branch: 1 branch
BranchTrueFalseTotal
defined $end ? : 8 5 13
142 }
143  
144 8 return { chapter => $chapter, ranges => \@range };
145 }
146  
147 #pod =method stringify
148 #pod
149 #pod $self->stringify
150 #pod
151 #pod This method returns a string representing the reference, using the canonical
152 #pod book name.
153 #pod
154 #pod =cut
155  
156 7 ▶sub stringify {
Subroutine stringify: called CC=2 SCAR=7.0
Pod documented
157 7 my ($self) = @_;
158 my $string = $self->{book}
159 . q{ }
160 7 . $self->{chapter};
161  
162 7 ▶ return unless @{ $self->{ranges} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"ranges"};} 0 7 7
163  
164 $string .=
165 7 ':' . join(', ', map { $self->_stringify_range($_) } @{ $self->{ranges} })
166 ;
167 }
168  
169 15 ▶sub _stringify_range {
Subroutine _stringify_range: called CC=2 SCAR=6.9
170 15 my ($self, $range) = @_;
171  
172 15 ▶ map { $_->[0] == $_->[1] ? $_->[0] : "$_->[0]-$_->[1]" } $range
Branch: 1 branch
BranchTrueFalseTotal
$_->[0] == $_->[1] ? : 6 9 15
173 }
174  
175 2 ▶sub _register_book_set {
Subroutine _register_book_set: called CC=2 SCAR=6.9
176 2 my ($class, $package) = @_;
177  
178 2 my @books = $package->_books;
179 2 for my $book (@books) {
180 132 my $full = $book->{full};
181 132 $book_chapters{ $full } = $book->{verses};
182 132 $book_abbrev { $full } = $book->{abbreviations};
183 132 $book_short { $full } = $book->{short};
184 }
185 }
186  
187 #pod =method stringify_short
188 #pod
189 #pod my $str = $self->stringify_short
190 #pod
191 #pod This method returns a string representing the reference, using the short book
192 #pod name.
193 #pod
194 #pod In other words, John 8:32 would be Jn 8:32. All short forms should safely
195 #pod round-trip back via parsing.
196 #pod
197 #pod =cut
198  
199 1 ▶sub stringify_short {
Subroutine stringify_short: called CC=2 SCAR=7.0
Pod documented
200 1 my ($self) = @_;
201  
202 my $string = $book_short{ $self->{book} }
203 . q{ }
204 1 . $self->{chapter};
205  
206 1 ▶ return unless @{ $self->{ranges} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"ranges"};} 0 1 1
207  
208 $string .=
209 1 ':' . join(', ', map { $self->_stringify_range($_) } @{ $self->{ranges} })
210 ;
211 }
212  
213 2 __PACKAGE__->_register_book_set("Religion::Bible::Reference::Standard");
214  
215 #pod =method canonicalize_book
216 #pod
217 #pod my $book = $class->canonicalize_book($book_abbrev)
218 #pod
219 #pod If possible, this method returns the canonical name of the book whose
220 #pod abbreviation was passed.
221 #pod
222 #pod =cut
223  
224 # mdxi suggests that I could have a list of pre-limiting regex, something like
225 # this:
226 # [ qr/\A(?:1|First)/, [ '1 Kings', '1 Samuel' ...
227 # so that if a passed string matches the regex, it's only checked against those
228 # entries in the associated list; good idea, for future revision
229  
230 9 ▶sub canonicalize_book {
Subroutine canonicalize_book: called CC=8 SCAR=21.3
Pod documented
231 9 my ($class, $book_abbrev) = @_;
232 9 ▶ return $book_abbrev if $book_abbrev{$book_abbrev};
Branch: 1 branch
BranchTrueFalseTotal
if $book_abbrev{$book_abbrev} 0 9 9
233 9 my $lc_abbrev = lc($book_abbrev);
234 9 for my $book (keys %book_abbrev) {
235 334 ▶ return $book if lc($book) eq $lc_abbrev;
Branch: 1 branch
BranchTrueFalseTotal
if lc $book eq $lc_abbrev 0 334 334
236 334 for (@{$book_abbrev{$book}}) {
237 496 ▶ if (ref $_) { return $book if $book_abbrev =~ m/$_/; }
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_) { } 110 386 496
if $book_abbrev =~ /$_/i 9 101 110
238 386 ▶ else { return $book if $lc_abbrev eq lc($_); }
Branch: 1 branch
BranchTrueFalseTotal
if $lc_abbrev eq lc $_ 0 386 386
239 }
240 }
241 0 return;
242 }
243  
244 #pod =method validate_verse
245 #pod
246 #pod $class->validate_verse($book, $chapter, $verse)
247 #pod
248 #pod This method returns true if the given book, chapter, and verse exists;
249 #pod otherwise it returns false.
250 #pod
251 #pod =cut
252  
253 27 ▶sub validate_verse {
Subroutine validate_verse: called CC=4 SCAR=13.9
Pod documented
254 27 my ($self, $book, $chapter, $verse) = @_;
255 27 ▶ return unless exists $book_chapters{$book};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $book_chapters{$book} 0 27 27
256 27 ▶ return unless defined $book_chapters{$book}[$chapter - 1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $book_chapters{$book}[$chapter - 1] 1 26 27
257 26 ▶ return unless $book_chapters{$book}[$chapter - 1] >= $verse;
Branch: 1 branch
BranchTrueFalseTotal
unless $book_chapters{$book}[$chapter - 1] >= $verse 1 25 26
258 25 return 1
259 }
260  
261 #pod =method iterator
262 #pod
263 #pod my $iterator = $bibref->iterator;
264 #pod
265 #pod while (my $verse = $iterator->next) {
266 #pod my $text = retrieve($verse);
267 #pod print "$text\n";
268 #pod }
269 #pod
270 #pod =cut
271  
272 3 ▶sub iterator {
Subroutine iterator: called CC=1 SCAR=0.0
Pod documented
273 3 my ($self) = @_;
274  
275 my $iterator = {
276 book => $self->book,
277 chapter => $self->chapter,
278 3 ranges => [ @{ $self->ranges } ],
279 };
280  
281 3 bless $iterator => 'Religion::Bible::Reference::Iterator';
282 }
283  
284 package Religion::Bible::Reference::Iterator;
285 2 $Religion::Bible::Reference::Iterator::VERSION = '0.016';
286 64 ▶sub next { ## no critic # honestly, next is a great method for an iterator
Subroutine next: called CC=5 SCAR=16.1
287 64 my ($self) = @_;
288 64 ▶ return unless @{ $self->{ranges} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"ranges"};} 3 61 64
289  
290 61 ▶ $self->{position} ||= $self->{ranges}[0][0];
Condition66%2/3
$self->{"position"} ||= $self->{"ranges"}[0][0]
l!l&&r!l&&!r
5470
MC/DC0%0/2
$self->{"position"} ||= $self->{"ranges"}[0][0]
$self->{"position"} $self->{"ranges"}[0][0]
Truth table66%2/3
A ||= B
A= $self->{"position"}B= $self->{"ranges"}[0][0]
ABresult
1X1
011
000
291 61 my $position = $self->{position};
292  
293 61 ▶ if ($position == $self->{ranges}[0][1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($position == $self->{'ranges'}[0][1]) { } 7 54 61
294 7 shift @{ $self->{ranges} };
295 7 undef $self->{position};
296 } else {
297 54 $self->{position}++;
298 }
299 61 ▶ return wantarray ? (@$self{qw(book chapter)}, $position) : $position;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 56 61
300 }
301  
302 #pod =head1 TODO
303 #pod
304 #pod =for :list
305 #pod * allow L<Text::Abbrev> instead of registered abbrevs
306 #pod * clean up regex/lists
307 #pod * make public the interface to load modules of books and abbreviations
308 #pod * make an interface to unload modules
309 #pod
310 #pod =cut
311  
312 2 1;
313  
314 __END__
↑ Summary Reference/Standard.pm »