Class: Pericope::Verse
- Inherits:
-
Struct
- Object
- Struct
- Pericope::Verse
- Includes:
- Comparable
- Defined in:
- lib/pericope/verse.rb
Instance Attribute Summary collapse
-
#book ⇒ Object
Returns the value of attribute book.
-
#chapter ⇒ Object
Returns the value of attribute chapter.
-
#letter ⇒ Object
Returns the value of attribute letter.
-
#verse ⇒ Object
Returns the value of attribute verse.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
-
#initialize(book, chapter, verse, letter = nil) ⇒ Verse
constructor
A new instance of Verse.
- #next ⇒ Object (also: #succ)
- #partial? ⇒ Boolean
- #to_i ⇒ Object (also: #number)
- #to_id ⇒ Object
- #to_s(with_chapter: false) ⇒ Object
- #whole ⇒ Object
- #whole? ⇒ Boolean
Constructor Details
#initialize(book, chapter, verse, letter = nil) ⇒ Verse
Returns a new instance of Verse.
5 6 7 8 9 10 11 |
# File 'lib/pericope/verse.rb', line 5 def initialize(book, chapter, verse, letter=nil) super raise ArgumentError, "#{book} is not a valid book" if book < 1 || book > 66 raise ArgumentError, "#{chapter} is not a valid chapter" if chapter < 1 || chapter > Pericope.get_max_chapter(book) raise ArgumentError, "#{verse} is not a valid verse" if verse < 1 || verse > Pericope.get_max_verse(book, chapter) end |
Instance Attribute Details
#book ⇒ Object
Returns the value of attribute book
2 3 4 |
# File 'lib/pericope/verse.rb', line 2 def book @book end |
#chapter ⇒ Object
Returns the value of attribute chapter
2 3 4 |
# File 'lib/pericope/verse.rb', line 2 def chapter @chapter end |
#letter ⇒ Object
Returns the value of attribute letter
2 3 4 |
# File 'lib/pericope/verse.rb', line 2 def letter @letter end |
#verse ⇒ Object
Returns the value of attribute verse
2 3 4 |
# File 'lib/pericope/verse.rb', line 2 def verse @verse end |
Class Method Details
.parse(input) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/pericope/verse.rb', line 13 def self.parse(input) return nil unless input id = input.to_i book = id / 1000000 # the book is everything left of the least significant 6 digits chapter = (id % 1000000) / 1000 # the chapter is the 3rd through 6th most significant digits verse = id % 1000 # the verse is the 3 least significant digits letter = input[Pericope.letter_regexp] if input.is_a?(String) new(book, chapter, verse, letter) end |
Instance Method Details
#<=>(other) ⇒ Object
23 24 25 26 |
# File 'lib/pericope/verse.rb', line 23 def <=>(other) raise ArgumentError, "Comparison of Pericope::Verse with #{other.class} failed" unless other.is_a?(Pericope::Verse) [ book, chapter, verse, letter || "a" ] <=> [ other.book, other.chapter, other.verse, other.letter || "a" ] end |
#==(other) ⇒ Object
28 29 30 |
# File 'lib/pericope/verse.rb', line 28 def ==(other) to_a == other.to_a end |
#next ⇒ Object Also known as: succ
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/pericope/verse.rb', line 58 def next if partial? && (next_letter = letter.succ) <= Pericope.max_letter return self.class.new(book, chapter, verse, next_letter) end next_verse = verse + 1 if next_verse > Pericope.get_max_verse(book, chapter) next_chapter = chapter + 1 return nil if next_chapter > Pericope.get_max_chapter(book) self.class.new(book, next_chapter, 1) else self.class.new(book, chapter, next_verse) end end |
#partial? ⇒ Boolean
45 46 47 |
# File 'lib/pericope/verse.rb', line 45 def partial? !letter.nil? end |
#to_i ⇒ Object Also known as: number
32 33 34 |
# File 'lib/pericope/verse.rb', line 32 def to_i book * 1000000 + chapter * 1000 + verse end |
#to_id ⇒ Object
37 38 39 |
# File 'lib/pericope/verse.rb', line 37 def to_id "#{to_i}#{letter}" end |
#to_s(with_chapter: false) ⇒ Object
41 42 43 |
# File 'lib/pericope/verse.rb', line 41 def to_s(with_chapter: false) with_chapter ? "#{chapter}:#{verse}#{letter}" : "#{verse}#{letter}" end |
#whole ⇒ Object
53 54 55 56 |
# File 'lib/pericope/verse.rb', line 53 def whole return self unless partial? self.class.new(book, chapter, verse) end |
#whole? ⇒ Boolean
49 50 51 |
# File 'lib/pericope/verse.rb', line 49 def whole? letter.nil? end |