Class: Kj::Verse

Inherits:
Base
  • Object
show all
Defined in:
lib/kj/verse.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Verse

Returns a new instance of Verse.



10
11
12
13
14
# File 'lib/kj/verse.rb', line 10

def initialize(args)
  args.each do |k,v|
    instance_variable_set("@#{k}", v) unless v.nil?
  end
end

Instance Attribute Details

#chapterObject



50
51
52
53
54
55
# File 'lib/kj/verse.rb', line 50

def chapter
  @chapter ||= begin
    c = Db.query("SELECT id, book_id, number FROM chapters WHERE id = ?", [chapter_id], true)
    Chapter.new(id: c['id'], book_id: c['book_id'], number: c['number'])
  end
end

#chapter_idObject

Returns the value of attribute chapter_id.



7
8
9
# File 'lib/kj/verse.rb', line 7

def chapter_id
  @chapter_id
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/kj/verse.rb', line 7

def id
  @id
end

#numberObject

Returns the value of attribute number.



7
8
9
# File 'lib/kj/verse.rb', line 7

def number
  @number
end

#pageObject

Returns the value of attribute page.



7
8
9
# File 'lib/kj/verse.rb', line 7

def page
  @page
end

Class Method Details

.countObject



20
21
22
# File 'lib/kj/verse.rb', line 20

def self.count
  31102
end

.page(page) ⇒ Object



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

def self.page(page)
  verses = Db.query("SELECT id, chapter_id, number, page FROM verses WHERE page = ?", [page])
  verses.map{|verse| new(id: verse['id'], chapter_id: verse['chapter_id'], number: verse['number'], page: verse['page'])}
end

.page_countObject



41
42
43
# File 'lib/kj/verse.rb', line 41

def self.page_count
  1609
end

.percent(decimal) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kj/verse.rb', line 29

def self.percent(decimal)
  if decimal.round(3) <= 0
    verse_id = 1
  elsif decimal.round(3) >= 1
    verse_id = count
  else
    verse_id = (count * decimal).round
  end
  verse = Db.query("SELECT id, chapter_id, number, page FROM verses WHERE id = ?", [verse_id], true)
  new(id: verse['id'], chapter_id: verse['chapter_id'], number: verse['number'], page: verse['page'])
end

.randomObject



24
25
26
27
# File 'lib/kj/verse.rb', line 24

def self.random
  verse = Db.query("SELECT id, chapter_id, number, page FROM verses WHERE id = ?", [rand(1..count)], true)
  new(id: verse['id'], chapter_id: verse['chapter_id'], number: verse['number'], page: verse['page'])
end

Instance Method Details

#book_nameObject



61
62
63
# File 'lib/kj/verse.rb', line 61

def book_name
  chapter.book_name
end


65
66
67
# File 'lib/kj/verse.rb', line 65

def book_permalink
  chapter.book_permalink
end

#chapter_numberObject



57
58
59
# File 'lib/kj/verse.rb', line 57

def chapter_number
  chapter.number
end

#nextObject



73
74
75
76
77
78
79
80
81
# File 'lib/kj/verse.rb', line 73

def next
  @next ||= begin
    v = Db.query("SELECT id, chapter_id, text, number, page FROM verses WHERE id = ?", [id +  1], true)
    self.class.new(id: v['id'], chapter_id: v['chapter_id'], text: v['text'], number: v['number'], page: v['page'])
    rescue Kj::Iniquity
      v = Db.query("SELECT id, chapter_id, text, number, page FROM verses WHERE id = ?", [1], true)
      self.class.new(id: v['id'], chapter_id: v['chapter_id'], text: v['text'], number: v['number'], page: v['page'])
  end
end

#prevObject



83
84
85
86
87
88
89
90
91
# File 'lib/kj/verse.rb', line 83

def prev
  @prev ||= begin
    v = Db.query("SELECT id, chapter_id, text, number, page FROM verses WHERE id = ?", [id - 1], true)
    self.class.new(id: v['id'], chapter_id: v['chapter_id'], text: v['text'], number: v['number'], page: v['page'])
    rescue Kj::Iniquity
      v = Db.query("SELECT id, chapter_id, text, number, page FROM verses WHERE id = ?", [self.class.count], true)
      self.class.new(id: v['id'], chapter_id: v['chapter_id'], text: v['text'], number: v['number'], page: v['page'])
  end
end

#textObject



93
94
95
96
97
98
# File 'lib/kj/verse.rb', line 93

def text
  @text ||= begin
    verse = Db.query("SELECT text FROM verses WHERE id = ?", [id], true)
    verse['text']
  end
end

#titleObject



16
17
18
# File 'lib/kj/verse.rb', line 16

def title
  "#{book_name} #{chapter_number}:#{number}"
end

#to_sObject



69
70
71
# File 'lib/kj/verse.rb', line 69

def to_s
  text
end