scriptura 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,5 @@
1
+ require 'yaml'
2
+
1
3
  module Scriptura
2
4
  module Scripture
3
5
  def self.old_testament_books
@@ -23,5 +25,15 @@ module Scriptura
23
25
  def self.book_names
24
26
  books.map(&:name)
25
27
  end
28
+
29
+ def self.metadata
30
+ @metadata ||= begin
31
+ YAML.load_file('lib/scriptura/metadata.yml')
32
+ end
33
+ end
34
+
35
+ def self.find_book_hash attribute, value
36
+ metadata.values.find { |book| book[attribute.to_s] == value }
37
+ end
26
38
  end
27
39
  end
@@ -1,21 +1,26 @@
1
+ require 'scriptura/scripture'
2
+
1
3
  module Scriptura
2
4
  class ScriptureBook
5
+ attr_reader :book_number
6
+ alias_method :number, :book_number
7
+
3
8
  def initialize(number)
4
- raise ArgumentError, 'book number cannot be converted to an integer' unless number.respond_to?(:to_i)
5
- raise 'book number should be within 1-66' unless (1..66).cover?(number.to_i)
6
- @number = number.to_i
9
+ fail ArgumentError, 'book number cannot be converted to an integer' unless number.respond_to?(:to_i)
10
+ fail DoesNotExist, 'Book number should be within 1-66' unless (1..66).cover?(number.to_i)
11
+ @book_number = number.to_i
7
12
  end
8
13
 
9
- def self.find_by_name name
10
- hash = METADATA.find{|book| book['name'] == name}
11
- raise "Book by the name #{name} was not found! Check if there was a spelling error.. or try initializing it with a book number" if hash.nil?
12
- self.new(hash['number'])
14
+ def self.find_by_name(name)
15
+ hash = Scripture.find_book_hash(:name, name)
16
+ fail "Book by the name #{name} was not found! Check if there was a spelling error.. or try initializing it with a book number" if hash.nil?
17
+ new(hash['number'])
13
18
  end
14
19
 
15
- def self.find string_id
16
- hash = METADATA.find{|book| book['string_id'] == string_id}
17
- raise "Book by the string_id #{string_id} was not found! Check if there was a spelling error.. or try initializing it with a book number" if hash.nil?
18
- self.new(hash['number'])
20
+ def self.find(string_id)
21
+ hash = Scripture.find_book_hash(:string_id, string_id)
22
+ fail "Book by the string_id #{string_id} was not found! Check if there was a spelling error.. or try initializing it with a book number" if hash.nil?
23
+ new(hash['number'])
19
24
  end
20
25
 
21
26
  def name
@@ -30,16 +35,12 @@ module Scriptura
30
35
  book_hash['chapters'].count
31
36
  end
32
37
 
33
- def number
34
- book_hash['number']
35
- end
36
-
37
38
  def first_chapter
38
- @first_chapter ||= ScriptureChapter.new(self.number, 1)
39
+ @first_chapter ||= ScriptureChapter.new(number, 1)
39
40
  end
40
41
 
41
42
  def last_chapter
42
- @last_chapter ||= ScriptureChapter.new(self.number, self.number_of_chapters)
43
+ @last_chapter ||= ScriptureChapter.new(number, number_of_chapters)
43
44
  end
44
45
 
45
46
  def first_chapter_number
@@ -57,11 +58,22 @@ module Scriptura
57
58
  def string_id
58
59
  book_hash['string_id']
59
60
  end
60
-
61
+
62
+ def to_s
63
+ name
64
+ end
65
+
66
+ def ==(other)
67
+ return false unless other.is_a?(ScriptureBook)
68
+ book_number == other.book_number
69
+ end
70
+
61
71
  private
62
72
 
63
73
  def book_hash
64
- @book_hash ||= METADATA.find{|book| book['number'] == @number.to_i}
74
+ @book_hash ||= Scripture.metadata[@book_number.to_i]
65
75
  end
76
+
77
+ class DoesNotExist < StandardError; end
66
78
  end
67
79
  end
@@ -1,17 +1,17 @@
1
+ require 'scriptura/scripture_book'
2
+
1
3
  module Scriptura
