bible_passage 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/bible_passage.gemspec +1 -1
- data/lib/bible_passage/book_data_store.rb +2 -2
- data/lib/bible_passage/reference.rb +108 -11
- data/spec/bible_passage/book_data_store_spec.rb +8 -0
- data/spec/bible_passage/reference_spec.rb +118 -10
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b4b4b875333c8c0752f13e6a2de2638e41d4cfb0
|
4
|
+
data.tar.gz: 66f0275e662976d67e728451dd88f050428c8078
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32f934c2accc408fdb71db8b41a184ffeb2b8c3142e5d9d8800c861e01f4f51794daf40bf3ba7c09ce5344009b207418a73ce7ade77d70093a29e535f22b2f33
|
7
|
+
data.tar.gz: 424679412f391d8c395234d82bbe15d7cd5de7cb4ce0628ce4bc737dca45d6b28dbb30692f5597e852584134e83d53a496bec92eb61d49830d90934490467992
|
data/Gemfile.lock
CHANGED
data/bible_passage.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'bible_passage'
|
3
3
|
s.summary = 'A simple library for parsing and rendering bible passages'
|
4
|
-
s.version = '0.0
|
4
|
+
s.version = '0.1.0'
|
5
5
|
s.authors = ["Si Wilkins"]
|
6
6
|
s.email = 'si.wilkins@gmail.com'
|
7
7
|
s.homepage = 'https://github.com/siwilkins/bible_passage'
|
@@ -65,7 +65,7 @@ module BiblePassage
|
|
65
65
|
},
|
66
66
|
neh: {
|
67
67
|
name: 'Nehemiah',
|
68
|
-
chapters: [11, 20,
|
68
|
+
chapters: [11, 20, 32, 23, 19, 19, 73, 18, 38, 39, 36, 47, 31]
|
69
69
|
},
|
70
70
|
esth: {
|
71
71
|
name: 'Esther',
|
@@ -85,7 +85,7 @@ module BiblePassage
|
|
85
85
|
},
|
86
86
|
eccl: {
|
87
87
|
name: 'Ecclesiastes',
|
88
|
-
chapters: [18, 26, 22,
|
88
|
+
chapters: [18, 26, 22, 16, 20, 12, 29, 17, 18, 20, 10, 14]
|
89
89
|
},
|
90
90
|
song: {
|
91
91
|
name: 'Song of Songs',
|
@@ -4,20 +4,73 @@ module BiblePassage
|
|
4
4
|
|
5
5
|
class << self
|
6
6
|
|
7
|
+
# Parsing methods are currently very complex and long.
|
8
|
+
# Could need some work
|
9
|
+
|
10
|
+
##
|
11
|
+
# The main method used for parsing passage strings
|
7
12
|
def parse(passage, options = {})
|
8
13
|
translator = options.delete(:translator) || BookKeyTranslator.new
|
9
14
|
data_store = options[:data_store] ||= BookDataStore.new
|
10
|
-
match = passage
|
15
|
+
match = match_passage_format(passage)
|
16
|
+
raise InvalidReferenceError.new("#{passage} is not a valid reference") if !match
|
11
17
|
book_key = translator.keyify(match[1])
|
12
18
|
if data_store.number_of_chapters(book_key) == 1
|
13
|
-
process_single_chapter_match(book_key, match, options)
|
19
|
+
ref = process_single_chapter_match(book_key, match, options)
|
14
20
|
else
|
15
|
-
process_multi_chapter_match(book_key, match, options)
|
21
|
+
ref = process_multi_chapter_match(book_key, match, options)
|
16
22
|
end
|
23
|
+
ref.parse_child(match[6].gsub(/^,\s*/, '')) if match[6]
|
24
|
+
ref
|
17
25
|
end
|
18
26
|
|
27
|
+
##
|
28
|
+
# Parses a child reference in a compound passage string
|
29
|
+
def parse_child(passage, parent, options = {})
|
30
|
+
if match_passage_format(passage)
|
31
|
+
ref = parse(passage, options)
|
32
|
+
else
|
33
|
+
match = passage.match(/\s*(\d+)?:?(\d+)?\s*(-?)\s*(\d+)?:?(\d+)?\s*(,.+)?$/)
|
34
|
+
book_key = parent.book_key
|
35
|
+
attrs = parent.inheritable_attributes
|
36
|
+
if attrs[:from_chapter]
|
37
|
+
if match[2]
|
38
|
+
attrs[:from_chapter] = match[1].to_i
|
39
|
+
attrs[:from_verse] = match[2].to_i
|
40
|
+
else
|
41
|
+
attrs[:from_verse] = match[1].to_i
|
42
|
+
end
|
43
|
+
else
|
44
|
+
attrs[:from_chapter] = match[1].to_i
|
45
|
+
if match[2]
|
46
|
+
attrs[:from_verse] = match[2].to_i
|
47
|
+
else
|
48
|
+
end
|
49
|
+
end
|
50
|
+
if match[5]
|
51
|
+
attrs[:to_chapter] = int_param(match[4])
|
52
|
+
attrs[:to_verse] = int_param(match[5])
|
53
|
+
elsif attrs[:from_verse]
|
54
|
+
attrs[:to_verse] = int_param(match[4])
|
55
|
+
else
|
56
|
+
attrs[:to_chapter] = int_param(match[4])
|
57
|
+
end
|
58
|
+
ref = new(book_key, attrs[:from_chapter], attrs[:from_verse],
|
59
|
+
attrs[:to_chapter], attrs[:to_verse])
|
60
|
+
end
|
61
|
+
ref.parent = parent
|
62
|
+
ref
|
63
|
+
end
|
19
64
|
|
20
65
|
private
|
66
|
+
def match_passage_format(passage)
|
67
|
+
passage.match(/^\s*(\d?\s*[A-Za-z\s]+)\s*(\d+)?:?(\d+)?\s*-?\s*(\d+)?:?(\d+)?\s*(,.+)?/)
|
68
|
+
end
|
69
|
+
|
70
|
+
def int_param(param)
|
71
|
+
param ? param.to_i : nil
|
72
|
+
end
|
73
|
+
|
21
74
|
def process_multi_chapter_match(book_key, match, options)
|
22
75
|
if match[2]
|
23
76
|
from_chapter = match[2].to_i
|
@@ -28,11 +81,11 @@ module BiblePassage
|
|
28
81
|
to_chapter = match[4].to_i
|
29
82
|
to_verse = match[5].to_i
|
30
83
|
else
|
31
|
-
to_verse = match[4]
|
84
|
+
to_verse = int_param(match[4])
|
32
85
|
end
|
33
86
|
else
|
34
|
-
from_verse = match[3]
|
35
|
-
to_chapter = match[4]
|
87
|
+
from_verse = int_param(match[3])
|
88
|
+
to_chapter = int_param(match[4])
|
36
89
|
end
|
37
90
|
end
|
38
91
|
new(book_key, from_chapter, from_verse, to_chapter, to_verse, options)
|
@@ -53,7 +106,9 @@ module BiblePassage
|
|
53
106
|
|
54
107
|
end
|
55
108
|
|
56
|
-
attr_reader :book_key, :book
|
109
|
+
attr_reader :book_key, :book, :child
|
110
|
+
|
111
|
+
attr_writer :parent
|
57
112
|
|
58
113
|
def initialize(book_key, from_chapter = nil, from_verse = nil,
|
59
114
|
to_chapter = nil, to_verse = nil, options = {})
|
@@ -65,6 +120,10 @@ module BiblePassage
|
|
65
120
|
self.to_verse = calculate_to_verse(to_verse)
|
66
121
|
end
|
67
122
|
|
123
|
+
def parse_child(child_passage)
|
124
|
+
@child = self.class::parse_child(child_passage, self)
|
125
|
+
end
|
126
|
+
|
68
127
|
def book_key=(key)
|
69
128
|
@book = @data_store.book_name(key)
|
70
129
|
@book_key = key
|
@@ -79,6 +138,7 @@ module BiblePassage
|
|
79
138
|
raise InvalidReferenceError.new(
|
80
139
|
"#{book} doesn't have a chapter #{val}") if val < 1 ||
|
81
140
|
val > @data_store.number_of_chapters(book_key)
|
141
|
+
@inherit_book_key = true
|
82
142
|
@from_chapter = val
|
83
143
|
end
|
84
144
|
end
|
@@ -92,6 +152,7 @@ module BiblePassage
|
|
92
152
|
raise InvalidReferenceError.new(
|
93
153
|
"#{book} #{from_chapter} doesn't have a verse #{val}") if val < 1 ||
|
94
154
|
val > @data_store.number_of_verses(book_key, from_chapter)
|
155
|
+
@inherit_chapter = true
|
95
156
|
@from_verse = val
|
96
157
|
end
|
97
158
|
end
|
@@ -128,11 +189,13 @@ module BiblePassage
|
|
128
189
|
end
|
129
190
|
|
130
191
|
def to_s
|
131
|
-
if
|
132
|
-
|
192
|
+
if @parent
|
193
|
+
out = to_s_child
|
133
194
|
else
|
134
|
-
|
195
|
+
out = to_s_root
|
135
196
|
end
|
197
|
+
out << child.to_s if child
|
198
|
+
out
|
136
199
|
end
|
137
200
|
|
138
201
|
def whole_chapters?
|
@@ -149,6 +212,24 @@ module BiblePassage
|
|
149
212
|
whole_chapters? && from_chapter == to_chapter
|
150
213
|
end
|
151
214
|
|
215
|
+
def attributes
|
216
|
+
%w{book_key from_chapter from_verse to_chapter to_verse}.
|
217
|
+
inject({}) do |memo, attr_key|
|
218
|
+
memo.merge(attr_key.to_sym => send(attr_key))
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
def ==(other)
|
223
|
+
attributes == other.attributes
|
224
|
+
end
|
225
|
+
|
226
|
+
def inheritable_attributes
|
227
|
+
out = {}
|
228
|
+
out[:book_key] = book_key if @inherit_book_key
|
229
|
+
out[:from_chapter] = to_chapter if @inherit_chapter
|
230
|
+
out
|
231
|
+
end
|
232
|
+
|
152
233
|
private
|
153
234
|
def from_part
|
154
235
|
out = ''
|
@@ -201,7 +282,6 @@ module BiblePassage
|
|
201
282
|
end
|
202
283
|
end
|
203
284
|
|
204
|
-
|
205
285
|
def int_param(param)
|
206
286
|
param ? param.to_i : nil
|
207
287
|
end
|
@@ -238,6 +318,23 @@ module BiblePassage
|
|
238
318
|
@data_store.number_of_verses(book_key, self.to_chapter)
|
239
319
|
end
|
240
320
|
|
321
|
+
def to_s_root
|
322
|
+
if single_chapter_book?
|
323
|
+
out = "#{book}#{from_verse_part}#{to_verse_part}"
|
324
|
+
else
|
325
|
+
out = "#{book}#{from_part}#{to_part}"
|
326
|
+
end
|
327
|
+
end
|
328
|
+
|
329
|
+
def to_s_child
|
330
|
+
out = ','
|
331
|
+
if book_key != @parent.book_key
|
332
|
+
out << " #{to_s_root}"
|
333
|
+
else
|
334
|
+
out << "#{from_part}#{to_part}"
|
335
|
+
end
|
336
|
+
end
|
337
|
+
|
241
338
|
end
|
242
339
|
|
243
340
|
end
|
@@ -183,6 +183,14 @@ describe BiblePassage::BookDataStore do
|
|
183
183
|
# Use non-Hebrew numbering of Nehemiah 9/10
|
184
184
|
it_gets_number_of_verses(:neh, 9, 38)
|
185
185
|
it_gets_number_of_verses(:neh, 10, 39)
|
186
|
+
|
187
|
+
it_gets_number_of_verses(:neh, 7, 73)
|
188
|
+
it_gets_number_of_verses(:neh, 4, 23)
|
189
|
+
it_gets_number_of_verses(:neh, 3, 32)
|
190
|
+
|
191
|
+
it_gets_number_of_verses(:eccl, 5, 20)
|
192
|
+
it_gets_number_of_verses(:eccl, 4, 16)
|
193
|
+
|
186
194
|
end
|
187
195
|
|
188
196
|
end
|
@@ -9,6 +9,13 @@ describe BiblePassage::Reference do
|
|
9
9
|
it msg do
|
10
10
|
expect(BiblePassage::Reference.parse(ref_str).send($1)).to eq(val)
|
11
11
|
end
|
12
|
+
elsif mth.to_s =~ /^child_has_(\w+)$/
|
13
|
+
ref_str, val, msg = args
|
14
|
+
msg << " (#{ref_str})"
|
15
|
+
it msg do
|
16
|
+
expect(BiblePassage::Reference.parse(ref_str).child.send($1)).
|
17
|
+
to eq(val)
|
18
|
+
end
|
12
19
|
else
|
13
20
|
super
|
14
21
|
end
|
@@ -68,6 +75,9 @@ describe BiblePassage::Reference do
|
|
68
75
|
it_has_from_chapter('Luke12:3', 12, 'works when no space used')
|
69
76
|
|
70
77
|
it_has_from_chapter('Jude 2', 1, 'works for single chapter book')
|
78
|
+
|
79
|
+
it_has_from_chapter('Revelation 8:6 - 11:19', 8, 'works with spaces around hyphen')
|
80
|
+
|
71
81
|
end
|
72
82
|
|
73
83
|
context "from_verse" do
|
@@ -112,6 +122,67 @@ describe BiblePassage::Reference do
|
|
112
122
|
|
113
123
|
end
|
114
124
|
|
125
|
+
context "compound" do
|
126
|
+
|
127
|
+
context "full passage reference child" do
|
128
|
+
|
129
|
+
let(:ref) { BiblePassage::Reference }
|
130
|
+
|
131
|
+
it "behaves like a normal reference" do
|
132
|
+
expect(ref.parse("gen 1:2-3, ex 4:5-6:7").child).to eq(ref.parse("ex 4:5-6:7"))
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
context "from_chapter" do
|
138
|
+
|
139
|
+
child_has_from_chapter("Exodus 1:2-3:4,5", 3, "defaults to parent's to_chapter if supplied")
|
140
|
+
|
141
|
+
child_has_from_chapter("Exodus 1:2-3:4,5:6", 5, "uses supplied if chapter and verse given")
|
142
|
+
|
143
|
+
child_has_from_chapter("Exodus 1, 3", 3, 'uses supplied if chapter given')
|
144
|
+
|
145
|
+
child_has_from_chapter("Jude 1, 2", 1, 'sets to 1 if single-chapter book')
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
context "from_verse" do
|
150
|
+
|
151
|
+
child_has_from_verse("Exodus 1:2-3:4, 5", 5, "uses supplied from_verse")
|
152
|
+
|
153
|
+
child_has_from_verse("Exodus 1:2-3:4, 5:6", 6, "uses supplied if chapter and verse given")
|
154
|
+
|
155
|
+
child_has_from_verse("Exodus 1, 2:3", 3, "uses supplied if parent doesn't have one")
|
156
|
+
|
157
|
+
child_has_from_verse("Jude 1, 2", 2, "uses supplied for single-chapter book")
|
158
|
+
|
159
|
+
end
|
160
|
+
|
161
|
+
context "to_chapter" do
|
162
|
+
|
163
|
+
child_has_to_chapter("Exodus 1, 3-4", 4, "uses supplied if given")
|
164
|
+
|
165
|
+
child_has_to_chapter("Exodus 1, 3:4-5", 3, "doesn't use if not supplied")
|
166
|
+
|
167
|
+
child_has_to_chapter("Exodus 1, 3:4-5:6", 5, "uses if to_verse also supplied")
|
168
|
+
|
169
|
+
child_has_to_chapter("ex 1:2-3:4, 5:6-7:8 ", 7, "uses if all supplied")
|
170
|
+
end
|
171
|
+
|
172
|
+
context "to_verse" do
|
173
|
+
|
174
|
+
child_has_to_verse("Exodus 1, 2-3:4", 4, "uses supplied if chapter and verse given")
|
175
|
+
|
176
|
+
child_has_to_verse("Exodus 1, 2:3-4", 4, "uses supplied if just verse given")
|
177
|
+
|
178
|
+
child_has_to_verse("Exodus 1, 2:3", 3, "doesn't use if to-part not supplied")
|
179
|
+
|
180
|
+
child_has_to_verse("Exodus 1, 2-3", 22, "uses end verse if just to_chapter supplied")
|
181
|
+
|
182
|
+
end
|
183
|
+
|
184
|
+
end
|
185
|
+
|
115
186
|
end
|
116
187
|
|
117
188
|
context "whole_chapters?" do
|
@@ -178,7 +249,7 @@ describe BiblePassage::Reference do
|
|
178
249
|
|
179
250
|
context "to_s" do
|
180
251
|
|
181
|
-
def self.
|
252
|
+
def self.it_renders_reference(book_key, from_chapter, from_verse, to_chapter,
|
182
253
|
to_verse, passage, msg)
|
183
254
|
it "renders #{msg}" do
|
184
255
|
expect(BiblePassage::Reference.new(book_key, from_chapter, from_verse,
|
@@ -186,23 +257,37 @@ describe BiblePassage::Reference do
|
|
186
257
|
end
|
187
258
|
end
|
188
259
|
|
189
|
-
it_renders_passage(
|
260
|
+
def self.it_renders_passage(passage, expected)
|
261
|
+
it "renders #{passage} as #{expected}" do
|
262
|
+
expect(BiblePassage::Reference.parse(passage).to_s).to eq expected
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
it_renders_reference(:gen, 1, 2, 3, 4, 'Genesis 1:2-3:4', 'where all attributes differ')
|
190
267
|
|
191
|
-
|
268
|
+
it_renders_reference(:gen, 1, 2, 1, 3, 'Genesis 1:2-3', 'where only verses differ')
|
192
269
|
|
193
|
-
|
270
|
+
it_renders_reference(:gen, 1, nil, 2, nil, 'Genesis 1-2', 'where full chapters')
|
194
271
|
|
195
|
-
|
272
|
+
it_renders_reference(:gen, 1, nil, nil, nil, 'Genesis 1', 'where single chapter')
|
196
273
|
|
197
|
-
|
274
|
+
it_renders_reference(:gen, 1, 2, nil, nil, 'Genesis 1:2', 'where single verse')
|
198
275
|
|
199
|
-
|
276
|
+
it_renders_reference(:jude, 1, 2, 1, 4, 'Jude 2-4', 'where a single chapter book')
|
200
277
|
|
201
|
-
|
278
|
+
it_renders_reference(:jude, 1, 2, 1, 2, 'Jude 2', 'where a single verse in a single chapter book')
|
202
279
|
|
203
|
-
|
280
|
+
it_renders_reference(:jude, nil, nil, nil, nil, 'Jude', 'where a whole single chapter book')
|
204
281
|
|
205
|
-
|
282
|
+
it_renders_reference(:exod, nil, nil, nil, nil, 'Exodus', 'where a whole book')
|
283
|
+
|
284
|
+
it_renders_passage("gn 1:2-3:4, ex 5:6-7:8", "Genesis 1:2-3:4, Exodus 5:6-7:8")
|
285
|
+
|
286
|
+
it_renders_passage("ex 1:2-3:4, 5:6-7:8", 'Exodus 1:2-3:4, 5:6-7:8')
|
287
|
+
|
288
|
+
it_renders_passage("ex 1-2, 3-4", "Exodus 1-2, 3-4")
|
289
|
+
|
290
|
+
it_renders_passage("Ephesians 4:20-32, 5:18-19", "Ephesians 4:20-32, 5:18-19")
|
206
291
|
|
207
292
|
end
|
208
293
|
|
@@ -265,8 +350,31 @@ describe BiblePassage::Reference do
|
|
265
350
|
"Jude doesn't have any chapters")
|
266
351
|
end
|
267
352
|
|
353
|
+
it "errors when no book is supplied to parse" do
|
354
|
+
expect { BiblePassage::Reference.parse('2') }.to raise_error(
|
355
|
+
BiblePassage::InvalidReferenceError, "2 is not a valid reference")
|
356
|
+
end
|
357
|
+
|
268
358
|
end
|
269
359
|
|
270
360
|
end
|
271
361
|
|
362
|
+
context "inheritable attributes" do
|
363
|
+
|
364
|
+
let(:ref) { BiblePassage::Reference }
|
365
|
+
|
366
|
+
it "is empty if just book supplied" do
|
367
|
+
expect(ref.new(:gen).inheritable_attributes).to eq({})
|
368
|
+
end
|
369
|
+
|
370
|
+
it "returns book_key if chapters specified" do
|
371
|
+
expect(ref.new(:gen, 1).inheritable_attributes).to eq(book_key: :gen)
|
372
|
+
end
|
373
|
+
|
374
|
+
it "returns to_chapter as from_chapter if verses specified" do
|
375
|
+
expect(ref.new(:gen, 1, 2, 3, 4).inheritable_attributes).
|
376
|
+
to eq(book_key: :gen, from_chapter: 3)
|
377
|
+
end
|
378
|
+
|
379
|
+
end
|
272
380
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bible_passage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Si Wilkins
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|