bible_passage 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,346 @@
1
+ module BiblePassage
2
+
3
+ class BookKeyTranslator
4
+
5
+ TRANSLATIONS = {
6
+ ge: :gen,
7
+ gen: :gen,
8
+ gene: :gen,
9
+ genes: :gen,
10
+ genesis: :gen,
11
+ gn: :gen,
12
+
13
+ ex: :exod,
14
+ exo: :exod,
15
+ exod: :exod,
16
+ exodus: :exod,
17
+
18
+ le: :lev,
19
+ lev: :lev,
20
+ levit: :lev,
21
+ leviticus: :lev,
22
+
23
+ nm: :num,
24
+ nu: :num,
25
+ num: :num,
26
+ numb: :num,
27
+ numbers: :num,
28
+
29
+ de: :deut,
30
+ de: :deut,
31
+ deu: :deut,
32
+ deut: :deut,
33
+ deuter: :deut,
34
+ deuteronomy: :deut,
35
+ dt: :deut,
36
+
37
+ jos: :josh,
38
+ josh: :josh,
39
+ joshua: :josh,
40
+
41
+ jd: :judg,
42
+ jdg: :judg,
43
+ judg: :judg,
44
+ judges: :judg,
45
+
46
+ ruth: :ruth,
47
+
48
+ '1 sa'.to_sym => '1sam'.to_sym,
49
+ '1 sam'.to_sym => '1sam'.to_sym,
50
+ '1 samuel'.to_sym => '1sam'.to_sym,
51
+
52
+ '2 sa'.to_sym => '2sam'.to_sym,
53
+ '2 sam'.to_sym => '2sam'.to_sym,
54
+ '2 samuel'.to_sym => '2sam'.to_sym,
55
+
56
+ '1 kgs'.to_sym => '1kgs'.to_sym,
57
+ '1 ki'.to_sym => '1kgs'.to_sym,
58
+ '1 king'.to_sym => '1kgs'.to_sym,
59
+ '1 kings'.to_sym => '1kgs'.to_sym,
60
+ '1 kngs'.to_sym => '1kgs'.to_sym,
61
+
62
+ '2 kgs'.to_sym => '2kgs'.to_sym,
63
+ '2 ki'.to_sym => '2kgs'.to_sym,
64
+ '2 king'.to_sym => '2kgs'.to_sym,
65
+ '2 kings'.to_sym => '2kgs'.to_sym,
66
+ '2 kngs'.to_sym => '2kgs'.to_sym,
67
+
68
+ '1 ch'.to_sym => '1chr'.to_sym,
69
+ '1 chr'.to_sym => '1chr'.to_sym,
70
+ '1 chro'.to_sym => '1chr'.to_sym,
71
+ '1 chron'.to_sym => '1chr'.to_sym,
72
+ '1 chronicles'.to_sym => '1chr'.to_sym,
73
+
74
+ '2 ch'.to_sym => '2chr'.to_sym,
75
+ '2 chr'.to_sym => '2chr'.to_sym,
76
+ '2 chro'.to_sym => '2chr'.to_sym,
77
+ '2 chron'.to_sym => '2chr'.to_sym,
78
+ '2 chronicles'.to_sym => '2chr'.to_sym,
79
+
80
+ ezr: :ezra,
81
+ ezra: :ezra,
82
+
83
+ ne: :neh,
84
+ neh: :neh,
85
+ nehem: :neh,
86
+ nehemiah: :neh,
87
+
88
+ es: :esth,
89
+ est: :esth,
90
+ esth: :esth,
91
+ esther: :esth,
92
+
93
+ jo: :job,
94
+ job: :job,
95
+
96
+ ps: :ps,
97
+ psa: :ps,
98
+ psal: :ps,
99
+ psalm: :ps,
100
+ psalms: :ps,
101
+
102
+ pr: :prov,
103
+ pro: :prov,
104
+ prov: :prov,
105
+ proverbs: :prov,
106
+
107
+ ec: :eccl,
108
+ ecc: :eccl,
109
+ eccl: :eccl,
110
+ eccles: :eccl,
111
+ ecclesiastes: :eccl,
112
+
113
+ so: :song,
114
+ 'song of solomon'.to_sym => :song,
115
+ 'song of songs'.to_sym => :song,
116
+ 'song of sol'.to_sym => :song,
117
+ ss: :song,
118
+ ssong: :song,
119
+
120
+ is: :isa,
121
+ isa: :isa,
122
+ isaiah: :isa,
123
+
124
+ je: :jer,
125
+ jer: :jer,
126
+ jere: :jer,
127
+ jerem: :jer,
128
+ jeremiah: :jer,
129
+
130
+ la: :lam,
131
+ lam: :lam,
132
+ lamen: :lam,
133
+ lament: :lam,
134
+ lamentations: :lam,
135
+
136
+ eze: :ezek,
137
+ ezek: :ezek,
138
+ ezekiel: :ezek,
139
+
140
+ da: :dan,
141
+ dan: :dan,
142
+ daniel: :dan,
143
+
144
+ ho: :hos,
145
+ hos: :hos,
146
+ hosea: :hos,
147
+
148
+ joe: :joel,
149
+ joel: :joel,
150
+
151
+ am: :amos,
152
+ amos: :amos,
153
+
154
+ ob: :obad,
155
+ oba: :obad,
156
+ obad: :obad,
157
+ obadiah: :obad,
158
+
159
+ jon: :jonah,
160
+ jona: :jonah,
161
+ jonah: :jonah,
162
+
163
+ mi: :mic,
164
+ mic: :mic,
165
+ micah: :mic,
166
+
167
+ na: :nah,
168
+ nah: :nah,
169
+ nahum: :nah,
170
+
171
+ hab: :hab,
172
+ habak: :hab,
173
+ habakkuk: :hab,
174
+
175
+ zep: :zeph,
176
+ zeph: :zeph,
177
+ zephaniah: :zeph,
178
+
179
+ hag: :hag,
180
+ hagg: :hag,
181
+ haggai: :hag,
182
+
183
+ zec: :zech,
184
+ zech: :zech,
185
+ zechariah: :zech,
186
+
187
+ mal: :mal,
188
+ malac: :mal,
189
+ malach: :mal,
190
+ malachi: :mal,
191
+
192
+ mat: :matt,
193
+ matt: :matt,
194
+ matth: :matt,
195
+ matthew: :matt,
196
+ mt: :matt,
197
+
198
+ mar: :mark,
199
+ mark: :mark,
200
+ mr: :mark,
201
+ mrk: :mark,
202
+
203
+ lk: :luke,
204
+ lke: :luke,
205
+ lu: :luke,
206
+ luk: :luke,
207
+ luke: :luke,
208
+
209
+ jhn: :john,
210
+ jn: :john,
211
+ joh: :john,
212
+ john: :john,
213
+
214
+ ac: :acts,
215
+ act: :acts,
216
+ acts: :acts,
217
+
218
+ ro: :rom,
219
+ rom: :rom,
220
+ roman: :rom,
221
+ romans: :rom,
222
+
223
+ '1 co'.to_sym => '1cor'.to_sym,
224
+ '1 cor'.to_sym => '1cor'.to_sym,
225
+ '1 corin'.to_sym => '1cor'.to_sym,
226
+ '1 corinth'.to_sym => '1cor'.to_sym,
227
+ '1 corinthians'.to_sym => '1cor'.to_sym,
228
+
229
+ '2 co'.to_sym => '2cor'.to_sym,
230
+ '2 cor'.to_sym => '2cor'.to_sym,
231
+ '2 corin'.to_sym => '2cor'.to_sym,
232
+ '2 corinth'.to_sym => '2cor'.to_sym,
233
+ '2 corinthians'.to_sym => '2cor'.to_sym,
234
+
235
+ ga: :gal,
236
+ gal: :gal,
237
+ galat: :gal,
238
+ galatians: :gal,
239
+
240
+ ep: :eph,
241
+ eph: :eph,
242
+ ephes: :eph,
243
+ ephesians: :eph,
244
+
245
+ ph: :phil,
246
+ phi: :phil,
247
+ phil: :phil,
248
+ phili: :phil,
249
+ philip: :phil,
250
+ philipp: :phil,
251
+ philippians: :phil,
252
+ php: :phil,
253
+
254
+ co: :col,
255
+ col: :col,
256
+ colo: :col,
257
+ colos: :col,
258
+ coloss: :col,
259
+ colossians: :col,
260
+
261
+ '1 ti'.to_sym => '1tim'.to_sym,
262
+ '1 tim'.to_sym => '1tim'.to_sym,
263
+ '1 timothy'.to_sym => '1tim'.to_sym,
264
+
265
+ '1 th'.to_sym => '1thess'.to_sym,
266
+ '1 the'.to_sym => '1thess'.to_sym,
267
+ '1 thes'.to_sym => '1thess'.to_sym,
268
+ '1 thess'.to_sym => '1thess'.to_sym,
269
+ '1 thessalonians'.to_sym => '1thess'.to_sym,
270
+
271
+ '2 th'.to_sym => '2thess'.to_sym,
272
+ '2 the'.to_sym => '2thess'.to_sym,
273
+ '2 thes'.to_sym => '2thess'.to_sym,
274
+ '2 thess'.to_sym => '2thess'.to_sym,
275
+ '2 thessalonians'.to_sym => '2thess'.to_sym,
276
+
277
+ '2 ti'.to_sym => '2tim'.to_sym,
278
+ '2 tim'.to_sym => '2tim'.to_sym,
279
+ '2 timothy'.to_sym => '2tim'.to_sym,
280
+
281
+ tit: :titus,
282
+ titu: :titus,
283
+ titus: :titus,
284
+
285
+ phile: :phlm,
286
+ philem: :phlm,
287
+ philemon: :phlm,
288
+ phlm: :phlm,
289
+ phlmn: :phlm,
290
+ phm: :phlm,
291
+ phmn: :phlm,
292
+
293
+ he: :heb,
294
+ heb: :heb,
295
+ hebr: :heb,
296
+ hebrews: :heb,
297
+
298
+ ja: :jas,
299
+ jam: :jas,
300
+ james: :jas,
301
+ jas: :jas,
302
+
303
+ '1 pe'.to_sym => '1pet'.to_sym,
304
+ '1 pet'.to_sym => '1pet'.to_sym,
305
+ '1 peter'.to_sym => '1pet'.to_sym,
306
+
307
+ '2 pe'.to_sym => '2pet'.to_sym,
308
+ '2 pet'.to_sym => '2pet'.to_sym,
309
+ '2 peter'.to_sym => '2pet'.to_sym,
310
+
311
+ '1 jn'.to_sym => '1john'.to_sym,
312
+ '1 jo'.to_sym => '1john'.to_sym,
313
+ '1 joh'.to_sym => '1john'.to_sym,
314
+ '1 john'.to_sym => '1john'.to_sym,
315
+
316
+ '2 jn'.to_sym => '2john'.to_sym,
317
+ '2 jo'.to_sym => '2john'.to_sym,
318
+ '2 joh'.to_sym => '2john'.to_sym,
319
+ '2 john'.to_sym => '2john'.to_sym,
320
+
321
+ '3 jn'.to_sym => '3john'.to_sym,
322
+ '3 jo'.to_sym => '3john'.to_sym,
323
+ '3 joh'.to_sym => '3john'.to_sym,
324
+ '3 john'.to_sym => '3john'.to_sym,
325
+
326
+ jude: :jude,
327
+
328
+ re: :rev,
329
+ rev: :rev,
330
+ revel: :rev,
331
+ revelation: :rev,
332
+ }
333
+
334
+ def keyify(book_name)
335
+ TRANSLATIONS[normalize_input(book_name)] ||
336
+ raise(InvalidReferenceError.new("#{book_name} is not a valid book"))
337
+ end
338
+
339
+ private
340
+ def normalize_input(book_name)
341
+ book_name.strip.downcase.gsub(/\s+/, ' ').to_sym
342
+ end
343
+
344
+ end
345
+
346
+ end
@@ -0,0 +1 @@
1
+ class BiblePassage::InvalidReferenceError < ArgumentError;end
@@ -0,0 +1,243 @@
1
+ module BiblePassage
2
+
3
+ class Reference
4
+
5
+ class << self
6
+
7
+ def parse(passage, options = {})
8
+ translator = options.delete(:translator) || BookKeyTranslator.new
9
+ data_store = options[:data_store] ||= BookDataStore.new
10
+ match = passage.match(/^\s*(\d?\s*[A-Za-z\s]+)\s*(\d+)?:?(\d+)?-?(\d+)?:?(\d+)?/)
11
+ book_key = translator.keyify(match[1])
12
+ if data_store.number_of_chapters(book_key) == 1
13
+ process_single_chapter_match(book_key, match, options)
14
+ else
15
+ process_multi_chapter_match(book_key, match, options)
16
+ end
17
+ end
18
+
19
+
20
+ private
21
+ def process_multi_chapter_match(book_key, match, options)
22
+ if match[2]
23
+ from_chapter = match[2].to_i
24
+ # has from verse
25
+ if match[3]
26
+ from_verse = match[3].to_i
27
+ if match[5]
28
+ to_chapter = match[4].to_i
29
+ to_verse = match[5].to_i
30
+ else
31
+ to_verse = match[4].to_i if match[4]
32
+ end
33
+ else
34
+ from_verse = match[3].to_i if match[3]
35
+ to_chapter = match[4].to_i if match[4]
36
+ end
37
+ end
38
+ new(book_key, from_chapter, from_verse, to_chapter, to_verse, options)
39
+ end
40
+
41
+ def process_single_chapter_match(book_key, match, options)
42
+ if match[2]
43
+ from_verse = match[2].to_i
44
+ to_verse = match[4].to_i if match[4]
45
+ end
46
+ if match[0] =~ /:/
47
+ book_name = options[:data_store].book_name(book_key)
48
+ msg = "#{book_name} doesn't have any chapters"
49
+ raise InvalidReferenceError.new(msg)
50
+ end
51
+ new(book_key, nil, from_verse, nil, to_verse, options)
52
+ end
53
+
54
+ end
55
+
56
+ attr_reader :book_key, :book
57
+
58
+ def initialize(book_key, from_chapter = nil, from_verse = nil,
59
+ to_chapter = nil, to_verse = nil, options = {})
60
+ @data_store = options[:data_store] || BookDataStore.new
61
+ self.book_key = book_key
62
+ self.from_chapter = int_param(from_chapter)
63
+ self.from_verse = int_param(from_verse)
64
+ self.to_chapter = calculate_to_chapter(to_chapter)
65
+ self.to_verse = calculate_to_verse(to_verse)
66
+ end
67
+
68
+ def book_key=(key)
69
+ @book = @data_store.book_name(key)
70
+ @book_key = key
71
+ end
72
+
73
+ def from_chapter
74
+ @from_chapter || 1
75
+ end
76
+
77
+ def from_chapter=(val)
78
+ if val
79
+ raise InvalidReferenceError.new(
80
+ "#{book} doesn't have a chapter #{val}") if val < 1 ||
81
+ val > @data_store.number_of_chapters(book_key)
82
+ @from_chapter = val
83
+ end
84
+ end
85
+
86
+ def from_verse
87
+ @from_verse || 1
88
+ end
89
+
90
+ def from_verse=(val)
91
+ if val
92
+ raise InvalidReferenceError.new(
93
+ "#{book} #{from_chapter} doesn't have a verse #{val}") if val < 1 ||
94
+ val > @data_store.number_of_verses(book_key, from_chapter)
95
+ @from_verse = val
96
+ end
97
+ end
98
+
99
+ def to_chapter
100
+ @to_chapter || from_chapter
101
+ end
102
+
103
+ def to_chapter=(val)
104
+ if val
105
+ raise InvalidReferenceError.new(
106
+ "to_chapter cannot be before from_chapter") if val < from_chapter
107
+ raise InvalidReferenceError.new(
108
+ "#{book} doesn't have a chapter #{val}") if val >
109
+ @data_store.number_of_chapters(book_key)
110
+ @to_chapter = val
111
+ end
112
+ end
113
+
114
+ def to_verse
115
+ @to_verse || from_verse
116
+ end
117
+
118
+ def to_verse=(val)
119
+ if val
120
+ raise InvalidReferenceError.new(
121
+ "to_verse cannot be before from_verse") if val < from_verse &&
122
+ single_chapter_passage?
123
+ raise InvalidReferenceError.new(
124
+ "#{book} #{to_chapter} doesn't have a verse #{val}") if val >
125
+ @data_store.number_of_verses(book_key, to_chapter)
126
+ @to_verse = val
127
+ end
128
+ end
129
+
130
+ def to_s
131
+ if single_chapter_book?
132
+ "#{book}#{from_verse_part}#{to_verse_part}"
133
+ else
134
+ "#{book}#{from_part}#{to_part}"
135
+ end
136
+ end
137
+
138
+ def whole_chapters?
139
+ from_verse == 1 && to_verse_last_in_chapter?
140
+ end
141
+
142
+ def whole_book?
143
+ from_chapter == 1 && from_verse == 1 &&
144
+ to_chapter == @data_store.number_of_chapters(book_key) &&
145
+ to_verse == @data_store.number_of_verses(book_key, to_chapter)
146
+ end
147
+
148
+ def whole_chapter?
149
+ whole_chapters? && from_chapter == to_chapter
150
+ end
151
+
152
+ private
153
+ def from_part
154
+ out = ''
155
+ unless whole_book?
156
+ out << ' '
157
+ out << from_chapter.to_s
158
+ out << from_verse_part
159
+ end
160
+ out
161
+ end
162
+
163
+ def from_verse_part
164
+ if whole_chapters?
165
+ ''
166
+ else
167
+ out = single_chapter_book? ? ' ' : ':'
168
+ "#{out}#{from_verse.to_s}"
169
+ end
170
+ end
171
+
172
+ def to_part
173
+ out = ''
174
+ unless whole_book? || single_verse_passage? || whole_chapter?
175
+ out << '-'
176
+ unless single_chapter_passage?
177
+ out << to_chapter_part
178
+ end
179
+ out << to_verse_part
180
+ end
181
+ out
182
+ end
183
+
184
+ def to_chapter_part
185
+ if single_chapter_passage?
186
+ ''
187
+ else
188
+ to_chapter.to_s
189
+ end
190
+ end
191
+
192
+ def to_verse_part
193
+ if single_verse_passage? || whole_chapters?
194
+ ''
195
+ else
196
+ out = ''
197
+ out << '-' if single_chapter_book?
198
+ out << ':' unless single_chapter_passage?
199
+ out << to_verse.to_s
200
+ out
201
+ end
202
+ end
203
+
204
+
205
+ def int_param(param)
206
+ param ? param.to_i : nil
207
+ end
208
+
209
+ def single_chapter_passage?
210
+ from_chapter == to_chapter
211
+ end
212
+
213
+ def single_verse_passage?
214
+ single_chapter_passage? && from_verse == to_verse
215
+ end
216
+
217
+ def single_chapter_book?
218
+ @data_store.number_of_chapters(book_key) == 1
219
+ end
220
+
221
+ def to_verse_last_in_chapter?
222
+ to_verse == @data_store.number_of_verses(book_key, to_chapter)
223
+ end
224
+
225
+ def calculate_to_chapter(supplied_to_chapter)
226
+ int_param(supplied_to_chapter) || begin
227
+ if @from_chapter
228
+ @from_chapter
229
+ else
230
+ @data_store.number_of_chapters(book_key)
231
+ end
232
+ end
233
+ end
234
+
235
+ def calculate_to_verse(supplied_to_verse)
236
+ int_param(supplied_to_verse) ||
237
+ @from_verse ||
238
+ @data_store.number_of_verses(book_key, self.to_chapter)
239
+ end
240
+
241
+ end
242
+
243
+ end
@@ -0,0 +1,5 @@
1
+ require_relative 'bible_passage/reference'
2
+ require_relative 'bible_passage/book_key_translator'
3
+ require_relative 'bible_passage/invalid_reference_error'
4
+ require_relative 'bible_passage/book_data_store'
5
+