2
4
  class ScriptureChapter
3
- attr_accessor :scripture_book
5
+ attr_reader :scripture_book
6
+ attr_reader :chapter_number
7
+ alias_method :number, :chapter_number
4
8
 
5
- def initialize book_number, chapter_number
9
+ def initialize(book_number, chapter_number)
6
10
  @scripture_book = ScriptureBook.new(book_number)
7
- raise ArgumentError, 'chapter number cannot be converted to an integer' unless chapter_number.respond_to?(:to_i)
8
- raise 'book number should be within 1-66' unless (1..66).cover?(book_number.to_i)
9
- @number = chapter_number.to_i
10
- raise 'chapter does not exist' if chapter_hash.nil?
11
- end
12
-
13
- def number
14
- chapter_hash['number']
11
+ fail ArgumentError, 'chapter number cannot be converted to an integer' unless chapter_number.respond_to?(:to_i)
12
+ fail 'book number should be within 1-66' unless (1..66).cover?(book_number.to_i)
13
+ @chapter_number = chapter_number.to_i
14
+ fail DoesNotExist, 'chapter does not exist' if chapter_hash.nil?
15
15
  end
16
16
 
17
17
  def number_of_verses
@@ -19,11 +19,11 @@ module Scriptura
19
19
  end
20
20
 
21
21
  def first_verse
22
- @first_verse = ScriptureVerse.new(scripture_book.number, self.number, 1)
22
+ @first_verse = ScriptureVerse.new(scripture_book.number, number, 1)
23
23
  end
24
24
 
25
25
  def last_verse
26
- @first_verse = ScriptureVerse.new(scripture_book.number, self.number, self.number_of_verses)
26
+ @first_verse = ScriptureVerse.new(scripture_book.number, number, number_of_verses)
27
27
  end
28
28
 
29
29
  def first_verse_number
@@ -34,10 +34,22 @@ module Scriptura
34
34
  last_verse.number
35
35
  end
36
36
 
37
+ def to_s
38
+ "#{scripture_book} #{@chapter_number}"
39
+ end
40
+
41
+ def ==(other)
42
+ return false unless other.is_a?(ScriptureChapter)
43
+ scripture_book == other.scripture_book &&
44
+ chapter_number == other.chapter_number
45
+ end
46
+
37
47
  private
38
48
 
39
49
  def chapter_hash
40
- @chapter_hash ||= @scripture_book.to_hash['chapters'].find{|chapter| chapter['number'] == @number.to_i}
50
+ @chapter_hash ||= @scripture_book.to_hash['chapters'][@chapter_number.to_i]
41
51
  end
52
+
53
+ class DoesNotExist < StandardError; end
42
54
  end
43
55
  end
@@ -1,55 +1,53 @@
1
+ require 'scriptura/scripture_verse'
2
+
1
3
  module Scriptura
2
4
  class ScriptureReference
3
- attr_accessor :start_verse
4
- attr_accessor :end_verse
5
+ attr_reader :start_verse
6
+ attr_reader :end_verse
5
7
 
6
- def initialize start_verse, end_verse
8
+ def initialize(start_verse, end_verse)
7
9
  @start_verse = start_verse
8
10
  @end_verse = end_verse
9
11
  end
10
12
 
11
13
  def within_same_book?
12
- start_verse.scripture_book.number == end_verse.scripture_book.number
14
+ start_verse.scripture_book == end_verse.scripture_book
13
15
  end
14
16
 
15
17
  def within_same_chapter?
16
- within_same_book? &&
17
- start_verse.scripture_chapter.number == end_verse.scripture_chapter.number
18
+ start_verse.scripture_chapter == end_verse.scripture_chapter
18
19
  end
19
-
20
+
20
21
  def spans_entire_book?
21
- within_same_book? &&
22
- start_verse.scripture_chapter.number == start_verse.scripture_book.first_chapter.number &&
23
- start_verse.number == start_verse.scripture_chapter.first_verse.number &&
24
- end_verse.scripture_chapter.number == end_verse.scripture_book.last_chapter.number &&
25
- end_verse.number == end_verse.scripture_chapter.last_verse.number
22
+ scripture_book = start_verse.scripture_book
23
+ start_verse == scripture_book.first_chapter.first_verse &&
24
+ end_verse == scripture_book.last_chapter.last_verse
26
25
  end
