acts_as_scriptural 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 64fb01e0e571241d7b4bc21eee9ad4a6f6261fce
4
- data.tar.gz: 4a7f618751b72b9f268bfd654f2b742e4884459d
3
+ metadata.gz: 8933d939f83cae6978c3e39faffc412ba768d858
4
+ data.tar.gz: 8ca2c6d30c653bf91bfa7ffb2375d76a473c6d8c
5
5
  SHA512:
6
- metadata.gz: 39846e58da1374cd9bdd9b87521e17f7f23e0eafa721d14c5efc67ae13ec3a18f9f70374b5a77b5beef5fdb531b7d91988423cae2fcd001bc6f2f325c193b0be
7
- data.tar.gz: fbadacdc09ddd72751ac6bca1e9f35acdf9ef43b1aa6ced9b4821159809a7ca2cb0237e9ec1aaa92d92b6ba5c1e81ec8ab1aac2bcfd34f48b190b914c10adecc
6
+ metadata.gz: 191a0d70d2056376d5705cb50b9055b5ecb98be3020a5c83c8854556aa36f852f0e00306412ea58ea152eb2471d514d12c2ea9647626c38a39f14255d4a8b840
7
+ data.tar.gz: 1c9297b346767f2fcee826a022cad14d171d6040f596318a0675fa3e7eb1d969b28f3cb1bc3a5e708dca204bd3d8477bcc7caa001babbd1ffe23be1864d83dc8
@@ -20,11 +20,14 @@ class ActsAsScriptural::Bible
20
20
  chapter_number.between?(1, @indexhash[book_index].num_chapters)
21
21
  end
22
22
 
23
+ def chapters_in_book(book_index)
24
+ @indexhash[book_index].num_chapters
25
+ end
26
+
23
27
  private
24
28
 
25
29
  def import_from_file
26
- datadir = Gem.datadir('acts_as_scriptural')
27
- File.readlines("#{datadir}/english.txt").each do |line|
30
+ File.readlines("data/acts_as_scriptural/english.txt").each do |line|
28
31
  book_name, book_index, num_chapters = line.chomp.split(',')
29
32
  book = ActsAsScriptural::Book.new(book_name, book_index.to_i, num_chapters.to_i)
30
33
  @namehash[book_name] = book
@@ -0,0 +1,5 @@
1
+ class ParsedReference
2
+
3
+ attr_accessor :first_book, :last_book, :first_chapter, :last_chapter, :first_book_index, :last_book_index
4
+
5
+ end
@@ -1,17 +1,57 @@
1
1
  class ActsAsScriptural::Parser
2
+ require 'ostruct'
2
3
 
3
- def self.book_and_chapters_from_reference(reference)
4
- regex = /^\s*([0-9]?\s*[a-zA-Z]+)\.?\s*([0-9]+)(?:\s*(?:-|..)[^0-9]*([0-9]+))?/
5
- match = reference.match(regex)
4
+ def parse_reference(reference)
5
+ result = ParsedReference.new
6
+ # this reference needs to have no spaces to be parsed properly;
7
+ # the matches will contain (if applicable)
8
+ # match 1: first book name
9
+ # match 2: first chapter
10
+ # match 3: second book name (if present)
11
+ # match 4: last chapter
12
+ regex = /^([0-9]?[a-zA-Z]+)\.?([0-9]+)(?:-|..)?(?:([0-9]?[a-zA-Z]+)?\.?([0-9]+))?/
13
+ match = reference.gsub(/\s+/,'').match(regex)
6
14
  if match
7
- if match[3]
8
- chapters = (match[2]..match[3]).to_a
9
- else
10
- chapters = [ match[2] ]
15
+ if (match[3].nil? && match[4].nil?) # only one chapter
16
+ result.first_book = match[1]
17
+ result.last_book = match[1]
18
+ result.first_chapter = match[2].to_i
19
+ result.last_chapter = match[2].to_i
20
+ elsif match[3] # the range must include two books
21
+ result.first_book = match[1]
22
+ result.last_book = match[3]
23
+ result.first_chapter = match[2].to_i
24
+ result.last_chapter = match[4].to_i
25
+ else # just one book in the range
26
+ result.first_book = match[1]
27
+ result.last_book = match[1]
28
+ result.first_chapter = match[2].to_i
29
+ result.last_chapter = match[4].to_i
11
30
  end
