Class: Pericope::Verse

Inherits:
Struct
  • Object
show all
Includes:
Comparable
Defined in:
lib/pericope/verse.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(book, chapter, verse, letter = nil) ⇒ Verse

Returns a new instance of Verse.

Raises:

  • (ArgumentError)


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

#bookObject

Returns the value of attribute book

Returns:

  • (Object)

    the current value of book



2
3
4
# File 'lib/pericope/verse.rb', line 2

def book
  @book
end

#chapterObject

Returns the value of attribute chapter

Returns:

  • (Object)

    the current value of chapter



2
3
4
# File 'lib/pericope/verse.rb', line 2

def chapter
  @chapter
end

#letterObject

Returns the value of attribute letter

Returns:

  • (Object)

    the current value of letter



2
3
4
# File 'lib/pericope/verse.rb', line 2

def letter
  @letter
end

#verseObject

Returns the value of attribute verse

Returns:

  • (Object)

    the current value of 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

Raises:

  • (ArgumentError)


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

#nextObject 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

Returns:

  • (Boolean)


45
46
47
# File 'lib/pericope/verse.rb', line 45

def partial?
  !letter.nil?
end

#to_iObject Also known as: number



32
33
34
# File 'lib/pericope/verse.rb', line 32

def to_i
  book * 1000000 + chapter * 1000 + verse
end

#to_idObject



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

#wholeObject



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

Returns:

  • (Boolean)


49
50
51
# File 'lib/pericope/verse.rb', line 49

def whole?
  letter.nil?
end