27
26
 
28
27
  def spans_entire_chapter?
29
- within_same_chapter? &&
30
- start_verse.number == start_verse.scripture_chapter.first_verse.number &&
31
- end_verse.number == end_verse.scripture_chapter.last_verse.number
28
+ scripture_chapter = start_verse.scripture_chapter
29
+ start_verse == scripture_chapter.first_verse &&
30
+ end_verse == scripture_chapter.last_verse
32
31
  end
33
32
 
34
33
  def spans_single_verse?
35
- within_same_chapter? &&
36
- start_verse.number == end_verse.number
34
+ start_verse == end_verse
37
35
  end
38
36
 
39
37
  def to_s
40
- case
41
- when self.spans_entire_book?
42
- start_verse.scripture_book.name
43
- when self.spans_entire_chapter?
44
- "#{start_verse.scripture_book.name} #{start_verse.scripture_chapter.number}"
45
- when self.spans_single_verse?
46
- "#{start_verse.scripture_book.name} #{start_verse.scripture_chapter.number}:#{start_verse.number}"
38
+ case
39
+ when spans_entire_book?
40
+ start_verse.scripture_book.to_s
41
+ when spans_entire_chapter?
42
+ start_verse.scripture_chapter.to_s
43
+ when spans_single_verse?
44
+ start_verse.to_s
47
45
  when within_same_chapter?
48
- "#{start_verse.scripture_book.name} #{start_verse.scripture_chapter.number}:#{start_verse.number}-#{end_verse.number}"
46
+ "#{start_verse}-#{end_verse.number}"
49
47
  when within_same_book?
50
- "#{start_verse.scripture_book.name} #{start_verse.scripture_chapter.number}:#{start_verse.number} - #{end_verse.scripture_chapter.number}:#{end_verse.number}"
48
+ "#{start_verse} - #{end_verse.scripture_chapter.number}:#{end_verse.number}"
51
49
  else
52
- "#{start_verse.scripture_book.name} #{start_verse.scripture_chapter.number}:#{start_verse.number} - #{end_verse.scripture_book.name} #{end_verse.scripture_chapter.number}:#{end_verse.number}"
50
+ "#{start_verse} - #{end_verse}"
53
51
  end
54
52
  end
55
53
  end
@@ -1,37 +1,50 @@
1
+ require 'scriptura/scripture_chapter'
2
+
1
3
  module Scriptura
2
4
  class ScriptureVerse
3
- attr_accessor :scripture_chapter
4
- attr_accessor :number
5
+ attr_reader :scripture_book
6
+ attr_reader :scripture_chapter
7
+ attr_reader :verse_number
8
+ alias_method :number, :verse_number
5
9
 
6
- def initialize *args
10
+ def initialize(*args)
7
11
  case
8
12
  when args.count == 1 && args[0].is_a?(Fixnum)
9
- raise ArgumentError, 'normalized value of scripture verse cannot be converted to an integer' unless args[0].respond_to?(:to_i)
10
- book_number, chapter_number, verse_number = de_normalize(args[0])
11
- when args.count == 3
13
+ fail ArgumentError, 'normalized value of scripture verse cannot be converted to an integer' unless args[0].respond_to?(:to_i)
14
+ book_number, chapter_number, verse_number = de_normalize(args[0])
15
+ when args.count == 3
12
16
  book_number, chapter_number, verse_number = args
13
17
  end
14
18
  @scripture_chapter = ScriptureChapter.new(book_number, chapter_number)
15
- raise ArgumentError, 'verse number cannot be converted to an integer' unless verse_number.respond_to?(:to_i)
16
- raise 'verse number does not exist' unless (1..@scripture_chapter.number_of_verses).cover? verse_number.to_i
17
- @number = verse_number.to_i
19
+ @scripture_book = @scripture_chapter.scripture_book
20
+ fail ArgumentError, 'verse number cannot be converted to an integer' unless verse_number.respond_to?(:to_i)
21
+ fail DoesNotExist, 'verse number does not exist' unless (1..@scripture_chapter.number_of_verses).cover? verse_number.to_i
22
+ @verse_number = verse_number.to_i
18
23
  end