12
- [ match[1], chapters.map(&:to_i) ]
31
+ result.first_book_index = ActsAsScriptural::AbbreviationLookup.new.index_number(result.first_book)
32
+ result.last_book_index = ActsAsScriptural::AbbreviationLookup.new.index_number(result.last_book)
33
+ result = validate_chapter_ranges(result)
34
+ result
13
35
  else
14
- [nil, nil]
36
+ nil
15
37
  end
16
38
  end
39
+
40
+ def validate_chapter_ranges(result)
41
+ # doesn't allow nonsensical chapter numbers
42
+ bible = ActsAsScriptural::Bible.new
43
+ max_first_book_chapters = bible.chapters_in_book(result.first_book_index)
44
+ max_last_book_chapters = bible.chapters_in_book(result.last_book_index)
45
+
46
+ result.first_chapter = 1 if result.first_chapter < 1
47
+ result.last_chapter = 1 if result.first_chapter < 1
48
+ result.first_chapter = max_first_book_chapters if result.first_chapter > max_first_book_chapters
49
+ result.last_chapter = max_last_book_chapters if result.last_chapter > max_last_book_chapters
50
+
51
+ result
52
+ end
53
+
54
+
17
55
  end
56
+
57
+
@@ -1,15 +1,6 @@
1
- require_relative "acts_as_scriptural/version"
2
- require_relative "acts_as_scriptural/book"
3
- require_relative "acts_as_scriptural/bible"
4
- require_relative "acts_as_scriptural/parser"
5
- require_relative "acts_as_scriptural/abbreviation_lookup"
6
-
7
-
8
-
9
-
10
1
  class ActsAsScriptural
11
2
 
12
- attr_accessor :chapters
3
+ attr_accessor :chapters, :parsed
13
4
 
14
5
  def initialize
15
6
  @chapters = Array.new
@@ -17,20 +8,29 @@ class ActsAsScriptural
17
8
 
18
9
  def parse(str)
19
10
  references = str.split(',')
20
- references.each {|ref| parse_a_reference(ref) }
11
+ references.each do |ref|
12
+ add_chapters(Parser.new.parse_reference(ref))
13
+ end
21
14
  self
22
15
  end
23
16
 
24
- def parse_a_reference(reference)
25
- book, chapters = Parser.book_and_chapters_from_reference(reference)
26
- add_chapters(book, chapters)
27
- end
17
+ def add_chapters(parsed_reference)
18
+ for i in parsed_reference.first_book_index..parsed_reference.last_book_index do
28
19
 
29
- def add_chapters(book, chapters)
30
- book_index = lookup.index_number(book)
31
- if book_index && chapters
32
- chapters.each do |c|
33
- @chapters << [book_index, c] if bible.chapter_exists?(book_index, c)
20
+ if (i == parsed_reference.first_book_index)
21
+ first_chapter = parsed_reference.first_chapter
22
+ else
23
+ first_chapter = 1
24
+ end
25
+
26
+ if (i == parsed_reference.last_book_index)
27
+ last_chapter = parsed_reference.last_chapter
28
+ else
29
+ last_chapter = bible.chapters_in_book(i)
30
+ end
31
+
32
+ for j in first_chapter..last_chapter
33
+ @chapters << [i, j]
34
34
  end
35
35
  end
36
36
  end
@@ -45,3 +45,9 @@ class ActsAsScriptural
45
45
 
46
46
 
47
47
  end
48
+
49
+ require_relative "acts_as_scriptural/book"
50
+ require_relative "acts_as_scriptural/bible"
51
+ require_relative "acts_as_scriptural/parser"
52
+ require_relative "acts_as_scriptural/abbreviation_lookup"
53
+ require_relative "acts_as_scriptural/parsed_reference"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_scriptural
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philip Bradley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-12 00:00:00.000000000 Z
11
+ date: 2015-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: 'Parse multibook, multiverse bible references of the form: Acts 1:1-10;
56
70
  Eph 1:5-10; John 12:4'
57
71
  email:
@@ -65,8 +79,8 @@ files:
65
79
  - lib/acts_as_scriptural/abbreviation_lookup.rb
66
80
  - lib/acts_as_scriptural/bible.rb
67
81
  - lib/acts_as_scriptural/book.rb
82
+ - lib/acts_as_scriptural/parsed_reference.rb
68
83
  - lib/acts_as_scriptural/parser.rb
69
- - lib/acts_as_scriptural/version.rb
70
84
  homepage: ''
71
85
  licenses:
72
86
  - MIT
@@ -1,3 +0,0 @@
1
- class ActsAsScriptural
2
- VERSION = "0.0.1"
3
- end