19
24
 
20
- def scripture_book
21
- @scripture_chapter.scripture_book
25
+ def normalize
26
+ scripture_book.number * 1_000_000 + scripture_chapter.number * 1000 + @verse_number
22
27
  end
23
28
 
24
- def normalize
25
- scripture_book.number * 1000000 + scripture_chapter.number * 1000 + @number
29
+ def to_s
30
+ "#{scripture_chapter}:#{number}"
31
+ end
32
+
33
+ def ==(other)
34
+ return false unless other.is_a?(ScriptureVerse)
35
+ scripture_book == other.scripture_book &&
36
+ scripture_chapter == other.scripture_chapter &&
37
+ verse_number == other.verse_number
26
38
  end
27
39
 
28
- private
40
+ private
29
41
 
30
- def de_normalize number
31
- quotient, verse_number = number.divmod(1000)
32
- book_number, chapter_number = quotient.divmod(1000)
42
+ def de_normalize(number)
43
+ quotient, verse_number = number.divmod(1000)
44
+ book_number, chapter_number = quotient.divmod(1000)
33
45
  [book_number, chapter_number, verse_number]
34
46
  end
35
47
 
48
+ class DoesNotExist < StandardError; end
36
49
  end
37
50
  end
@@ -0,0 +1,5 @@
1
+ module Scriptura
2
+ module Version
3
+ STRING = '0.0.3'
4
+ end
5
+ end
metadata CHANGED
@@ -1,46 +1,89 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scriptura
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gagan Awhad
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-05 00:00:00.000000000 Z
11
+ date: 2015-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: safe_yaml
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rspec
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
- - - '>='
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
18
46
  - !ruby/object:Gem::Version
19
47
  version: '0'
20
48
  type: :development
21
49
  prerelease: false
22
50
  version_requirements: !ruby/object:Gem::Requirement
23
51
  requirements:
24
- - - '>='
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
25
60
  - !ruby/object:Gem::Version
26
61
  version: '0'
27
- description: This is a ruby library that provides operations related with meta information
28
- about the Bible
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A ruby library that attempts to provide an intuitive API for operations
70
+ on meta information about the Bible
29
71
  email:
30
72
  - gagan.a.awhad@gmail.com
31
73
  executables: []
32
74
  extensions: []
33
75
  extra_rdoc_files: []
34
76
  files:
35
- - lib/scriptura/data.rb
77
+ - LICENSE
78
+ - README.md
79
+ - lib/scriptura.rb
80
+ - lib/scriptura/metadata.yml
36
81
  - lib/scriptura/scripture.rb
37
82
  - lib/scriptura/scripture_book.rb
38
83
  - lib/scriptura/scripture_chapter.rb
39
84
  - lib/scriptura/scripture_reference.rb
40
85
  - lib/scriptura/scripture_verse.rb
41
- - lib/scriptura.rb
42
- - README.md
43
- - LICENSE
86
+ - lib/scriptura/version.rb
44
87
  homepage: https://www.github.com/gaganawhad/scriptura
45
88
  licenses: []
46
89
  metadata: {}
@@ -50,20 +93,19 @@ require_paths:
50
93
  - lib
51
94
  required_ruby_version: !ruby/object:Gem::Requirement
52
95
  requirements:
53
- - - '>='
96
+ - - ">="
54
97
  - !ruby/object:Gem::Version
55
98
  version: '0'
56
99
  required_rubygems_version: !ruby/object:Gem::Requirement
57
100
  requirements:
58
- - - '>='
101
+ - - ">="
59
102
  - !ruby/object:Gem::Version
60
103
  version: '0'
61
104
  requirements: []
62
105
  rubyforge_project:
63
- rubygems_version: 2.0.3
106
+ rubygems_version: 2.4.6
64
107
  signing_key:
65
108
  specification_version: 4
66
- summary: This is a ruby library that provides operations related with meta information
67
- about the Bible
109
+ summary: A ruby library that attempts to provide an intuitive API for operations on
110
+ meta information about the Bible
68
111
  test_files: []
69
- has_rdoc: